
\documentclass[12pt]{article}
\usepackage[T2A,OT1]{fontenc}
\usepackage[default]{cantarell}
\usepackage[a4paper, top=20mm, bottom=20mm, left=20mm, right=20mm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[russian, english]{babel}
\usepackage{tabu}
\usepackage{hyperref}
\usepackage{parskip}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage[normalem]{ulem}
\usepackage{float}
\floatstyle{boxed}
\restylefloat{figure}
\usepackage{setspace}
\onehalfspacing
\author{Artyom Bologov \href{mailto:ugly-things@aartaka.me}{(email)}}
\date{\today}
\title{Some Things Have To Be Ugly}
\makeatletter
\def\endenv{\expandafter\end\expandafter{\@currenvir}}
\makeatother
\begin{document}
\maketitle

\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{./assets/ugly-things.png}

Some things are just ugly and you have to live with it.
Or even make some things ugly deliberately—to highlight the complexity behind the code.

\section*{Accidentally Ugly} \label{accidentally-ugly}

I was working on "humanization" of medical data on my dayjob.
The task is pretty simple: take the data, check its type, output the human-readable text.
The data is strongly typed too, so it's a matter of dispatching over the type.
I'm gonna use \verb|case|!

\begin{figure}[h!]\begin{verbatim}
;; Four lines
(case (:type data)
 ("Patient" "Person") (process-person ...)
 "Address"            (process-address ...)
 (process-default ...))
\end{verbatim}\caption{Simple case over data types}\end{figure}

But what if \verb|data| is malformed or null?
We need to handle that too!
But \verb|case| no longer cuts it, because we need to make arbitrary checks.
Well, we can encode these in the "else" branch of the \verb|case|,
but it's going to get dirty and deep indentation-wise.
Better use good old \verb|cond|:

\begin{figure}[h!]\begin{verbatim}
;; Six lines
(let [type (:type data)]
  (cond
    (nil? data)         (throw ...)
    (contains? #{"Patient" "Person"} type) (process-person ...)
    (= type "Address")  (process-address ...)
    :else               (process-default ...)))
\end{verbatim}\caption{cond dispatch}\end{figure}

But okay, now we need to make some conditional logic in the Patient/Person branch.
Humans are never easy to encode...

\begin{figure}[h!]\begin{verbatim}
;; Thirteen lines
(let [type (:type data)]
  (cond
    (nil? data)
    (throw ...)
    (contains? #{"Patient" "Person"} type)
    (cond
      (...) ...
      (...) ...
      :else ...)
    (= type "Address")
    (process-address ...)
    :else
    (process-default ...)))
\end{verbatim}\caption{cond dispatch}\end{figure}

Can I inline it in the toplevel \verb|cond|?
Yes, but what if there are five "special" cases like that?
The \verb|cond| grows longer, more branched and duplicated.

Now to the fun stuff: some entities might have sub-entities.
These need encoding too.
So we also need to \verb|recur| on the function containing these:

\begin{figure}[h!]\begin{verbatim}
;; Sixteen lines
(let [type (:type data)]
  (cond
    (nil? data)
    (throw ...)
    (contains? #{"Patient" "Person"} type)
    (cond
      (...) ...
      (...) ...
      :else ...)
    (= type "Address")
    (process-address ...)
    (= type "Entity")
    (str ...
         (recur (:sub-entity data) "SubEntity"))
    :else
    (process-default ...)))
\end{verbatim}\caption{Cond+recur, the control flow gets convoluted}\end{figure}

At this point, one (Artyom) might be tempted to rewrite it all with \verb|defmethod|-s.
Because we're dispatching over data and then delegating some logic to specialized methods.
Must be a beautiful architecture...
But it'll take twice as many lines and won't be able to handle more complex checks.
Not today, methods.

...

You might see where I'm getting with this:
Some code is ugly enough to not fit in.
No amount of cool language constructs can handle real world.
Some home-made macros can manage it... for a while.

But some things are inherently and inevitably ugly.
Because problem domain often is ugly enough to require ugly code.
Face it.

\section*{Deliberately Ugly} \label{deliberately-ugly}

Now to the controversial part: you should make your code ugly.
No, not all of it, but some parts at least!

Ugly code is the code that embraces the complexity.
Complexity of the data, complexity of the algorithm, complexity of the problem domain.
Making ugly code in a real world setting is not shameful.
It's the only mode of existence we have when we encounter complexity.

Complexity needs a lot of verbal and mental energy to process and convey.
We (programmers) are more fluent in code than in words.
Ugly code is a better representation of complexity than some domain glossary.
It's a literal recipe for what complexity there is and how to handle it.

It must stay that ugly.
And it must be made ugly if it not yet is.
Embrace ugly code and fight the complexity on your own terms.


\par\noindent\rule{\textwidth}{0.4pt}
\href{https://creativecommons.org/licenses/by/4.0}{CC-BY 4.0} 2022-2026 by Artyom Bologov (aartaka,)
\href{https://codeberg.org/aartaka/pages/commit/a91befa}{with one commit remixing Claude-generated code}.
Any and all opinions listed here are my own and not representative of my employers; future, past and present.
\end{document}
