Remove your code

iamprovidence
5 min readOct 5, 2022

--

Do you know what is the best way to write clean code? By removing it.

You just finished your program. The code is done. It is yours code. You like it. You care about it. You feel sorry for him. I understand. Let this feeling to cool down. It can take one hour, maybe two. Go and drink some coffee or vine. Don’t look to your code. Let all feeling to go away.

Your thoughts will turn to it frequently, and you’ll likely be tempted a dozen times or more to look at it. If only you could re-read some pieces that seem particularly fine in your memory, something you feel proud of, so you can re-experience what a really excellent programmer you are. Resist temptation. Wait until the moment you have no feeling inside. Done?

And now when you are ready, you are allowed to have a look in your code once again. Look at it. Look how fancy and sophisticated it is. Look at it and remove it. Forget about that girly bullshit and grow some balls already. It is not your code anymore. Be ruthless. Remove it.

Here is the ultimate formula to write clean code — ‘Write your code and then remove 20% of it, better even 30%’

When reviewing your code, you could have a fake feeling that something can be done better if only you could write a little more here and there. That’s the worst that could happen to you. You are not ducking Shakespeare, and we are not writing a novel here. The best you can do with your code is to delete it.

I have met developers that were so scared they would not write better code that they have now, that they would not be better professional that they are now, so instead of deleting code they would try to save him. Do you know what they did? They commented it. Why the heck are you commenting your code? Code should work. If it is commented, it does not work and therefore does not deserve existing. That how simple it is. If you see commented code on your project, delete it.

What else you can delete? Your code. Question your code more. Ask yourself, “What I can delete here?”. Ask yourself this as often as possible.

Here some practice for you. Ask yourself, “What is wrong here? What can I delete here?”

Have you noticed it? Why do you need ternary operator here? Delete it!

Here is another one:

What the point of that resultvariable? I have seen people doing this a lot. Just delete it. If you created a variable to simplify the debugging process, and you did it consciously, then let it be. If you're done it because you were not questioning what you are doing, then remove it.

You think those are simple examples? Here is my favorite one. I have seen multiple experienced developers write code like this:

Firstly, we are checking whenever collection is null. Sounds reasonable. Collection is a reference value and therefore can be null. However, LINQ methods are designed in a way that they will always return an object instead of null, even if that object is an empty collection. So what seems to be logically correct just makes your code clumsy.

What else can we remove? What about that if statement at line 5? Why do we need it? What is the point of that? Nobody who write such code could not explain it to me. Remove your code and see it blooming.

Ask yourself, “Do I need this? Can this be removed?” Yes, it may be null? But is it expected? If not, just remove it. Remove all those useless validations! Remove tons of try/catch statements! Find something to remove.

Long name with full namespaces in it? Remove it! Unused variables? Remove it! You wrote a comment that explain your code. Why would you do that? I think at this point you know what your next move. Delete that comment and create a function with appropriate name.

Your language probably have some sugar built in to help you to remove your code. If you really want to be better at removing your code, you should learn your language first. Use any opportunity, any tool available to get rid of your code.

Remember, you are writing for people, not for a compiler. Do not remove spaces if they help to understand your code. Do not get rid of formatting because it will save you two more lines. Other than that, you are good to go. Just question your code more often, for God’s sake.

Every time you see some code, think, “What I can remove here?” Developers usually feel sorry when removing code. They spend time and efforts on it. Do you know what differentiate a professional developer from a looser? A professional developer is a butcher. He is a terminator. Machine with no emotions. He removes his code. It is hard, but if you want to be like that, you got to be strong. If something can be removed — remove it. With no hesitation. No doubt. Remove your code.

You like reviewing somebody else code, don’t you? You are enjoying criticizing other people with snarky comments? You like to be sharp and smartass. And we both know deep inside you are enjoying hurting people, saying how bad their code is. Start criticizing yourself!

Start seeing what can be removed and remember to remove your code!

I hope you get as much fun reading it as I writing it 😃

Just wanted to give you some motivation during routine work 🙌

There are so other posts where I'm trying in a funky and quirky way to explain complex programming stuff, so you may check them out 👇

There is also a link to support me ☕️

Follow and clap to receive more of those ✅

And now when your break is done, you are ready to get back to your code, do not forget to remove it 😃

--

--

iamprovidence

👨🏼‍💻 Full Stack Dev writing about software architecture, patterns and other programming stuff https://www.buymeacoffee.com/iamprovidence