开发者

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

开发者 https://www.devze.com 2022-12-08 04:53 出处:网络
Some may feel this question is subjective. But, I feel this is among the most important things to be told to a programmer.

Some may feel this question is subjective. But, I feel this is among the most important things to be told to a programmer.

Is this a good function name to check for null values.

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

What开发者_运维技巧 if I write

checkIfNull() 

I do not know how many people share the same feeling as I do, I have spent more time in thinking good names for my functions than writing one.

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

Update: As I go by the number of updates till now, Most people prefer isNull(). How do you decide upon this that isNull() is the perfect name.

checkNotNull() // throw exception if Null

Is this a good name? Does everyone depend upon their intuition for deciding a name?

The question is about choosing a perfect name!!!


isNull might be a bad example, because:

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

Otherwise, you've got:

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

Which seems harder and less clear than just doing:

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


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

Why? Beside it is a widely accepted convention, it sounds nice when you read the code:

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

and so on. Almost natural English... :-) Compare to the alternatives!
Like iWerner, I would avoid negative form for making identifiers (variables, methods) names.
Another common convention is to start method/function names with a verb. Now, Sun did not follow this convention in the early days of Java (hence the length() and size() methods, for example) but it even deprecates some of these old names in favor of the verb rule.


If the function throws an exception if it's null, it should be called ThrowIfNull to make it clear that it will throw for you.


IsNull() is a good choice, But additionally it should return a bool.

So that you can check its value in if statment without getting any NullReference exception.


Nowadays it is 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 types access members, you should use get.
For static variable members use the camel case style: "myVar".
For class name use camel case style with capitalized first letter: "MyClass".
And for constant members use uppercase letter with underscore as separator: "MY_CONSTANT".


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


I prefer IsNull.

To learn good naming style, study the standard libraries (except in PHP). You should follow the style used by the standard libraries in each language.

For C#, study the Framework Design Guidelines.


personally, I would use

   IsNull()


I found this article. Felt like sharing with you guys!


If you're doing a lot of null checking in your code, I think having a pair of methods, i.e.:

IsNull()

IsNotNull()

will lead to the most readable code in the long run.

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

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


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

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

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

For your example in C# I'd reccommend : IsNull()


If your company does not specify naming conventions in its coding standards I suggest it's time you add them.

Our company's Java coding standards are basedon the official Java Coding Standards which, I believe, specify names like isNull().

From your example, the notNull() is bad, because you may end up with statements like if(!notNull()) or the like.


I would use IsNull(); there is a precedence in .Net which has a static IsNullOrEmpty() method for the String type. "Is" is my preferred prefix for methods that return a bool. I would not have a negative method "notNull", because this too easily results in double negatives. Instead use the negation operation on a positive method, e.g., !IsNull().

However, a method that only checks for a null value may be overly complicating things; what is wrong with

x == null

Which I think is more readable than

IsNull(x)

Most developers seeing IsNull(x) would wonder if there is some fancy null checking in the IsNull method; if there isn't then "x == null" is probably better.

0

精彩评论

暂无评论...
验证码 换一张
取 消