17 November 2010

I do see a lot of source codes, both in the projects at the job and sources I download for "inspiration". Some ugly, some nice.. but mostly ugly.
Just like in art there is no general rule for what is ugly and what is beautiful. It's all in our heads, some may consider p_privateField as a rule, while others as the ugliest thing in the world. Also there is the society, that imposes rules of conduct that it expects others to follow, and as a rule there are the "rejects" that decide to follow no rules.
I do try to follow some rules for formatting and arranging the code, some I consider beautiful and some helpful. There are rules that help other developers better understand the code written by others, it's like music genres, if you like rock you can understand rock, but classic will sound weird. There are also rules that help avoid bugs.
After all that said let's get to some practical stuff.

Spaces vs Tabs
When indenting the code you can use spaces or tabs. Some recommend spaces while others using Tabs.
There is no general rule for that. As for me, I see not a single reason for using spaces for indentation. There is a cons, it takes longer to delete the lines of code.
Tabs are more easy to manage. It's easier to indent the code just by hitting a few times the tabs button. You can set the tab size whatever you like in your environment and no other developer will complain that you insert only one space for indenting. And it's faster to delete a lines of indented code.

Line size
Yes, yes, yes, most of us have HD monitors this days ( I am still waiting they will give me one at the job, yes I am a dreamer ), but you have to respect your colleagues with 1280x1024 monitors. Also consider the colleagues with laptops, horizontal scrolling is not that comfortable (after microsoft inserted a bug in the VS2010 and it does not accept horizontal scroll from my mouse).
Back in the days 80 was a rule. Later it was 100. Now it's falling into chaos.
I did read on a blog that since the HD monitors are becoming more widely used we should use longer lines and even more expressions per line, but hey, why would you want to do that??
It is still good practice to keep it to 100 chars. Small lines are always easier to comprehend. Just as an example, do you prefer big books with long lines or small books? Even newspapers make the lines narrow for easier reading.
In case you consider 100 outdated, a good way to guide is to see the smallest monitors in the team and keep the line sizes within the working area.
As an updated, a friend of mine pointed out a very good reason to keep the lines short. You can have 2 parallel windows with code simultaneously on a HD monitor. Useful when comparing bits of code, merging, or doing a similar implementation from another file.

I'll keep this post short (for the first one in the series) and end it here. Comments are welcomed.

blog comments powered by Disqus