Wednesday, October 3, 2018

9 tips to write clean code, nice code

I have been writing code for 20 years. During that time, I worked with 17 different language development teams to build hundreds of projects. These include everything from a simple blog, to APIs that support 3,000 requests per second, to bestselling apps.
From these experiences, I believe the most important thing about code is that it's easy to read.
This is something that many developers still overlook or do not care about. Unfortunately, because the code readability will actually change and affect everything in your project. In this article, I will list 9 ways to help you write better and easier to read code.

Puzzled in choosing Format

Too many things are wasted when you keep thinking about formatting. It's a never-ending debate between Tab vs Space. Or Allman vs. K & R. Instead, apply a standard format to the codebase and automate it. Then, you can focus that energy on the code.

Code died

All commented blocks, unused variables and unreachable code are considered dead. Over time, they will kill your codebase so that you have to search and kill the dead code. While it does not have to be your main focus, always be a Scout.

Code heavy

The foundation of almost all code is logic. We write code to make decisions and calculate. This often leads to branches or loops that create cross-cube and nested code blocks. Thus, it makes the code complicated and unreadable. You can solve this problem with the return, guard clause or features of functional programming.

Use objects

Although this is the era of object-oriented programming, devs still write very bad code with long winding parameters, data clusters, and custom array / dictionary structures. However, they can be refactored into objects. This helps to unify the structure of the data.

The code is too long

When you determine that the code is too long, immediately recognize , regroup , and refactor it. This simple process allows you to define the context and complexity of the code so that you can refactor it to readability and less complexity.

To name

Certainly, naming is always a difficult task. But that is because we make it more complicated than necessary. There is a little trick that works well with many things in programming, including naming. That is, never let yourself be stuck for thinking about naming something. Instead, continue writing the code. Putting a name that is clear and simple and time-consuming to take care of the code will be more practical.

Leave a comment

Do not write code that you must constantly explain, instead, try to write code that is easy to understand and need as little comment as possible. As such, your writing skills will improve.

Use a reasonable return

You should try to return a reasonable value instead NullIdeally, something that allows code to call to perform even in the case of a negative value. If there are really special cases, you still have better ways to handle them than they are null.

Symmetry - Symmetry

Symmetry represents the creative side of the text. It is the foundation of many other practices: naming, structure, objects, pattern. It may vary by language, codebase, and development team. However, once you start applying symmetry to your code, things will become clearer and easier to understand.

No comments:

Post a Comment