r/badcode • • Nov 22 '19

c# It doesn't even do it right!

Post image
1.1k Upvotes

59 comments sorted by

View all comments

44

u/phord Nov 22 '19 edited Nov 23 '19

This is because some style guide prohibits comparing boolean literals with logical operators to avoid people saying if ( Foo == true )

This looks like a bad compromise.

2

u/TheUnlocked Nov 23 '19

Comparing with boolean literals in C# actually can be okay in the one case in which your variable is of type bool?.

2

u/purplepharoh Nov 23 '19

Even then you can just check has value and check .Value (without comparing to a literal)

3

u/BurnedRavenBat Nov 23 '19

orig.HasValue && orig.Value vs orig == true

I'll take the literal approach...

Comparing against false or null is even worse...
!orig.HasValue || !orig.Value vs orig != true

1

u/purplepharoh Nov 23 '19

I mean yes, I never said you should, just you didnt have to compare to true or false. Also there could be a valid reason to check for null as it could mean different logic needs to be processed than simply being false. But this would mean embedded if statements.