mardi 23 février 2021

Doing action in if statement. Is this example of clean code? How to do it better?

Recently I've started working in a new place and I noticed there is some code lines I find "strange". I don't like it. Here is what I mean:

private colorizeElement(): boolean {
    //do something like change background of some div
    if (succeed) {
        return true;
    } else {
        return false;
    }
}
//some other part of the code (it can be different function or even different file) where above function is called

if (this.colorizeElement()) {
    //do something else
}

As I said, I don't like it, because in my opinion if statement is not a place for running such functions and checking values at the same time.

I don't think it breaks any clean code rules, but I don't think it's the best way. colorizeElement should set a property like this.isColorized and it should be read in places where colorizeElement is called. What do you think? Is it wrong or is it me?

Aucun commentaire:

Enregistrer un commentaire