Day 2: What are the best practices for writing readable code?


Code readability is a universal subject in the world of computer programming. It’s one of the first things we learn as developers.

Today, the challenge is to share one important thing to consider when writing readable code. You should nominate another one after posting yours.

I’ll start with a simple one,

Comments
Comments showing bad code. Good code should be understandable without a line of comments. But what to do to save time for new developers? — Write simple inline documentation describing what and how method work. This will save much time for understanding and even more — it will give people more chances to come up with a better implementation of this method. And also it will be a good start for global code documentation.

I nominate @Gravewalker :stuck_out_tongue:

2 Likes

Code alignment
Keeping the right alignment on your code improves the readability of your code. Breakdown the lines that are too long into a few readable lines. Always keep the children elements inside the parent block. Bad code alignments make your teammates go nuts.

I nominate @piumal1999 to continue.

1 Like

Naming elements

Name the ids, class names, variables etc. with a meaningful names. For best practice, names should have consistent naming scheme. camelCase and underscores can be used for it.
:negative_squared_cross_mark:
“x”,“element1”, “other_element”, “another-element”

:white_check_mark:
“template-videos”, “onelive_heading”, “calenderId”

I nominate @anjisvj

1 Like

Limit Line Length

Our eyes are more comfortable when reading tall and narrow columns of text.It is a good practice to avoid writing horizontally long lines of code. It is a good idea to to limit the line length to around 100 characters.

I nominate @Yohan to continue. :wink:

1 Like

Code Grouping

More often than not, certain tasks require a few lines of code. It is a good practice to keep these tasks within separate blocks of code, with some spaces between them.

I nominate @osusara :wink:

1 Like

File and Folder Organization

If we want, we can write the whole code in a single file, but it will be a nightmare to read such code. So best practice is, separate folders and files in a meaningful manner. As an example, We can create a separate folder for user interface components. As well as, we can create separate file for each GUI element.

I nominate @BhathiyaTK :grin:

1 Like