How can I name my class, functions, member variables and static variables?

Some may feel that this question is subjective. But I feel like this is one of the most important things to say to a programmer.

This is a good function name for testing null values.

1. checkNull()
2. notNull()
3. isNull()

      

What if I write

checkIfNull() 

      

I don't know how many people share the same feeling that I do, I spent more time thinking about good names for my functions than writing.

How do people think of good names? Can naming be consistent across languages ​​(mainly C ++ and Java)

Update: Since I have been visiting the number of updates so far, most people prefer isNull (). How do you decide isNull () is the perfect name.

checkNotNull() // throw exception if Null

      

Is that a good name? Does everyone depend on their intuition to determine the name?

The question of choosing the perfect name !!!

+2


source to share


13 replies


I found this article . Feeling like share with you guys!



+1


source


isNull might be a bad example because:

Object foo = null;
if (foo.isNull()) { // Causes a NullPointerException in Java. }

      

Otherwise, you have:



Object foo = null; 
if (UtilityClass.isNull(foo) { }

      

Which seems harder and less clear than just doing:

Object foo = null;
if (foo == null) { }

      

+3


source


Like others have suggested, I prefer isNull () (or IsNull (), depending on your language / coding conventions).

Why? Also, this is a generally accepted convention, it sounds good when you read the code:

if (isNull())
// or
if (foo.isInitialized())

      

etc. Almost natural English ... :-) Compare with the alternatives!
As iWerner, I would avoid the negative form for generating identifier (variable, method) names.
Another common convention is to run method / function names with a verb. Now Sun did not adhere to this convention in the early days of Java (hence the length () and size () methods, for example), but it even ignores some of those old names in favor of the verb rule.

+3


source


If a function throws an exception, if it is null, it should be called ThrowIfNull

to make it clear that it will be thrown to you.

+3


source


It is currently highly recommended to use the javaBeans convention:

isNull() //if the return type is a primitive
getNull() //if the return type is an object (Like Boolean in java)

      

For non boolean access members, you must use get.
For static variable members, use the camel-case style: "myVar".
Use the capitalized camel style for the class name: "MyClass".
And for constant members, use an uppercase letter with an underscore as separator: "MY_CONSTANT".

+2


source


IsNull () is a good choice, but it should also return bool.

So, you can check its value if statment doesn't get NullReference exception.

+2


source


The answer depends on what your method returns.
If it returns a bool indicating whether the object is null, I would call it IsNull (Thing thing) because that's the least ambiguous formulation - what the method does and what it returns is immediately obvious. If the method is invalid but throws if the object is null then I would call it GuardAgainstNull () or something along those lines. IMO, CheckNull () is somewhat ambiguous - you don't know by looking at the method if it should return bool or throw, or whatever bool indicates exactly.

+2


source


I prefer IsNull

.

For good naming conventions, check out the standard libraries (other than PHP). You must follow the style used by the standard libraries in each language.

For C #, check out the Framework Design Guide .

+1


source


personally, I would use

   IsNull()

      

+1


source


If you are doing a lot of null checking in your code, I think there are several methods, i.e .:

IsNull()

IsNotNull()

      

will result in the most readable code in the end.

I know! IsNull () is a standard idiom in curly binding languages, but I think it's much less clear than IsNotNull.

It's too easy to miss this single "!" character, especially if it is buried in a more complex expression.

+1


source


It can vary depending on the language you use - and you've tagged a couple for this question. It is important to adhere to the standards of the language / library you are coding with. Yes, naming conventions are very important! [There is even a wikipedia entry: http://en.wikipedia.org/wiki/Naming_conventions_%28programming%29 ]

For .Net, I found this "cheat sheet" in naming conventions:

http://www.irritatedvowel.com/Programming/Standards.aspx

For your example in C #, I recommend: IsNull()

0


source


If your company doesn't define naming conventions in their coding standards, I suggest you add the time.

Our Java coding standards are based on the official Java Coding Standards , which I believe defines type names isNull()

.

From your example, notNull()

is bad because you can wrap expressions such as if(!notNull())

or the like.

0


source


I would use IsNull (); there is a precedence in .Net which has a static method IsNullOrEmpty () for type String. "Is" is my preferred prefix for methods that return bool. I wouldn't have a negative "notNull" method because it leads to double negatives too easily. Use a positive negation operator instead, such as IsNull ().

However, a method that only checks for a null value can overcomplicate things; What is wrong with

x == null

Which I find more readable than

IsNull (x)

Most developers who see IsNull (x) wonder if there is any fancy null check in the IsNull method; if not, then "x == null" is probably better.

0


source







All Articles