When in Rome, code as the Romans do

This entry was posted on Friday, January 25th, 2008 at 5:50 am and is filed under Development, Working Life.

No doubt most developers are in favour of coding standards, but something that has annoyed me of late is ‘coding baggage’. For example, former Visual Basic programmers using VB style in C#. Such style pollutes the purity of a language that has its own style for a particular reason.

Many years ago, I spent 6 months in India. For the first three months, I was staying at a rural polytechnic in Uttar Pradesh, and I tried to learn some Hindi for my own interest, and because I felt it would help me settle in and go some way to befriending the mainly non English-speaking people in the area. Some of my fellow travellers felt no need to make an effort, and insisted on saying English words more loudly and slowly. Funnily enough, this doesn’t really work.

I make this comparison to people who bring coding style and standards from previous languages. I’ve recently been working on C# code that screams of work from a former VB programmer. There are prefixes to all the variable names to indicate their type, constants are capitalised, method parameters are prefixed with additional information as to whether they are passed by reference, and members are prefixed with ‘m’.

public const string cstrDataGridXml = "DataGridDetails.xml";

public EditableTable(string p_strTableName,string p_strTableDesc, bool p_bExtended)
{
	mstrTableName = p_strTableName;
	mstrTableDescription = p_strTableDesc;
	mbExtended = p_bExtended;
}

I find this type of thing mildly infuriating – it’s like going to India and shouting English. To bring this old coding style to a new language blurs the distinction of coding standards between languages, the very coding standards which are established because of language features. To mix the styles blurs this distinction, and reduces the clarity and purpose of those coding standards.

I come from a C++ background, and although I don’t touch the stuff these days, you can bet that if I returned, I’d be coding as I did back in the year 2000. Now I’m doing C#, I use those the standards defined by Microsoft, who, to be fair, should know a thing or two about C#.

Having started Ruby and Ruby on Rails, I code Ruby like a Ruby programmer – so you won’t find any m_ prefixes for member variables or such like. Perhaps one day, another Ruby programmer will look at my code. If it’s styled like Ruby, their understanding and comfort in reading my code will be maximised.

This naming also extends to control prefixes in ASP.NET and WinForms. Drop a TextBox onto a web form and by default, you’ll get TextBox1. So why change the prefix to txtBox?

I think it’s important for developers to respect the language they’re programming in, and code accordingly – although the exact content of a coding standard for language ‘X’ promotes contention, a mishmash surely doesn’t have any advantages.

Leave a Reply