Don’t Program by Fear, Question Everything
- by João Angelo
Perusing some code base I’ve recently came across with a code comment that I would like to share. It was something like this:
class Animal
{
public Animal() { this.Id = Guid.NewGuid(); }
public Guid Id { get; private set; }
}
class Cat : Animal
{
public Cat()
: base() // Always call base since it's not always done automatically
{ }
}
Note: All class names were changed to protect the innocent.
To clear any possible doubts the C# specification explicitly states that:
If an instance constructor has no constructor initializer, a constructor initializer of the form base() is implicitly provided. Thus, an instance constructor declaration of the form
C(...) {...}
is exactly equivalent to
C(...): base() {...}
So in conclusion it’s clearly an incorrect comment but what I find alarming is how a comment like that gets into a code base and survives the test of time. Not to forget what it can do to someone who is making a jump from other technologies to C# and reads stuff like that.