There is a trend that I’ve been seeing recently that I find somewhat disturbing. It primarily manifests itself with C# programmers, who also tend to be really indignant when questioned about it. The issue is whitespace in source code.
When I see a big block of C# code and all the text is crammed into as little space as possible, it is very difficult to read. This means that it takes longer to figure out what the code. This means that it takes me longer to do my job, making me more expensive to my employer or client. What benefit does this have with efficiency? Further, although unrelated to this post, is the use of significant coding shortcut expressions in C#, which the developers praise as so efficient and elegant, but make the code barely intelligible.
Whenever I ask one of the coders about this, their answer is that whitespace is for people and compilers don’t need whitespace. That response baffles me because it sounds like they are arguing my point. The whole point of source code is to be human-readable. But, somehow in their mind, it sounds like whitespace slows the application down.
Years ago, I read an excellent book, Developing User Interfaces for Microsoft Windows. Although it’s rather outdated now, it had a lot of good advice in it, and one of the tips was to make use of whitespace for code clarity. Up until that time, I didn’t pay much attention to blank lines and I had a different indenting scheme then what was the standard. But then I changed both of these and my code became immediately more readable.
Although it maybe sounds a bit obvious, I demand whitespace in my code because I am a writer and an avid reader. I need paragraph breaks to indicate to me when a topic is changing or a new thought is starting. If you treat writing a program like writing a story, your code will be much easier to understand; and to echo the C# developers, the compiler won’t care.
Aside from the line breaks between methods and between logical code blocks within methods, I like to put all my variable declarations at the beginning of the method. It introduces you to all the characters in the chapter and gives you an idea of how complex the plotline of the chapter is. This is also out of fashion with the current declare-just-before-use style.
One of my other structural designs that goes against the current fashion is to put my properties at the beginning of the class instead of at the end. This is the same structure as a UML diagram, so I’m not sure why that design practice changed. With methods, I try to put all my event handlers first, then order the methods by their access level (public, friend, protected, private). Finally, I put methods that are called by other methods later in the class, so if you need to reference a called method, you almost always scroll down instead of up to find the called method. This is made easier since private methods are last in the class.
Reading this post without whitespace is what it is like to read source code without whitespace. It sucks.