Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 1, 2021 06:46 am GMT

Best Practices for Writing Super Readable Code

1. Commenting & Documentation

IDE's (Integrated Development Environment) have come a long way in the past few years. This made commenting your code more useful than ever. Following certain standards in your comments allows IDE's and other tools to utilize them in different ways.

The comments I added at the function definition can be previewed whenever I use that function, even from other files.Here is another example where I call a function from a third party library : In these particular examples, the type of commenting (or documentation) used is based on PHPDoc, and the IDE is Aptana.

2 Million+ WordPress Themes & Plugins, Web & Email Templates, UI Kits and More

Download thousands of WordPress themes and plugins, web templates, UI elements, and much more with an Envato Elements membership. Get unlimited access to a growing library to millions of creative and code assets.

2. Consistent Indentation

I assume you already know that you should indent your code. However, it's also worth noting that it is a good idea to keep your indentation style consistent.

There are more than one way of indenting code.

Style 1:

function foo(){    if ($maybe)    {        do_it_now();        again();    }    else    {        abort_mission();    }    finalize();}Style 2:function foo() {    if ($maybe) {        do_it_now();        again();    } else {        abort_mission();    }    finalize();}Style 3:function foo(){   if ($maybe)    {   do_it_now();        again();    }    else    {   abort_mission();    }    finalize();}

I used to code in style #2 but recently switched to #1. But that is only a matter of preference. There is no "best" style that everyone should be following. Actually, the best style, is a consistent style. If you are part of a team or if you are contributing code to a project, you should follow the existing style that is being used in that project.

The indentation styles are not always completely distinct from one another. Sometimes, they mix different rules. For example, in PEAR Coding Standards, the opening bracket "{" goes on the same line as control structures, but they go to the next line after function definitions.

PEAR Style:

function foo(){                     // placed on the next line    if ($maybe) {     // placed on the same line        do_it_now();        again();    } else {        abort_mission();    }    finalize();}

Also note that they are using four spaces instead of tabs for indentations.

3. Avoid Obvious Comments

Commenting your code is fantastic; however, it can be overdone or just be plain redundant. Take this example:

// get the country code$country_code = get_country_code($_SERVER['REMOTE_ADDR']);// if country code is USif ($country_code == 'US') {    // display the form input for state    echo form_input_state();}

When the text is that obvious, it's really not productive to repeat it within comments.

If you must comment on that code, you can simply combine it to a single line instead:

// display state selection for US users$country_code = get_country_code($_SERVER['REMOTE_ADDR']);if ($country_code == 'US') {    echo form_input_state();}

Get some more from here


Original Link: https://dev.to/devto4/best-practices-for-writing-super-readable-code-37hm

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To