How to write good code? Part 1.

Yurii K
4 min readMar 8, 2019

--

How can we measure code quality? It is not about lines count in file and it is not about files count in project.

Let’s just say you wrote a code, everything works fine. A month later you found a bug in that code and you must read and debug your own code. Now it is take a lot of time to understand your own code.

If you understand the code well, then your code is good. Code understanding is important to us, that is how we measure it.

Here I have some advises which make code much easier to understand and maintain.

Folder hierarchy

We all know this type of hierarchy: if we have models we create model folder and put all our models there. The same story with services, controllers and other, this type of hierarchy works fine if you have a small project. It is very easy to find what you need because you have a couple files there, but this hierarchy does not fit for big project.

Standart hierarchy

In most cases we create object for specific place and we use that object only in that place. Imagine we create a web site. We create a page then we create a component for that page, table, models, service all those stuff we use only in that page. That how we should create our hierarchy.

Good hierarchy

We create main part of our application in this case it is page and we put all services, all models that we use on this page inside of page folder.

What benefit we have from that hierarchy?

  1. Now we see a whole picture objects that we use on a page.
  2. It is very easy to find what we need.

Let’s say we have a bug — title inside of a table is wrong. We found out the table is located on a home page. We go inside of table component and we found an issue one of the model have a wrong property. It was easy to find an issue because this hierarchy help us. Now imagine that we have a standard hierarchy, it will take much more time to find an issue.

Formatting

First of all our code must be formatted and have one lines for separation. Formatted code much better to read, if it is easy to read it is easy to understand.

Bad formatting

Not so much people know about two lines for separation.

Two lines

Every book contains paragraphs and chapters if we compare the book and the code, one line is a paragraph two lines is a chapter.

Good formatting

The code above is very easy to read and understand because it have separation, good naming and a few comments which explain what the code do.

Order

Inner elements of the class must be in order. Here is example:

  • Fields
  • Constructors
  • Properties
  • Methods

And we also should put all elements in alphabet order, it is good for search.

Order

Commnets

Everybody write comments in code, but most of the people write bad comments.

Bad comments

Why this comments are bad? Because all what comments tell to us — this a bad code we need to rewrite it. When time come to rewrite it we do not remember why we put this comments here, we totally forget an issue. That mean comments will be there forever.

My advise — do not comment bad code rewrite it now when you remember an issue.

But if you still need to put comments in code do it right. Write in comments a small story, write an issue which you found out and tried to solve but you can not do it because you out of time, write why this solution is bad. It is much easier to understand meaning of comments when you have an explanation of issue.

Originally published at tomorrowmeannever.wordpress.com on March 8, 2019.

--

--

Yurii K

Full-stack developer. In my work, I use Angular and C#. In my free time I write articles for my blog.