What are the naming conventions of functions that return boolean?

10,084

Solution 1

Short answer to your title question: Different. There are tons of different conventions, perhaps even more conventions than programmers. Some conventions want to have an "is" at the start of every bool returning function's name, others don't. And don't bother questioning/arguing about underscores, CamelCase, lowerCamelCase etc. As long as you stick to the one you picked they are all equally readable.

I think what is important is that the function names express what they are doing. Functions returning bools often appear in context of conditionals or loops. So I think its best to put your intended name in such a context and see if it sounds right. e.g. if (fileExists()) { /* ... */ } sounds good. tryToCloseWindow() sounds like "hey, compiler/program, close that window please". Your comment gives a hint thats not what you want. Maybe use 'userIsClosingWindow()' or something like that. "intend" is not a verb I would use for function names. To intend sth. means you want to do sth., you have it in mind, you are thinking about it, but there is no real action. So if you intend to close a window, just do it, or leave it. No need to tell the compiler "hey, I intend to close that window, in a few lines maybe I will actually DO it...". And you have no means to determine if the user intends to close a window. Unless he plugged his Microsoft Mindreader Device into an USB port and you have access to the API, send me a link please, thanks ;)

Solution 2

in general this should be the naming convention like the one below start with lowercase.

bool tryToCloseWindow();    // check to see if the user try to close the window
bool intendToCloseWindow(); // as above
bool fileExists();          // check to see if the file exists
Share:
10,084
xmllmx
Author by

xmllmx

Updated on June 26, 2022

Comments

  • xmllmx
    xmllmx almost 2 years

    I'm not a native English speaker, so I got confused by the naming convention of functions that return boolean.

    I have known the following function names are conventional:

    bool is_valid();
    bool is_sorted();
    bool is_empty();
    bool has_children();
    bool can_draw();
    

    However, I wonder whether another function names, which begin with verbs other than "is, are, can, has, etc.", are also conventional as follows:

    bool TryToCloseWindow();    // check to see if the user tries to close the window
    bool IntendToCloseWindow(); // as above
    bool FileExists();          // check to see if the file exists
    

    If you are a native English speaker and programmer, do you think the three function names are conventional?

  • xmllmx
    xmllmx over 11 years
    I just want to know whether the last three are conventional.
  • andy
    andy over 11 years
    if you use like this bool IsTryToCloseWindow(); bool IsIntendToCloseWindow(); bool IsFileExists(); then its more conventional.
  • xmllmx
    xmllmx over 11 years
    these names are not grammatically correct.