Whitespace is a code smell

Published Friday, March 23, 2007 12:39 AM

Do you space out your code so there are line breaks between the pieces of logic?  Why do you think this is necessary?  Typically this is done to separate chunks of logic so that they can be easily distinguished.  If it is a complicated enough chunk, then it may even make sense to put a comment at the top of the chunk.  At this point, the Agile police will jump on you and tell you to turn your comment into a method name and perform an Extract Method refactoring.  This is great practice (fellow Agile police member here!) and it makes a lot of sense since it abstracts the concept and makes the code more self describing.

 But let's think back to those line breaks again ... if it needed the line breaks, then there is code smell.  If your code needs whitespace in the method to make it cleaner to read then it is probably doing too much.  Of course, this is very subjective and easy to argue (heck - our team can't fully agree on this either!) but think carefully the next time you find yourself adding extra whitespace to your code and think:

  • Is this code as simple as it should be?
  • Does this logic all belong here or could it be broken out into other methods/classes?

 

Jonathan Cogley is the CEO and founder of Thycotic Software, a .NET consulting company and ISV in Washington DC.  Our product, Secret Server is an enterprise password management system for teams to secure their passwords.  Where do you keep your passwords or do you use the same password everywhere?

Comments

# jayson knight said on Friday, March 23, 2007 12:16 PM

I disagree with this assertion. I personally put a linebreak between logical sections of code for aesthetic reasons only, as well as a comment or 2. Anything that makes code easier to read can't just assumingly be called a code smell, and that's a good way to start a holy war amongst developers :-).

Readability is paramount.

# John Walker said on Friday, March 23, 2007 12:17 PM

Ahh...another coding holy war about to break out :)

Seriously, I don't really buy into the fact that spacing out your code means something is potentially wrong with it or it could be simpler. I think it's more of a style issue.

My coding partner tends to space things out quite a bit, but I tend to shrink my code down with minimal line-breaks. At the end of the day, I don't think it really matters, but maybe that's just me. Interesting post for sure.

# Jonathan Cogley said on Friday, March 23, 2007 3:13 PM

You have to love a holy war about whitespace! :)

The key point in my post is just to think about why you want whitespace in your code when you are adding it.  Is the method too long, too complicated or doing too much?  Maybe not and everything is fine.

Several members on our team disagree with me on this point too but I think it is interesting to strive for no whitespace and still have readable code that is very specific in function.

Thanks for posting guys! :)

# Luke Breuer said on Thursday, March 29, 2007 2:33 PM

Isn't this discussion useless without actual code examples?

# Mat said on Tuesday, April 3, 2007 1:48 AM

I hacked my fonts so that the spacing between characters, words, and lines is zero pixels and my largest character is only 3 pixels wide and 5 pixels high.  I also don't use Enter.

# MHolmgren said on Tuesday, May 29, 2007 10:26 AM

Do you use blank rows between your one line methods, or do you just use extract class when this happens? :-)

This Blog

Syndication