Superfluous Code and Readability


I have currently taken over a large project written for several other developers over a period of several years.  Like a lot of long term projects things get bloated and like a Sara Lee pastry it seem to be folded layer upon layer.

But, as I am going through I am seeing a lot of funny things, and I felt, looking over it that I needed to include some small tips of writing code that would be correct technically but is superfluous based on maintenance and what the compiler will remove.

But a question I have is, should a programmer be reliant on the compiler to fix errors or just issues with your code or should you write decent clean code off the bat and the compiler will just do its things.  Compilers are complex beasts, even simple ones are stuff for serious computer science people but when we write code that a compiler must correct simply to make it better does that mean there is something wrong with our code or coding.

Private Enum MyEnum
End Enum

Public Sub Main()
	Dim x as Integer = 0
	If MyEnum.TypeOne = CType(x, MyEnum) Then
		Console.WriteLine("They Match")
		Console.WriteLine("They Don't Match")
	End If
End Sub

Consider the code above.  It is correct in all senses, technically.  It will compile and will achieve the desired result.  But why is the CType  there.  I mean, yes it is correct, but it is a little confusing to look at.  So why do it?  I don’t know why, as there is no need to perform this type conversion for this comparison to work.

If x = MyEnum.TypeOne Then

This line is a little easier to understand and the output that is executed ( the Assembly ) is the same.  I could also pose, if the end output being executed on the chip is the same does it matter.  Yes and No. 

Thankfully compiler optimisations are such that they are using such complex heuristics to analyse your code that if you see the code

i += 1;
i += 1;
i += 1;
i += 1;
i += 1;

That the compiler would

i += 5;

It makes sense to, why execute that 5 times when all is needed is one.  Other things like constant replacements, where you use;

Public Const MY_CONSTANT As Long = 9876273

CSharpAnd every where you find the MY_CONSTANT it replaces it with that number.  But letting the compiler do its thing is one point the other is making your code readable.  Using Constants with meaningful names doesn’t make it readable than having magic numbers just appearing, but unnecessary type conversion  just can confusing the sometimes already confusing code.

The Condition to Nowhere

This is a good one really, I see it in so many places and in the early days of programming, this was when I was learning programming for the first time I was guilty of it.

Normally it is this;

If True Then
	'Do Nothing
End If

OK, that is OK in some respects, not good programming.  The issue I found was this;

if (dataAdapter.dataTable(0).Count > 0 && dataAdapter.dataTable("Table").Field.ToString() == "MyName")

Yes, that is right, an empty if statement.  The true nature of what that is there is unknown, did it have code in there and they removed it, did they plan to have code in there at some point in the future.  But if it is either case surely comment it out. 

Why?  There are calls to objects and checks that aren’t needed.  The reason why the compile will leave this as it is, is due to what those calls might do.  The compiler can do some good things but to to determine all parts of what may or may not be called based on data it doesn’t know anything about, it will simply leave those calls in place, each takes time to process.

Though the best one I saw recently was this;

Private parameter As String
Public Sub Main()
End Sub

Private Sub PerformCall(parameterName as String)
	If parameter = parameterName  Then
	End If
End Sub

VBYou might think this looks a little silly and certainly the circumstances on what I wrote were not exact, but it shows you what I saw, a class variable being passed into a method call, and that variable being compared to the passed in parameter and it simply returning while performing no action.

This astounds me as to why, or more importantly, how this happens.  I am by no means a perfect programmer, I am still learning many things about the craft and will be happy to learn many new things as I go on.  I make mistakes but I go along, learn and fix them.  But the above example, and seeing it in live code, was just mind boggling.

So I will run some short entries about simple programming tips, often ones that we do not get exposed to other than when you first start programming and even more experience programmers feel they don’t need to ever revisit these things.  Helpful for the beginners and certainly should be mind jogging for the veterans out there.


Posted on October 18, 2013, in Development and tagged , . Bookmark the permalink. Leave a comment.

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: