Ambrosius Topor

Notiz
(2017-10-13)

Coding Style: Return expected last

Example: Person object

function getFullName() {

    if (this.first_name && this.last_name) {

        return this.first_name + ' ' + this.last_name;
    }

    // More checks/validation

    return '';
}

Return the expected result as last statement.
More checks might follow after validating first name and last name not being empty.

function getFullName() {

    if (!this.first_name || !this.last_name) {

        return '';
    }

    return this.first_name + ' ' + this.last_name;
}

Example: Database connect function

function connect() {
    if (db.connect()) {
        if (db.selectdb()) {
            return db.getConnection();
        }
        else {
            return null;
        }
    }
    else {
        return null;
    }
}
function connect() {

    if (!db.connect()) {
        return null;
    }

    if (!db.selectdb()) {
        return null;
    }

    return db.getConnection();
}