Don't use negated method names!
Read the article on my new blog under http://blog.js-development.com/2009/08/dont-use-negated-method-names.html
What does this mean. Don't write something like
public bool IsNotCorrect(...)
{
...
}
but rather writepublic bool IsCorrect(...)
{
...
}
and the caller of the method then does the negation by calling it likeif(!IsCorrect(...)){
//do something
}
orif(IsCorrect(...) == false){
//do something
}
as you prefer.For instance consider the "pseudo-like-code" example
bool IsNotCorrect("today is Monday")
{
}
and in the program I want to check whether today is Monday. So what I would have to write is..
bool isMonday = !IsNotCorrect("today is Monday");
This is some kind of "double negation". The "IsNotCorrect(...)" will return "true" if today is NOT Monday, and false otherwise. Too complicated.I mean this sounds somehow obvious to me and I guess a lot of people are doing this already without even thinking about it, but still I often see code written in this manner wherefore I guess it's worth mentioning.


1 Comments:
This is interesting, and I also found sometimes such code. As you say this is very important for the readability of the code and should be followed by all developers.
Keep on posting design guidelines :-)
Post a Comment