6 Jun

The Best Code You Will Ever Write

Category:UncategorizedTag: :

If you?ve been writing code for any decent length of time, you will most likely come to the same conclusion I have.

If you?re new to writing code, I?m about to give you the single biggest secret to developing software.

The best code you will ever write is? code you never write.

No matter how good of a developer you are the simple truth of the matter is: any code you write will add complexity to your software system.

It?s true, any time you add functionality or logic to a system you make it more complicated.? You may refactor some code and make the overall structure of the code more simple, but inevitably you will add complexity.

Salvation in a keystoke

It is not all gloom and doom from the developer looking to introduce simplicity to a code-base.? There is one single key on the keyboard, which has done more for the cause of simplicity than any code that any developer has ever written: DELETE.


Good developers love to delete code.

The only thing I love to do more than write code is delete code.? Deleting code brings me inner peace and joy.? Deleting code is violating the law of entropy and setting the universe right.

Trust in source control

Often it is the case that developers are stricken by an unhealthy fear of deleting code.? Perhaps it is a hold-over from the days when most software organizations didn?t use source control.? Perhaps it comes from the pack-rat mentality of “what if I need this someday.?

Whatever the reason is, many developers are afraid to delete code.

If you?re one of those developers, I am here to set you free.


Repeat after me: ?I trust my source control system to save the previous versions of any code I delete.? I hereby solemnly swear to delete any commented out code.? I promise to delete any code which is determined to the best of my knowledge as dead code.?

Don?t be afraid to delete code. If you have deemed that some code isn?t doing anything or it has no purpose hanging around, purge it.? If you find that you need that code later, you can always pull it out of source control.? Source control will remember that code exactly as you left it.

But it?s not hurting anyone

So why not just leave that dead code or commented code around?? Why go out of your way to try to find ways to remove code and reduce code? There is no victim to this crime.? No one gets hurt by having some dead code hanging around, or do they?

Every line of code in a software system has a maintenance cost associated with it, even dead commented out code, has to be maintained.

Don?t believe me?? What happens when you have some commented out code that calls a method, and you change the name of the method?? You have to, or rather you should, update the reference in the commented code.? If you don?t, if you were to uncomment that code, it would no longer work.

So you can see that even commented code has a maintenance cost associated with it.? The same can be said for dead code or copy and pasted excess code.? The maintenance cost may be small, but it does exist and it adds up.

The other pain with dead code is the cost of comprehension.? If you leave a method in a class that does nothing, every traveler coming through that class has to look at it, understand that is it not relevant and move on.

How many cars have to come down the road blocked by road construction and turn around, before you put up the ?detour? sign?

Finding opportunity

Here are some tips which can help make you a ?code hero? by doing the right thing, deleting that code.

  • Look for commented out blocks of code.? Almost every time I see a commented out block of code, I delete it.? There is virtually no excuse someone can give me which will save the life of that poor abandoned code.
  • Many IDEs and tools have the ability to detect unreachable code.? Unless some kind of reflection magic is happening, unreachable code is dead code and is our enemy.? Terminate it with much prejudice.
  • Copy and pasted code blocks are dead code in disguise.? The code is actually functioning, but it is still dead code.? Find a way to consolidate the functionality into a single point that all the copy and pasted code can call, and delete the original code pointing it to the consolidated code.
  • Tests that do nothing but get compiled are worthless.? Get rid of them and don?t forget the ?Ego Tests?, kill them or fix them.
  • Look for old code that does things that libraries now do.? If you find some implementation of a linked list, or a stack, or some other operation which exists in a base library, check and make sure there isn?t some really good reason it was implemented custom, and if there isn?t, take it out and replace it with a library call.

    As always, you can subscribe to this RSS feed to follow my posts on elegant code.? Feel free to check out my main personal blog at http://simpleprogrammer.com, which has a wider range of posts, updated 2-3 times a week.? Also, you can follow me on twitter here.

13 thoughts on “The Best Code You Will Ever Write

  1. When I do code reviews, commented code is one of the first thing I scan for in the diff. If there’s commented out code it gets rejected and sent back to either uncomment or delete. Checking in commented out code is purely unnecessary. Was the code checked in commented out by accident? (I.e. was the modifying user testing something out and forgot to re-activate the “real” code?) Quite often the response is “Oh, that shouldn’t have been left commented out.”

  2. Great post. Only one comment regarding:

    Many IDEs and tools have the ability to detect unreachable code. Unless some kind of reflection magic is happening, unreachable code is dead code and is our enemy. Terminate it with much prejudice.

    Just a word of caution that these tools assume that enumerations can only take on their enumerated values. In C and C a run away pointer can overwrite any variable, so I typically have a default case with an assert(0). I know that the code should never reach there, but if it does, I want to catch it.

  3. IMHO, the reason folks are “afraid of the source control” is that nobody ever looks there.

    When a developer considers erasing code for functionality that *might* one day be reintroduced, he asks the question: “will whoever reintroduces the functionality use my code or reinvent the wheel?”.

    He figures (correctly) that this other person (or himself after much time has passed and memory has faded) would not go through the entire version history to look for this code, but rather start from scratch. Hence the motivation to leave zombies lying around.

    While I don’t advocate leaving dead code, I feel that we need some other mechanism to provide awareness of its existence. Even a comment saying: “Code for doing X was removed in revision Y” might do.

  4. So I’m a PO and only code on the weekends…but I LOVE this and (as my team knows) I am a huge supporter of clearing out cruft and dead code. Delete!

  5. I love deleting code. My favourite C# slimming-down refactoring is to find an interface with only one method, replace it with a delegate and then fix the rest of the code accordingly. Usually turns out that you can delete a bunch of wordy classes that are only used in one place, and replace each of them with a minuscule lambda. So much noise goes away.

    I find that photo of the falling backwards “trust exercise” scary though. I’m never going to trust anyone else to look after my spine for me.

Comments are closed.