Dear reader of Juri's TechBlog,
I moved my blog to a new domain and a new hosting solution as well. I'm now blogging on juristr.com.

Don't use negated method names!

What does this mean. Don't write something like

public bool IsNotCorrect(...)
{
   ...
}
but rather write
public bool IsCorrect(...)
{
   ...
}
and the caller of the method then does the negation by calling it like
if(!IsCorrect(...)){
  //do something
}
or
if(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.

Posts you might also be interested in..

Credits: Hoctro | Jack Book

1 Comments:

Manfred said...

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