3 Best Tips For Writing Better Code.

Writing beautiful, better and readable code is an art.

Konadu Akwasi Akuoko
4 min readFeb 11, 2021

Elegant code adopts high aesthetics to achieve high maintainability-Ruben Verbogh

A dog playing with computers.
Photo by Cookie the Pom on Unsplash

As a growing software engineer someday I will be tasked to do a code review of another developer. Imagine doing a code review for a programmer who only thinks about his/her code’s ability to work, this programmer doesn’t care about code maintainability, readability, and code efficiency. I know it will be a real pain working with such a developer, that is why we must all learn to write better codes. Here are my top 3 tips that I use when coding to write better code.

  1. Apply the DRY principle.
  2. Keep indentation consistent.
  3. Use coding conventions.

1. Apply the DRY principle.

The DRY principle means Don’t Repeat Yourself. It means every line of code or a piece of code must have a single, unambiguous, authoritative representation within the environment. I first learned about this principle from Ann Adaya, and I am glad I did it. The dry principle can sometimes separate a junior developer from a senior developer. Imagine you are given a task to perform, the task is to calculate the total scores of students and send this report to various school departments, would you write the code for calculating the scores and duplicate it at places you would need them, or would you rather create a function and paste the code in it, and call it every time you need it?

A man painting a repeated parttern over a cloth.
Photo by Francisco Arnela on Unsplash

By following the dry principle you can put that in a public function and later call it whenever you need it. We must also note that the DRY does not refer to code only according to the Pragmatic Programmer algorithms or ways to solve problems can also be typed once and used elsewhere. Why do the DRY principle? Using the DRY principle makes you avoid duplication of code and thus helps you to declutter your code making reading your codes easier, also the DRY principle makes the act of debugging easier, when algorithms are put into functions it’s easier to find and see where errors are.

2. Keep indentation consistent.

A very beautiful code is one that does take indentation seriously. Compare the following examples,

The first code:

public void UpdateLives(int currentLives) { //sprite changes everytime there is a change in the player's life, due to the use of an array _livesImage.sprite = _livesSprite[currentLives]; if (currentLives == 0){ GameOverSequence(); }}

The second code:

public void UpdateLives(int currentLives){//sprite changes everytime there is a change in the player's life, due to the use of an array_livesImage.sprite = _livesSprite[currentLives];if (currentLives == 0){GameOverSequence();}}

Which one of the above code is better to understand and read? Code indentation makes our code look very professional, every code must divide into units with each unit performing a specific task.

A well organized table makes a room beautiful, so as it is to code a well indented code looks beatifull.
Photo by Surface on Unsplash

By keeping a consistent indentation scheme you are making your code more readable, beautiful, and easy to debug. There are different kinds of indentation and everyone has their unique style, if you are in a team do well to follow the indentation scheme they use.

3. Use coding conventions.

According to Wikipedia coding conventions are a set of guidelines for a specific programming language that recommend programming style, practices, and methods for each aspect of a program written in that language. By using coding conventions you use a set of guidelines and practices to make your code very nice, but that is not the main reason for using coding conventions, if we use coding conventions it really helps makes our code readable, very understandable, and also it makes the introduction of new engineers into our codebase very easy.

A man reading through his code.
Photo by Patrick Amoy on Unsplash

This is the coding convention I use myself, I use this because I usually code in C#. Every programming language, team, and companies have their coding conventions which help them to code faster and efficiently as possible. Learn the coding conventions of your programming language of choice, your team, and your company, by doing so you are helping to write readable and understandable code.

Writing better code as a developer is part of our game, we just don’t focus on writing codes that work. By applying the DRY principle, using indentation and keeping it consistent, and using coding conventions we stand a chance to improve the readability, maintainability, and easier debugging of our code. For the most part it can improve relationship between you and your senior developer.

A happy group of people.
Photo by Campaign Creators on Unsplash

I hope these tips helped you and gave you some idea of how a readable code should look like, if you have any ideas please don’t hesitate to share them with me either in the comments or on Twitter, you can also join me on YouTube as we do some gamedev. If you enjoyed the article you can leave as many as 50 claps to help us create more content you love. You can also join our newsletter “Programmer’s diary.” here. I wish you all the best and until next time happy coding.

--

--