18184

Is there a right or wrong practice when using IF statements : if(foo) or if(foo == true)?

Question:

Throughout my (short) career so far in programming (merely a student working on internship) I have noticed that when it comes to IF statements, there is two different ways of doing it.

If we take foo as a boolean value:

if(foo) { //do stuff }

This is my preferred way of doing things when dealing with IF statements, if I'm looking for false I use:

if(!foo) { //do more stuff }

However, when some people see this they raise an eyebrow, suggesting that I may be stuck in a bad habit. But I wanted to know, is there any difference between this way or the "typical" way?

if(foo == true) { //do a bit more stuff }

Am I falling into a common trap for new programmers? Or is there no difference (at least a noticeable one)

Answer1:

I never write == true or == false. It goes against the point of an if-sentence, in my opinion.

an if is basicly: If a Boolean expression is true, do something. a Boolean is a boolean expression in and of itself, so why wrap it?

So in my opinion, the ones using == true are the ones with a bad habit. Becuase they display ignorance of how the language works.

think of these "allowed" ways to write if (Foo) and if (!Foo):

if (Foo == true) //If Foo is the same as true if (Foo != true //If Foo is not the same as true if (Foo != false) //if Foo is not the same as false if (Foo == false) //If Foo is the same as false if (Foo) //If Foo if (!Foo) //If not Foo

using == and != with booleans actually introduce new ways to make mistakes both when programming and reading code.

Boolean and !Boolean is hard to misread.

Answer2:

I guess doing it your way is recommended in most of the languages

if(foo) { //do stuff }

For instance <strong>Python <a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow">PEP8</a> says</strong>

Don't compare boolean values to True or False using ==.

Yes: if greeting: No: if greeting == True: Worse: if greeting is True:

Another example from <strong><a href="http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-142311.html#449" rel="nofollow">Code Conventions for the Java Programming Language 7</a></strong>

The if-else class of statements should have the following form:

if (condition) { statements; }

Just the small tip: Speaking about condition checks, here's the good tip I've heard few weeks ago If you compare for instance in C like

if(variable == "value") ....

you can get to the problems that if you write by accident if (variable = "value") ... compiler will not throw an error, so some people use the convention of if ("value" == variable) ..., then if you write by accident = instead of ==, compiler will throw an error

Answer3:

Most coding conventions tell you that boolean checks are tested as

if(someBoolean) {

or

if(!someBoolean) {

This simply improves readability. If you are unsure, check out some code conventions, here are the Java ones for you: <a href="http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html" rel="nofollow">http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html</a>

Answer4:

Checking if(foo == true) means

<blockquote>

check if foo is equal to true is equal to true

</blockquote>

Which is just an overhead according to me.

Recommend

  • Saving VirtualStringTree Node Data
  • what is good way to do merge the code in below scenarion (Git,Intellij,Stash)?
  • Can I generate a JSON file with javascript?
  • Value assigned is not used in any execution path - C# [duplicate]
  • Where to put code in (primarily) windowless WPF app?
  • scope of the mouse adapter
  • Running java programs in one runtime instance
  • How to fallback to entirely different index page if user has javascript disable?
  • Best HTML5 structure for a layout where the title/header is outside the article tag
  • How to make SASS put relative paths in its output
  • How to pass a value from ASP.NET MVC controller to ASP.NET webforms control inside MVC View?
  • NUnit 3.0 TestCase const custom object arguments
  • Plotting line graph with factors in R
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • Android activity accessing service's static reference before the service is ready
  • How can I sort a a table with VBA with given text condition?
  • How to match http request and response using Jersey ContainerRequestFilter and ContainerResponseFilt
  • Is there a way to do normal logging with EureakLog?
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • How to set ini file attributes during an Inno install
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Switching to Release Build causes runtime error in Web Reference
  • FFmpeg Conversion Error
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Array.prototype.includes - not transformed with babel
  • PHP - How to update data to MySQL when click a radio button
  • How to add date and time under each post in guestbook in google app engine
  • How to recover from a Spring Social ExpiredAuthorizationException
  • ILMerge & Keep Assembly Name
  • ActionScript 2 vs ActionScript 3 performance
  • Large data - storage and query
  • To display the title for the current loaction in map in iphone
  • WOWZA + RTMP + HTML5 Playback?
  • Traverse Array and Display in markup
  • How to set the response of a form post action to a iframe source?
  • coudnt use logback because of log4j
  • Bitwise OR returns boolean when one of operands is nil