Saturday, November 16, 2019

Have The Courage to Declutter - Best Practices Series


The things in our homes each have their own purpose, and they have their proper place.  In the course of time there is the inevitable accumulation of things, and this can become clutter which is a problem.  This is a way to think about programming.  It is not a perfect metaphor of course but I think it is useful.

Clean Your Room and Take Out the Trash

Living is a messy affair.  We do all kinds of things in our rooms.  Things like eating, opening and reading mail, getting dressed and undressed, play video games, etc.  If we don't stop and put things away and throw out the trash our rooms will unlivable.  How can we apply this when programming? First all if you find that you are commenting out code (in some languages comments are called remarks) then after a while your code will become harder to read and maintain.  Similarly if you notice you never use some part of your code any more it might be a good idea to delete that code.

Be brave and delete the old code.  If you want to keep the code then save the file in a different name so that you won't lose that version of it.  If you are using some kind of source code control (also known as version control) then you will never lose old code.

Don't Be a Hoarder

Hoarders are afraid to get rid of things and they often have many of the same kinds of things until their house fills up with so much stuff that they can't find anything and there is no room to live in their house anymore.

How to implement this idea in programming?  Each idea should only be expressed in one place. Do you notice sometimes when you're writing code that you are doing the same thing again and again so that when you change something then it needs to be changed everywhere you have that similar code?  You need to create a subroutine or a function that does that one thing in only one place, and then call that routine from all those places where the code was duplicated.  This is a kind of code refactoring.

Live In a Tiny House

Imagine your program must fit in a constrained memory.  What is the purpose of this, you might ask?  Today we have what is essentially unlimited memory in our computers.  If you just sat down and wrote code every day for the rest of your life you would never be able to fill up all that memory.  You might not be able to resist the temptation to add every single feature that you can to your software.  People who live in a tiny house often ask themselves what are the essential things, and what can be disposed of?  The more features you have, the more code you have, and the more complicated it is, and this makes it harder to extend and maintain.

    -"Everything should be as simple as possible, but not simpler." -- Albert Einstein

I hope this article is helpful to you and will give you something to incorporate into your work.

No comments: