/* Is this comment actually useful? */

The snipped from SO

Recently, while looking for a solution to a simple problem of parsing a string into Enum that would have a default value, I’ve found this solution on Stack Overflow:

TypeEnumeration type = Enum.TryParse(inputString, out type)
                    ? type
                    : TypeEnum.Default 

Looks simple enough, but if you’re not programming in C# you might be just as confused as one of my coworkers that I’ve shown that to. In C# you can use the ternary operator, which works like one line if-else clause when making assignments:

var areEqual = (2 == 2) ? true : false;

The expression in this example is checking if 2 is equal to 2, which would then assign the first value (true) to variable areEqual. Back to our initial example Enum.TryParse return boolean value of true if it successfully recognizes inputString as one of the values of TypeEnumeration enum. So ultimately we’re assigning to variable type value of itself if parsing was successful or TypeEnum.Default if the string was not matched.

Should it have comment

That when a discussion sparked should there be a comment that would explain what the line does or not. Angelo’s position is that comments are just as important as code and they might’ve helped understand confusing code, especially for junior developers.

The actual comment would be something like

// This will parse inputString into enum or assigns it a default value

What I found problematic with this solution is that comment is disassociated with actual code. There is nothing preventing me from inserting something between those two and making comment not only useless but straightforward misleading.

As a strong advocate of no-comments-ever philosophy (there, I might as well end my blog post right here) I think that code should be self-documenting and for that reason, I’ve come up with the following solution:

var type = inputString.EnumParseWithDefaultValue(TypeEnum.Default);
public static TypeEnum EnumParseWithDefaultValue(this string input, TypeEnum default) 
    TypeEnumeration type = Enum.TryParse(inputString, out type)
                    ? type
                    : default

Take note that this is not a shorter solution than using comments. It’s also more decoupled (as a call to method and method will be in totally different places), but it offers granularity when trying to understand the code: “Oh EnumParseWithDefaultValue method called on a string that gives me enum. Ok, let’s move on” with the ability to dig dipper if you’re not satisfied.

So whenever you put a block of code that you think requires a comment to explain what it’s actually trying to achieve, why not extract it to a method and try to come up with the best possible name for it? Essentially the method name is the comment here, just without this annoying green text 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s