Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

Don't use negated method names!

1 min read

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.
Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus