mardi 4 décembre 2018

Following with rule - `"if" statement needs to call a function instead of having a body' - I stuck by choosing between 3 approaches

How to write code without if body and call a function instead for better readability. Each approach as I think has disadvantages 1. Unnecessary else word. I can complete this without else 2. A ternary operator used for returning value. It seems clean, but is it right using ternary operator without returning value? 3. Every time I need to repeat the keyword return. Seems not so clean; Unlike the 1. approach this approach has only 1 branch. And calling b() seems clean, unlike the 1 approach.

I'm stucking with this. I want to write clean code but I don`t know what to choose. Maybe do you know a better way than these 3 approaches? Or can you argue wich approach in what situation to use?

1.

 if ($contidition) {
        a();
    } else {
        b();
    }

2.

$contidition ? a() : b();

3.

if ($contidition) {
    a();
    return;
}

b();

Aucun commentaire:

Enregistrer un commentaire