Wednesday, September 03, 2008

Assume that someone who is less experienced than you needs to understand your code

Transparency is an important aspect in a project where many developers are working on the same software.

Some developers like to show of how smart they are using complex constructs that are difficult to understand. This is unnecessary and not fair towards their colleagues.

Some examples:

  • Unnecessary usage of the ?: operator and the null coalescing operator. These operators are not commonly known and should only be used if they improve the readability of the code.
  • Complex expressions whose output depends on the operator precedence rules.
  • Usage of anonymous methods.
  • Usage of regular expressions. Regular expressions can be very powerful, but they have the nasty habbit that they tend to be easier to write than to read.
    Consider rewriting your logic using simple string actions. If you really need to use regular expressions, you should document them very well.

If you encounter a situation where one of these techniques really makes sense then of course you should use it. But you should also add some documentation why you are doing it (and maybe include a link to a website that explains the technique).


Donny said...

Wow!!! I did not know about "null coalescing operator". That is handy. Any down side to using that?
...besides people not knowing about it. ;-)

Anonymous said...

Yes, with all due respect, at first look I think the null coalescing operator is something that needs to become more widely known/used, and not go on the shelf of arcane constructs! It seems like it could save a lot of typing!

BTW I have enjoyed reading your various C# articles this afternoon. TY!

Dave said...

I think that ?: is pretty widely understood.
The null colaescing operator is pretty new, though.