Contents
What to name a method that returns a Boolean?
The usual convention to name methods that return boolean is to prefix verbs such as ‘is’ or ‘has’ to the predicate as a question, or use the predicate as an assertion. For example, to check if a user is active, you would say user. isActive() or to check if the user exists, you would say user. exists().
Should Booleans start with is?
Boolean variables should be prefixed with ‘is’ This is the naming convention for boolean methods and variables used by Sun for the Java core packages.
When to return a reference to a variable?
Never return a reference to a local variable or some such, because it won’t be there to be referenced. You can return a reference to something independent of the function, which you don’t expect the calling function to take the responsibility for deleting. This is the case for the typical operator [] function.
What are the best practices for variable and method naming?
Best Practices for Variable and Method Naming. Join the DZone community and get the full member experience. Use short enough and long enough variable names in each scope of code. Generally length may be 1 char for loop counters, 1 word for condition/loop variables, 1-2 words for methods, 2-3 words for classes, 3-4 words for globals.
What’s the best way to name a variable?
Use short enough and long enough variable names in each scope of code. Generally length may be 1 char for loop counters, 1 word for condition/loop variables, 1-2 words for methods, 2-3 words for classes, 3-4 words for globals. Use specific names for variables, for example “value”, “equals”, “data”, are not valid names for any case.
When to return a pointer or a value?
If you are creating something, you should return either a value or a pointer (regular or smart). You can return a value freely, since it’s going into a variable or expression in the calling function. Never return a pointer to a local variable, since it will go away.