Mittwoch, 14. Dezember 2011

On code formatting

Creating a piece of software is a creative process. Just like an artist sculpts a piece of rock with his tools so it looks like the picture of the statue that he has in mind, a programmer has a distinct idea of how his program is going to work. A programmer uses a different way to put his idea into reality than an artist though. He does not need a hammer or other physical tools. Instead he utilizes a computer and writes down his idea in a language that the computer understands.

Of course most these languages have rather strict rules, so a mashine can understand them. But these limitations are a reason why a programmer needs creativity to achieve his goal: He can't just write "Check the weather and tell me what I need to wear today!" The computer does not know what "weather" is and so he does not know how to check the weather. So the programmer needs to tell the computer what "weather" is and what it needs to know to determine which clothes (another thing that needs explanation for a computer) to wear. So a good program that uses online weather forecasts to tell which clothes to wear will end up in lots of files written in a programming language.
These source codes are very easy to read for a computer and they also can not be misunderstood by it because everything has only one single meaning. Since a computer reads the code differently, in most of the languages it does not matter how big the spaces between the words in our sentences are. Most of the time it does not even matter if we write everything in one long line which you could only write on a very very long roll of paper. But writing everything in a long line makes the code which already is not that easy to read even harder to understand. Thus programmers got together and agreed: "We shall write our programs, so they are easy to read." And they talked about how one could do that and wrote down their results. These ideas that they had are now known as conventions of style between programmers. And mind you - there is not only one set of conventions of style, but there are many different for each programming language and every single programmer adapts the rules that he thinks are best for his own code.
Although I already wrote much about those conventions I don't want to go into detail here. Even though this entry is titled "On Code Formatting", I don't want to talk about how to format your code right. I personally don't believe that there is only one single way of doing it right. Instead there are lots of different styles which all serve different purposes.

Some - especially new - programmerss are too lazy or hasty to do proper code formatting. This is something that I've seen especially in students that hand in solutions for work assignments. And yes, I have even done this myself - and I am still doing in some circumstances. The reason for this is rather simple: Code Formatting means more work while writing your code - which is the only thing you see while you are doing it. The big tradeoff is later when you have an error in the code and you want to find it. In good formatted code you can find a specific thing much easier.

*** Warning: From here on I will start ranting a bit. ***
Nowadays you can choose from many different programs to write your code in. Programmers call these "IDE"'s. Some of those IDEs like NetBeans, VisualStudio, Eclipse, etc. come with a hell of functions that you'll probably never need and others simple text editors as notepad, gedit, vim, etc.
Those functions also cover code formatting.

A simple example: When you write your code and you start a block like "Do this block of instructions only if it is the 3rd of november: ...", it is agreed that you indent the instructions inside the block, so you can easily see what is inside and when the block ends. If you press the enter key to start a new line, very simple editors like Windows' notepad go to the beginning of that line. Other editors - especially those that were made for programmers - start a new line at the same indentation as the previous one, so you don't need to press a lot of spaces to get where you want to.

Other programs go as far as to automatically add spaces after commas, mathematical operators (+, -, etc.) and the like and some even add line breaks for you automatically in specific cases. Of course this leads to a dispute about whether those functions are good and helpful or whether they are harmful and bad.

While a lot of programmers say "A good programmer should be able to write well formatted code on himself, thus those programs are made by the devil." I beg to differ. As a programmer I am - as stated above - undergoing a creative process and I already am restricted in my actions by the programming language itself (just as a musician who is restricted by his instrument - he will never be able to make piano sounds coming out of his violin) while writing my code. I need to think about how to make the program do what it should (we programmers call this "solving the problem") and when I have to heed the code formatting rules, I need to cut thinking power from "How am I going to make this work" to "Oh, is this written neatly, so everyone can read it?" Especially when the opinions of what neat code actually is differ. So if my IDE starts formatting my code for me as I write it (or when I tell it to, if I don't want it to happen automatically) I can concentrate on what my work is all about: Explaning the computer what it has to do in a language that it understands. In my personal opinion having to bother about correct indentation, spacing and so on is a burden to a programmer. I think now that we are living in the year 2011 we should leave those tasks to someone who can do it much more reliable and faster: The computer. I mean - that's what they're for, aren't they? Okay - apart from porn and video games. But when a programmer writes a program which is to become a tool for someone else then he is doing something to make the life of that person easier by being able to use the computer for something he would have to do by hand otherwise.

It seems that programmers want to be the exception. Actually the reason I am writing this is because someone told me that people who make tools that automatically format your code don't know anything about programming. I think it's exactly the opposite: They understood what being a programmer means. Programming isn't just the process of writing down your code into a file and then letting the computer "translate" it into an internal language, so it becomes a real program. It is a process of thinking about how you are going to tell the computer what to do. Especially during "debugging" - which is finding errors and mistakes in programs - you often find yourself in the situation where you don't understand what could be wrong. Sometimes the approach that you take is to simply recode a specific part in a different way - just like when you take a paragraph from a novel and replace it with a new one which contains the same content but is written in your own words. But it is not about concentrating on writing neat code - especially not when it's proven that the computer is able to do that for you (in some IDEs even so highly configurable that it will use your personal style for the autmatic code formatting).

Well of course not all IDEs have these functions and those that have them don't support every language - but to me a simple question arises: "Why?" Of course for little languages nobody bothered of writing routines that do that for you but there are bigger languages that are common don't have a single IDE that supports automatic code formatting. It seems as if the people "hating" this function block the idea of adding it to the IDEs...

I have actually never heard any arguments for formatting the code yourself which is kinda sad, because it gives one the (hopefully wrong) feeling that people that are strong against auto-formatting (and I am talking about those who want to deny the use of that function to others) are afraid that they are using a kinda elite-position as coding becomes easier and easier. Either that or they are jealous that they didn't have those functions "back in their days". So the (unspoken) argument would be: "No, you can't take that hardship away from programmers or else programming would become mainstream" - or "No, you can't give them that feature - they already have it easy enough not having to punch holes in big stacks paper cards to code their programs". Nothing about punch-cards by the way. I personally think they are cool, but - they are as outdated as having to format your code by hand.

Well this rant would not be complete with a little clarification to those who like formatting their code by hand: I don't have anything against doing it voluntarily. I just can't understand why it is needed to force others to do it also.
And also I need to add that I am open to arguments for having to do it yourself as long as they are not "So you are able do it when you don't have the IDE that does it for you" - seriously: IDEs can be changed, so they support it this just isn't done - or "Because I didn't had that function too."

Thank you for reading so far. I will post further rants - erm - posts about other topics when I feel like it.