You will want to know these 5 tips for error free code - Part One

Posted by Anthony Eli Agbenu on Tuesday, June 2, 2020
Last Modified on Wednesday, June 3, 2020

TOC

Why have clean code

Because filth is disgusting

No one likes filth, especially in this pandemic era. Everyone the world over wants to be clean. In fact we all want to be very clean. Thus, soap and water are now one of the most used and prized possessions in every household all over the globe.

It goes without saying that cleaning and washing your hands has become crucially important.

What is clean code ?

Similarly, when it comes to creating software, it does not hurt to also produce bug free software. Error or bug free software is the result of clean code.

I would define clean code as well written and maintained pieces of code. There are many reasons to write clean code. A consequence of producing error plagued software is that it gives you gives you a bad name.

And with a bad name ( or reputation ) comes other undesirables that people associate with you as well as with your software and your work. Here a few tips to help you write clean code.

Tips to have clean code

Use a framework

Use that which has already been tried and tested

I might have a backlash on this point. However I still stand by it. Frameworks have done alot of the work we would have had to spend so much time recreating. Frameworks also add structure to your code. I personally believed in frameworks as far back as 2010, when CakePHP was the most used framework for PHP.

And in this day and age, I would be personally against starting projects that are based on no frameworks, for they make it both faster and easier for you to code the product.

Think less about the code and more about the product holistically. That is where the value is. The product.

Reuse pieces of code

It is a waste of everyone’s time to reinvent the wheel

This is where functions, methods and classes come in. You can keep copying and pasting code all over. Unfortunately, it makes your work look very nasty indeed.

Instead, I would advise you write it once, and place it inside a function, or class. Afterwards just pass the necessary inputs, and have it return the output you desire.

Show a little class and keep your work organized in a class.

Add comments

Sometimes it is best to say it in plain English

To prevent the next coder from struggling with your code, just write what you are trying to do in plain English. In this way, comments can be life saver. I have found no problem reading my own code, even without comments. However when it is somebody’s else own messed up huge spaghetti like code, then there could be a problem.

It would be just easier if he leaves useful comments to help me out. Comments are a bit like professional courtesy. They give consideration to the next developer who has to carry on that piece of work in your absence.

When you pick up someone else’s work you want to be able to understand it. Comments help us pass that knowledge to other developers long after we have left the project.

Review your code

Review your old work, and make it better

I know the immediate push back would be you do not have enough time to write code, much less review your old ones. However when it is possible, I still insist you go back to your software.

This is especially true for those projects of yours which are still in still in production. I bet you would want them to run more efficiently, would you not ?

So take a little time out of your day and change those 50 lines of code to 3 lines. Change that if else statement to an itenary operator. Small things like that.

Implement version control

In the absense of time travel, version control is the next closest thing

It might shock you to know, but somewhere on this green God given earth of ours, someone somewhere is not saving their code into git. Your jaws might have dropped but it is true. And for some others it might just be because they forgot. That was the same with me in 2014.

I forgot to save an Android app I did. And before I knew it that laptop had developed a fault. Unfortunately the laptop had to be formatted. And there went my app.

It was only after the damage had been done did I remember that I forgot to save that specific project. Luckily, it was only about three days worth of work that went with it. Still, I took it for granted because i was always committed my codes.

Learn from my mistake and do not repeat similar evils.

Remember to wash your hands:

That is it for now! You see how having clean code is not that difficult. There are many more tips I would like to share on having clean code. I will post these soon.

In the meantime, make steady progress and you would have shiny software in no time. And remember to pleeeeeeeeease wash your hands.

After all, we want to keep all things clean.

Cheers!


comments powered by Disqus