简体   繁体   中英

How to name a function which decides if an action should be taken

What is an appropriate/conventional way to name a function which takes a value, evaluates it against some criteria, and makes a decision if certain action should be taken or not?

For example, a function that decides if a valve should be opened to water a plant:

def FUNCTION(soilMoisture: float) -> bool:
    threshold = 0.1
    return soilMoisture < threshold

A name like openValve() would be confusing, because this function does not open the valve, it only makes a decision.

You shouldn't name the function after the action which you intend to perform but after the condition. Then you can use the function in many contexts. You can start with thinking about an adjective which describes the condition and then add words in front of it to make a sensible function name. In this case there is something that needs to be done if the soil is too dry . Hence, I would recommend something like SoilTooDry . Then your code will read

if SoilTooDry(soilMoisture: float):
   OpenValve()

However, in this situation I think introducing a function is unnecessary, so I would just write

if soilMoisture < threshold:
   OpenValve()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM