Top 5 Things to Learn to Write Good Code

 Writing code is easy, but writing quality code is difficult. There are many things to learn in terms of design concepts, patterns, and more. Here, we’ve created a list of 5 important things which will help any programmer learn how to improve their code quality.

 

Properly Naming Variables and Functions

When variables and functions are given good names, a developer can come to the codebase for the first time and understand exactly what each one does. Giving good names may sound easy, but often codebases will include many poorly named variables and functions, making it difficult to work with that codebase. Names should be brief but should be descriptive enough to indicate what they are for. This page on Writing Elegant Names for Variables and Functions breaks this down in much more detail.

 

Effectively Debugging

No matter how disciplined your approach to coding, defects will arise. An effective approach to analyzing and determining its root cause will save hours or days of time spent working on it. To summarize the ideal approach:

1.       Review the initial report and check for assumptions.

2.       Analyze the symptoms of the defect without drawing conclusions.

3.       Follow the path of code and data flow to the root cause.

The root cause might be a defect itself, or it can be something specifically pertaining to this defect. The Method of Debugging and Investigating Defects gives a more detailed overview of this process and working with it.

 

Strategy Design Pattern

Design Patterns are reusable concepts which can be applied to solve common problems. There are dozens of design patterns, but one of the most common is the Strategy Pattern. This is used in situations where some task needs to be done, such as logging, but the way we need to do it changes based on some conditions. This enables you to satisfy this requirement with clean and readable code. It’s based on using different classes to encapsulate different strategies, then selecting the required class at runtime. This article on the Strategy Pattern outlines this in more detail and how to use it.

 

Thinking Recursively

Recursion appears everywhere code is written. Recursion is a behavior which is exhibited when something is defined in terms of itself. This applies to functions, object definitions, and more. This concept isn’t limited to coding, but understanding it from a coding perspective is required if you want to write efficient and clean code. If you are new to this, A Non-Technical Introduction to Recursion may be helpful, or you can skip to writing recursive functions.

 

Binary Trees

No matter what kind of code you write, an understanding of binary trees will help break down and understand different types of problems. Binary trees come up in many situations: recursive functions, sorting inputs, situations where the program makes choices, etc. Even if you never code with a binary tree directly, an understanding of it will go a long way. This introduction to binary trees outlines this structure, and some related structures, in more detail.

Comments