Skip to main content

Clean Code

Muhammad FurqanAbout 2 min

Introduction

Tips

Programming is the art of telling another human what one wants the computer to do. Donald Knuth

If you speak well, people will love to listen to you. Communication is an art that either can make someone fall in love with you or can give you critics. Similarly in the world of programming. If you write horrible code, you will not be able to tell another programmer why you had written it. It’s a harsh reality that you work under the pressure of deadlines which makes for hastily written code. But keep this fact in mind, a badly written codebase is going to make your life more difficult.

Programmers usually want to learn different programming languages. In today’s world, there is a wrong perception that one who knows several technologies or the latest technologies, is a hero. But believe me, there is no meaning in learning Java, CSS, AngularJS, Perl, Ruby, or Python, if at the end of the day, you are not able to admire your code. In the end, you become "Jack of all, Master of none". It’s not just the technology; it’s the code that makes software powerful.

You’re the author, you’re the artist of writing code. Improve your code writing every day. Each day is a new day to improve what you learned yesterday. Improve your coding day by day. Try to make it cleaner, more beautiful, and small every day. Be the best artist at writing source code. When you start to embrace this thinking, you tell yourself, "This can much be better". You make it better and better than starting to admire your code. You start feeling proud of yourself.

Tips

One of the best programming skills you can have is knowing when to walk away for a while. Oscar Godson

Don't Make Anyone Think

The code should be easy to read and understand without much thinking. If it isn't then there is a prospect of simplification.

How to write clean code?

Tips

Code is clean when it is easily readable by developers other than the original author as well.

A few of the broad guidelines to write clean code are:

  • Give meaningful names to variables, functions, classes, and other entities in the code.

  • Create functions that are small and do a single thing.

  • Declare local variables as close as you can to their usage.

  • Declare instance variables at the top of the class.

  • Constants should be declared at the top of the class or in a Constants class by example.

  • Encapsulate related data and functions into small independent classes.

  • Structure the code for better readability. Keep related code together and keep the lines smaller.

  • Objects expose behavior and hide data. Conversely, data structures expose data and lacks of (significant) behavior.

  • If you use third-party wrap third-party code/API/libraries, wrap them, so if they change, only your wrapper implementation needs to change.

  • Code should always be separated with blank lines to club logical blocks together. Think of different lines of code as thoughts and then always think of organizing similar thoughts together.