简体   繁体   中英

Function and variable naming conventions for boolean methods

I've got a question that it's somewhat tricky for me because I spend quite some time figuring out useful names for both variables and method names.

In this case, I have a method that is called isTestCompleted , but I cannot use the same name for the variable because of naming conflicts.

What approaches do you usually use when faced with similar situations when you want to just do:

const isTestCompleted = isTestCompleted(test);

I would probably name the function checkTestCompleted since the function checks if some given test has been completed instead of referring to some singular shared test state. As for the variable name, it may change depending on the context it is used in. If it is obvious it refers to the given test, I might shorten it to completed . Or maybe give it a longer name which better describes what this test is for like fooTestCompleted . It all comes down to how I am feeling when writing it. There is no one single correct answer.

Just choose whatever you feel helps you understand the problem best. Don't be afraid to use longer names if it helps you remember what a piece of code is doing.

At school we ar tought to us testCompleted for variable and isTestCompleted for function.
For the variable you say "it completed". For the function you can see it as a question "is it completed?".

For naming functions you usualy start with a verb: Is, Get, Save, Update, Move,...
And for a boolean we had to start the funciont with "is" this way you can easy see it returns a boolean and nothing els.

I use this_case for variables, thisCase for functions, ThisCase for classes and THIS_CASE for constants

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