coding-style

How do I name the function which only does something if condition is true


According to clean code laws, we want to have functions which do only one thing and are on the same "level of abstraction". But how to name function, whose work is just to check some condition and do the work if condition is true. For example, how could this function be named?

public void HowToNameThis(){
   if(!ComponentIsInstalled()){
      DisableCheckbox();
   }
}

I thought about naming it like DisableCheckboxIfComponentIsNotInstalled, but then the name just repeats the code, which effectively means I have created a function but did not create any abstraction.


Solution

  • CleanCode also suggest that you stay as positive as you can in your code. If you reverse the logic within your method, then, naming becomes easier.

    public void TryEnableComponent() {
        if(ComponentIsInstalled()) {
            EnableCheckbox();
        }
    }