Naming Conventions

Code is written to be read by humans, not for machines so it makes sense that following some basic ground rules for the look and feel of your code could make your code easier to read and boost your productivity. This is particularly important if you work in teams where each developer can go off and write their code in entirely different ways. Reading these different styles hinders your productivity. In my opinion if you add up all the few extra seconds here and there, it all adds up to extra hours or even days wasted over the course of a year per developer.

Disclaimer: At the end of the day, there are no rules for coding style. This is all a matter of personal preference.

I’ve recently been doing a fair amount of T-SQL and C++ and thought I’d look into some form of naming conventions for the two languages. If you’ve read my previous blog post ‘Stop the Brace Wars, Use StyleCop‘, then you’ll know how I feel about coding style in the C# language.

SQL Coding Style

The SQL language is an interesting case, its a really old language from 1974 and in those days they didn’t even have keyboards that could deal with upper and lower case letters!

A lot of examples you’ll see in books have used all-caps for the SQL keywords like ‘SELECT’ and ‘WHERE’. As is widely researched, all-caps is really bad for readability. However, as this StackOverflow article shows, all-caps is still a really popular style of writing SQL.



In the above example, the all capitals doesn’t look too bad. It’s a very short SQL statement and helps break up the three parts of the query. You could argue, that the SQL keywords are coloured blue, so we don’t need the capitalization and that’s a pretty good argument. As an aside please remember that colour blindness affects approximately 1 in 12 men and 1 in 200 women in the world (source).

But this is a really simple SQL statement, if you start writing a stored procedure of any complexity, things get ugly pretty fast (Imagine writing C# with upper case keywords, yuck!). Happily though, SQL developers seemed to have cottoned onto this. A lot more real world SQL examples on blogs and forums seem to be all lower-case. Even to me though, all lower case SQL does not look entirely correct, perhaps I’ve just been conditioned into all-caps, it is however easier to read for more complex T-SQL.

C++ Coding Style

C++ is fairly similar to C#, so for me as a mainly C# developer it’s easier to write in a similar style. However, I was not happy with this approach and wanted to see what was being done elsewhere and what was the more ‘correct’ approach, if there was one.

I found the Google C++ Style Guide which is a really detailed, yet simple set of guidelines for how to write your C++. Definitely worth a quick read.

Conclusions

Coding style is a deeply personal subject and a pretty important one too that is often overlooked. In my opinion, it’s always worth spending a little time looking up the preferred methods (There will usually be more than one) of writing in any particular language and picking one of the most popular approaches.

If you’re working in a team, you’ll reap the benefits pretty quickly. Code written by others will look just like yours, saving you precious seconds. Even if your a solo developer, you’ll benefit. Developers are inherently plagiarists, copying snippets of code found on-line written by others. Using a common coding style will mean that your style is more likely to be the same as the next snippet of code you or I shamelessly copy from the internet.