Wednesday, April 29, 2009

Magic Numbers

I have been dabbling in 4 or 5 different code bases over the last couple weeks and over the course of these weeks I have noticed a much too common pattern involving magic numbers. Every few files I would come across a number that was part of a class definition, method call, etc that had no significant meaning by itself. Take a quick peek at this line of code I found & refactored inside of a class definition:

def get_remaining_schedules
15 - schedules.size
end

Now at second glance you can probably gather that the maximum # of schedules is 15 and we are just subtracting the current count from that total to get the remainder available. However, wouldn't this make more sense at a first glance if we did something like this?

MAXIMUM_SCHEDULES_ALLOWED=15

def get_remaining_schedules
MAXIMUM_SCHEDULES_ALLOWED - schedules.size
end

Now you can immediately see what is happening here as you skim the code (I like to use constants in a case like this because they stand out to my eye.) Now you may say that makes sense but within the method body the # 15 is understandable anyways? Why go the extra step? Well, I think it's important for people to be able to see exactly what the programmer's intent was at a glance if possible. So I say why not? What happens if we end up needing to know the maximum schedules allowed somewhere else in the class or calling code? Do we just drop the # 15 wherever we need to know the maximum schedules allowed? You can see how that might spiral in a hurry especially when your co worker has to grep for the #15 and try to figure out if it is related to what he/she is doing or not! Take a peek at another piece of this code I modified:

#snippet I refactored
<%= link_to_unless (15-@user.schedules.size)==0, "New Schedule", new_schedule_path %>

#refactor
<%= link_to_unless @user.schedules_limit_exceeded?, "New Schedule", new_schedule_path %>

#Which one is more readable? Which one is easier to maintain?

I think it's obvious sometimes when you're looking at code from an outside perspective or reviewing it after the fact but that is also why I dropped this post. I think any programmer that is refactoring and reviewing their work consistently would see that magic numbers take away from readability, add to maintenance headaches, and ultimately make sure not to use them.

In any event, this is just a quick little tidbit you can use when you brush by some code and have to stop, say "What the hell is that?", then answer yourself "Oh I see.". This is one of the simplest things you can quickly refactor into something much more readable at little cost to you or your employer.

In closing, happy coding and remember readability and maintainability is what we are striving for so the next time you see your co worker they say "Thanks for that sweet code bro!".

2 comments:

Drop a Line