Skip to main content
more colours
Source Link
R. Schmitz
  • 2.6k
  • 1
  • 17
  • 28

This is not a question where you will get a direct answer, because both work. Most people will tell you it's a matter of taste. My personal opinion is: Don't lose sight of the end goal readability.

My Favourite in a Perfect World

I prefer to write code that's "small and easy"; simple statements that are rather short. I prefer to use ! in that code:

if(!IsReadable) SkipWord();

if(!IsReadable) SkipWord();

Reality

At work we have a legacy project which - diplomatically said - is written in a way I personally don't prefer. Using == false makes it more readable. Imagine something like

if(word.IsReadable && sentence.HasSubclause && customer.HasAlreadyChosen == false || word.IsReadable == false && order.IsCompleted)

if(word.IsReadable && sentence.HasSubclause && customer.HasAlreadyChosen == false
 || word.IsReadable == false && order.IsCompleted)

It can go on longer, but I'm gonna stop here. Don't pay much notice to how you could recombine the statements - the point is it's a lot of stuff, a lot of "noise" and it would be easier to accidentally ignore a tiny ! in there.

Best would be to make the code overall more readable, but if a tool (resharper etc) can apply one of the styles throughout the codebase automatically, this is a way to prevent a whole bunch of bugs.

In Summary

In my opinion: Use ! if your code is clean enough that it still stands out.

And, probably obvious, but whichever one you end up using, never do condition === true.

And finally, mostly you'll work in a team and will have to use what the team agrees on, even if it would be an obviously worse decision.

This is not a question where you will get a direct answer, because both work. Most people will tell you it's a matter of taste. My personal opinion is: Don't lose sight of the end goal readability.

My Favourite in a Perfect World

I prefer to write code that's "small and easy"; simple statements that are rather short. I prefer to use ! in that code:

if(!IsReadable) SkipWord();

Reality

At work we have a legacy project which - diplomatically said - is written in a way I personally don't prefer. Using == false makes it more readable. Imagine something like

if(word.IsReadable && sentence.HasSubclause && customer.HasAlreadyChosen == false || word.IsReadable == false && order.IsCompleted)

It can go on longer, but I'm gonna stop here. Don't pay much notice to how you could recombine the statements - the point is it's a lot of stuff, a lot of "noise" and it would be easier to accidentally ignore a tiny ! in there.

Best would be to make the code overall more readable, but if a tool (resharper etc) can apply one of the styles throughout the codebase automatically, this is a way to prevent a whole bunch of bugs.

In Summary

In my opinion: Use ! if your code is clean enough that it still stands out.

And, probably obvious, but whichever one you end up using, never do condition = true.

And finally, mostly you'll work in a team and will have to use what the team agrees on, even if it would be an obviously worse decision.

This is not a question where you will get a direct answer, because both work. Most people will tell you it's a matter of taste. My personal opinion is: Don't lose sight of the end goal readability.

My Favourite in a Perfect World

I prefer to write code that's "small and easy"; simple statements that are rather short. I prefer to use ! in that code:

if(!IsReadable) SkipWord();

Reality

At work we have a legacy project which - diplomatically said - is written in a way I personally don't prefer. Using == false makes it more readable. Imagine something like

if(word.IsReadable && sentence.HasSubclause && customer.HasAlreadyChosen == false
 || word.IsReadable == false && order.IsCompleted)

It can go on longer, but I'm gonna stop here. Don't pay much notice to how you could recombine the statements - the point is it's a lot of stuff, a lot of "noise" and it would be easier to accidentally ignore a tiny ! in there.

Best would be to make the code overall more readable, but if a tool (resharper etc) can apply one of the styles throughout the codebase automatically, this is a way to prevent a whole bunch of bugs.

In Summary

In my opinion: Use ! if your code is clean enough that it still stands out.

And, probably obvious, but whichever one you end up using, never do condition == true.

And finally, mostly you'll work in a team and will have to use what the team agrees on, even if it would be an obviously worse decision.

Source Link
R. Schmitz
  • 2.6k
  • 1
  • 17
  • 28

This is not a question where you will get a direct answer, because both work. Most people will tell you it's a matter of taste. My personal opinion is: Don't lose sight of the end goal readability.

My Favourite in a Perfect World

I prefer to write code that's "small and easy"; simple statements that are rather short. I prefer to use ! in that code:

if(!IsReadable) SkipWord();

Reality

At work we have a legacy project which - diplomatically said - is written in a way I personally don't prefer. Using == false makes it more readable. Imagine something like

if(word.IsReadable && sentence.HasSubclause && customer.HasAlreadyChosen == false || word.IsReadable == false && order.IsCompleted)

It can go on longer, but I'm gonna stop here. Don't pay much notice to how you could recombine the statements - the point is it's a lot of stuff, a lot of "noise" and it would be easier to accidentally ignore a tiny ! in there.

Best would be to make the code overall more readable, but if a tool (resharper etc) can apply one of the styles throughout the codebase automatically, this is a way to prevent a whole bunch of bugs.

In Summary

In my opinion: Use ! if your code is clean enough that it still stands out.

And, probably obvious, but whichever one you end up using, never do condition = true.

And finally, mostly you'll work in a team and will have to use what the team agrees on, even if it would be an obviously worse decision.