Don’t Program by Fear, Question Everything

Posted by João Angelo on Exceptional Code See other posts from Exceptional Code or by João Angelo
Published on Wed, 29 Sep 2010 21:56:10 +0000 Indexed on 2010/12/06 16:59 UTC
Read the original article Hit count: 524

Filed under:
|

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.


© Exceptional Code or respective owner

Related posts about c#

Related posts about wtf