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 othe…


This content originally appeared on DEV Community and was authored by Dev to

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 US
if ($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


This content originally appeared on DEV Community and was authored by Dev to


Print Share Comment Cite Upload Translate Updates
APA

Dev to | Sciencx (2021-05-01T06:46:46+00:00) Best Practices for Writing Super Readable Code. Retrieved from https://www.scien.cx/2021/05/01/best-practices-for-writing-super-readable-code/

MLA
" » Best Practices for Writing Super Readable Code." Dev to | Sciencx - Saturday May 1, 2021, https://www.scien.cx/2021/05/01/best-practices-for-writing-super-readable-code/
HARVARD
Dev to | Sciencx Saturday May 1, 2021 » Best Practices for Writing Super Readable Code., viewed ,<https://www.scien.cx/2021/05/01/best-practices-for-writing-super-readable-code/>
VANCOUVER
Dev to | Sciencx - » Best Practices for Writing Super Readable Code. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/01/best-practices-for-writing-super-readable-code/
CHICAGO
" » Best Practices for Writing Super Readable Code." Dev to | Sciencx - Accessed . https://www.scien.cx/2021/05/01/best-practices-for-writing-super-readable-code/
IEEE
" » Best Practices for Writing Super Readable Code." Dev to | Sciencx [Online]. Available: https://www.scien.cx/2021/05/01/best-practices-for-writing-super-readable-code/. [Accessed: ]
rf:citation
» Best Practices for Writing Super Readable Code | Dev to | Sciencx | https://www.scien.cx/2021/05/01/best-practices-for-writing-super-readable-code/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.