Coding Like an Artist
I recently stumbled upon a blog with the interesting title “Code like a girl,” which presents a question I have spent many hours thinking about, but never realized it: is aesthetic code important? My answer to the question, as both an artist and a programmer, is yes – aesthetics are extremely important.
However, to fully explain my position on the topic I should first outline what I consider to be “aesthetic.” Beauty, after all, is in the eye of the beholder. The following outlines a few things I consider important when writing attractive, readable code:
- Excessive whitespace is kept to a minimum, but is appropriate where shifts in the logic of the function occur. For example, I would put a line of whitespace in between a chunk of code that reads from a file, and a chunk of code that writes to a file. I might also insert a line of whitespace between the area where I declare my variables, and the area in which I use them.
Lines of code that preform similar tasks are grouped together when appropriate. If two pieces of code are similar, but separated by hundreds of lines of code, it is inappropriate to group them together. This “rule” is best applied to localized regions of the code. - Obey an 80 character line limit. This ensures that even vi users will enjoy looking at your code since they won’t have to look at ugly line wraps. I will admit that I sometimes violate this aesthetic principal more than I would like to admit.
- Code with words. I prefer to see English words instead of heavily abbreviated variable names because I find that it is more intuitive to read when you are first skimming code, especially if someone else has written it. A well named variable name shouldn’t even need Hungarian Notation. However, I also recognize that some people consider the increased verbosity unnecessary and unappealing to read, and I sometimes even find myself agreeing with them. In this situation, I believe a balance between natural language and succinctness is most aesthetic.
- Indent your code with spaces, not tabs, and never mix the two. Spaces will ensure that your code looks the same regardless of where it is being examined. If spaces and tabs are mixed the tab may not be displayed with the same width as your spaced indents, and you will end up looking at jagged, uneven lines of code.
I could go on an on with this list, but for the sake of brevity, I will end there. The list above, however, only outlines visually aesthetic elements. There is, however, a more abstract tier of aesthetics, which I will call functional aesthetics. Code that is functionally aesthetic executes in a simple or clever way, but is also intuitive and efficient upon examination. I think a great example of functionally aesthetic code exists within the open source Spring framework. While it may have a learning curve, once you become familiar with its workings you suddenly see the brilliance of the approach, and you come to love working with it. Functional aesthetics is that feeling of “wow, this design just makes sense” coupled with computational efficiency.
Aesthetics has even fueled the emergence of new paradigms. Take the MVC programming paradigm for example. As programming websites became a larger, more complicated endeavor, programmers discovered that mixing business logic code with presentation code (often in the form of HTML) was both difficult to maintain and extremely unaesthetic. The MVC paradigm was born to counteract this. Not only is the MVC approach more aesthetic visually, with the business logic carefully separated from the presentation through the use of a model, but it is also more functionally aesthetic because of its ability to scale more fluently.
Without even knowing it, I have long been using all of these ideas in my own code, especially in my PHP framework (a pet project I have been tinkering with for a long time now). As I work on my framework, I am constantly asking myself questions like, “how can I make this simpler and easier to use?” or, “how can I leverage the strengths of the PHP?” and even, “how can I make this function easier to read and take up fewer lines of code?” The fact that I ask myself these questions at all demonstrates that aesthetics are important when coding. Gender, however, has little to do with the issue. I have seen women and men alike write extremely ugly code, both visually and functionally.
Programming is an art form, making each programmer an artist. Visually speaking, it might not be as sophisticated as one of Vermeer’s masterpieces, but making your code easy to read is certainly important. The programming world has its own masterpieces, as well. Projects like he Apache web server and Linux immediately come to my mind. Clearly, aesthetics has a very important role in programming, but it is perhaps misunderstood, or poorly labeled with more “left-brained” terms such as “processor efficiency,” or other dehumanizing titles. One thing is for certain – I will never be ashamed to ask myself, “is my code pretty?”