Is it really true? Is it not false?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 4815 Views - Last Post: 30 June 2012 - 05:46 AM Rate Topic: -----

#1 investor5555  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 1
  • Joined: 28-June 09

Is it really true? Is it not false?

Posted 15 February 2012 - 06:54 PM

*
POPULAR

I have seen code like this over and over again:

if (someVariable == true)
{ ... }

if (someVariable == false) 
{ ... }

if (someVariable != false) 
{ ... }



Isn't this sufficient:

if (someVariable)
{ ... }

if (!someVariable)
{ ... }



What makes it worse is when I see things like

if (notVisible != false) 
{ ... }



How negative do you want to be?

Is This A Good Question/Topic? 6
  • +

Replies To: Is it really true? Is it not false?

#2 stackoverflow  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 164
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Re: Is it really true? Is it not false?

Posted 15 February 2012 - 08:06 PM

Um, if you're going to do that then...

if( someVar ){

} else { 
// !someVar

}


Not to mention, using an else is faster because the 'not' conditions are not being re-evaluated.

This post has been edited by stackoverflow: 15 February 2012 - 08:06 PM

Was This Post Helpful? 2
  • +
  • -

#3 Zachari  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 22
  • Joined: 28-February 11

Re: Is it really true? Is it not false?

Posted 16 February 2012 - 01:14 AM

Surprisingly, a lot of people don't learn that you can do that. A lot of programmers out there are just trying to get a program to work, and they forget about optimization and sensible code.
Was This Post Helpful? 1
  • +
  • -

#4 Gamma Ray  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 24-September 10

Re: Is it really true? Is it not false?

Posted 16 February 2012 - 05:37 AM

There, there... There, there... :2guns: :angel:
Was This Post Helpful? 0
  • +
  • -

#5 aliancemd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-December 10

Re: Is it really true? Is it not false?

Posted 16 February 2012 - 07:23 AM

View Postinvestor5555, on 15 February 2012 - 06:54 PM, said:

Isn't this sufficient:

if (someVariable)
{ ... }

if (!someVariable)
{ ... }



Why don't you use else?
if ( someVariable ) {
...
} else {
...
}



And btw, switch is faster than if/else because it creates a special table of options instead of checking every line.
switch ( someVariable ) {
case 0: ... break;
default: ...
}


Was This Post Helpful? 0
  • +
  • -

#6 GenHornet18  Icon User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • Posts: 629
  • Joined: 19-October 09

Re: Is it really true? Is it not false?

Posted 17 February 2012 - 12:31 AM

I argue daily the benefits of performance over code clarity and it always comes out to a matter of opinion.

@aliancemd I would argue that the compiled (OP posted in C++) code determines the execution speed (which is compiler/arch independent). On average (dependent on compiler, etc.) the usage of a switch statement to evaluate boolean operations would likely be slower (due to the overhead of setting up a jump-table) and it would therefore be more prudent to use a simple if-else statement.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Is it really true? Is it not false?

Posted 17 February 2012 - 12:46 AM

Not if the jump table is created at compile which .net does.
Was This Post Helpful? 0
  • +
  • -

#8 aliancemd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-December 10

Re: Is it really true? Is it not false?

Posted 17 February 2012 - 01:19 AM

View PostGenHornet18, on 17 February 2012 - 12:31 AM, said:

I argue daily the benefits of performance over code clarity and it always comes out to a matter of opinion.

@aliancemd I would argue that the compiled (OP posted in C++) code determines the execution speed (which is compiler/arch independent). On average (dependent on compiler, etc.) the usage of a switch statement to evaluate boolean operations would likely be slower (due to the overhead of setting up a jump-table) and it would therefore be more prudent to use a simple if-else statement.


I don't recommend using switch for boolean, I actually don't use switch for boolean, I just tried to show that switch is there for if/else too and it is better.
Was This Post Helpful? 0
  • +
  • -

#9 djkitt  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 189
  • Joined: 22-May 08

Re: Is it really true? Is it not false?

Posted 20 February 2012 - 03:16 PM

This is the kind of code I encountered when I started in my current position:

     
        If a = 0 Then b = 1
        If a = 0 Then c = 1
        If a = 1 Then b = 2
        If a = 1 Then c = 2
        If a <> 0 And a <> 1 Then b = 3
        If a <> 0 And a <> 1 Then c = 3
  


This kind of code was everywhere and was much lengthier.

The young man that preceded me had just graduated from college, but c'mon...
Was This Post Helpful? 1
  • +
  • -

#10 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 391
  • Joined: 04-January 11

Re: Is it really true? Is it not false?

Posted 21 February 2012 - 08:18 PM

I just graduated from college too but I never coded like that.

We had programming subjects in elementary and high school and never in my elementary or high school days have I coded like that.
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3517
  • View blog
  • Posts: 10,150
  • Joined: 08-June 10

Re: Is it really true? Is it not false?

Posted 24 February 2012 - 12:15 AM

I never graduated in CS, but even I never wrote such code.
Was This Post Helpful? 1
  • +
  • -

#12 BBuschRN  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 83
  • Joined: 28-February 12

Re: Is it really true? Is it not false?

Posted 03 March 2012 - 12:44 PM

View Postdjkitt, on 20 February 2012 - 02:16 PM, said:

This is the kind of code I encountered when I started in my current position:

     
        If a = 0 Then b = 1
        If a = 0 Then c = 1
        If a = 1 Then b = 2
        If a = 1 Then c = 2
        If a <> 0 And a <> 1 Then b = 3
        If a <> 0 And a <> 1 Then c = 3
  


This kind of code was everywhere and was much lengthier.

The young man that preceded me had just graduated from college, but c'mon...

OMG that's hilarious - you should put that in the Nightmare.In.Code forum!! I just started and even that hurts my eyes!!
Was This Post Helpful? 1
  • +
  • -

#13 BBuschRN  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 83
  • Joined: 28-February 12

Re: Is it really true? Is it not false?

Posted 03 March 2012 - 01:58 PM

Sorry but how do I edit? I just realized that we are in said forum... (it's been a long day)
Was This Post Helpful? 1
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Is it really true? Is it not false?

Posted 03 March 2012 - 04:01 PM

Too late! It's already on the internet, where it'll live on as troll bait.
Was This Post Helpful? 1
  • +
  • -

#15 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3517
  • View blog
  • Posts: 10,150
  • Joined: 08-June 10

Re: Is it really true? Is it not false?

Posted 05 March 2012 - 03:09 AM

View Postinvestor5555, on 16 February 2012 - 02:54 AM, said:

What makes it worse is when I see things like

if (notVisible != false) 
{ ... }



How negative do you want to be?

sometimes that is necessary when you evaluate function return values in loosely-typed languages. though in that case you'd use type-safe comparison (aka. !== or ===)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2