閉じるボタン Login as
Member / Promoter

[KIT] How to write better code

Being a programmer, a developer, or a software engineer can be very tough. Writing code that works does not mean that your job is done, it must be written cleverly and in a form that is readable. So that your team or people who going to work with it afterward, can understand it. It is always advisable to practice writing consistent and readable code at any time.

For this article, we will be giving some tips that could surely help you to write better code. These are some factor that you will need to look out for while writing your code.
1. Spacing and Indentation
2. Comments
3. Naming
4. Set a limit
And so, I will make some personal recommendations to help you address these issues.

  1. SPACING and INDENTATION
    Adding some extra style to your code will surely help, not to make it look pretty and cool, but to make it much more readable in a more humane way. You should keep your code spaced in a specific manner and with consistent indentation, whether it is a 2-space, 4-space, or a single tab.
    Why are spacing and indentation important? Here is some examples code:

    function(age,has_id){
    if(age>=18 && has_id==true)
    {return true}};

    function(age, has_id) {
    if (age >= 18 && has_id == true) {
    return true
    }
    };

    between the 2 code block above, which one is easier to read? Of course, it’s going to be the second code block. The first code block does not have proper indentation and spacing making it really complicate and hard to read. This is the importance of spacing and indentation. With the help of spacing you and easily divide each property inside the code and comprehend the logic of the code. While indentation will help us in comprehending the scope of each code block
  2. COMMENTS
    Comments on every code file will help us easily identify what a piece of code going to do. Comments can be very useful especially when writing a huge project with lots of code files. Imagine this, you were tasked to debug some code in your project that you have written 6 months ago, the project involved 10 developers. Without the help of comments, it could be very difficult to work with others’ codes and also your own code when you forgot what it does. You have to spend lots of time just to find out what a piece of code does. If you were to give a comment on it, you will make things much easier for you and the people you’re working with.
  3. Naming
    Naming variables, functions, and classes are also important when writing code. A name should be specific, unique, consistent, and easy to understand. Do not go with generic names such as temp, n, number, name, etc. A name can be packed full of information. For example, when naming a variable the velocity of a moving car:
    car_v_kmh = 0
    Based on the name, it obviously means that the variable holds the value of a car velocity in km/h, however, if a variable was something like this
    velocity = 0
    You will struggle to find out what the variable is for. Is it a velocity of a car? or a plane? Also there also the measurement issue you need to figure out, whether it is in miles or kilometers.

    You should also be consistent with how you name variables, functions, and classes. There is 2 standard way every programmer use to name their variables and functions. They are camelCase and underscore_name. You can choose whatever you want depending on your style and stick to it. As for classes, it usually has CapitalizeName mixed with camel case. If you are wondering what is a camel case, it is typography in which no space and punctuation are not needed. The separation of each word is indicated with a single capitalized letter. For example, icedLemonJuice.
  4. Set a limit
    What do I mean by setting a limit? Code can be very long and messy. So set a limit to how long a line of code should be, best practice is to have it at 80-100 characters long. Also how much code should exist in a single file. Do not pack everything into one single file. Break it down into smaller chunks saved in separate files so you can navigate much better. For example, instead of having the main function, classes, and functions mixed up together, you can have a file for classes and a file for functions, while the main file is kept separately.
    Set a limit to how long a name should be too while you’re at it.

That is all from me. I hoped you learn something new from this. I tried to kept this article as short as possible, while trying to give out as much information as I could. There is much more practice you should follow as well, I recommend you to search for documents guidelines for different language. If you’re writing python code, search for PEP 8. If you need extra help to write better code in any language, try installing linter for the language you are using.

Related posts

  1. [KIT] 4 temples you willing to go when visiting Cambodia’s Angkor Wat

  2. [KIT] How to overcome depression

  3. [KIT] 8 Tips to maximize your days

  4. [KIT]How to move on from something

  5. [KIT]How to prepare for the exam in one week

  6. [KIT] How to change yourself to become a new version of you

PAGE TOP