jeudi 6 juillet 2017

Why is it bad to do things in if conditions

Today I discovered a code block in a code review that seamed weird to me but I could't find an objective reason why this code looks weird to me.

The code looks like this (it's PHP)

    // inside a for loop
    if(
       $thing->isGood() === true &&
       saveThing($thing)
    ) {
      $successfulSavedCount++;
    }
    // for end

    function saveThing($thing) {
      // this method persists $thing to the database
    }

Checking if $this->isGood() === true is fine for me but calling a method that does something feels wrong to me.

A long time ago I read somewhere that doing things in conditions is bad style but I can't remember why it is bad style.

Maybe someone of you can explain why it is bad (or fine)?

Aucun commentaire:

Enregistrer un commentaire