VB.NET and Short-Circuiting

Started by Thorin, September 23, 2008, 04:05:39 PM

Previous topic - Next topic

Mr. Analog

Quote from: Darren Dirt on September 26, 2008, 02:24:09 PM
Quote from: Thorin on September 26, 2008, 01:06:36 AM
Quote from: Tom on September 25, 2008, 07:36:11 PM
QuoteBecause this is happening as part of the test portion of an if statement, the test becomes whether the assignment worked correctly or not independent of the data or lack of data in x.
In C/C++ and Perl, the foo=bar statement resolves to the final value of foo, since = doesn't return a boolean if it succeeded or failed. So in those languages you can indeed test for the final value.

Well, based on the coding convention used I'd say the example was intended to be seen as Java code.

However, I remember writing C++ code way back in CST, and if you only put one equals sign the return value from the function you called was assigned to the variable and you *always* hit the true branch.  The only way to get the false branch to run was to cause the called function to return something that the variable couldn't hold without an explicit cast.

^ Winner of the "Clear Post Of The Day" award.

I think now we all agree on what all of us were trying to say ;D  Thanks!

Sick thing is that it all makes sense to most of use here...

You know what my other favourite line of code to see nested three or four times?


//Seriously I will beat you if you do this all the time
dont = (x != y) ? everdo : this.kindaCode;

//NOOOOOOOOOOOOO!!!!
blah = (x == ((a > b) ? true : false) ? y : z;
By Grabthar's Hammer

Thorin

Ahh, ternary operators.  Yes, always a good idea to write really cryptic code with those!  I love when they're nested four or five deep, and indentation isn't properly followed so the poor maintenance programmer has to re-arrange the code before it makes sense.
Prayin' for a 20!

gcc thorin.c -pedantic -o Thorin
compile successful

Darren Dirt

Quote from: Thorin on September 26, 2008, 03:48:42 PM
Ahh, ternary operators.  Yes, always a good idea to write really cryptic code with those!  I love when they're nested four or five deep, and indentation isn't properly followed so the poor maintenance programmer has to re-arrange the code before it makes sense.

I luvs teh ternary -- especially within bookmarklets and the like... All hail the ternary! Heathen unbelievers of the ternary, enjoy your lengthy scrolling code, I live by teh ternary.
_____________________

Strive for progress. Not perfection.
_____________________

Tom

Know what I like even more than the ternary operator?

computed gotos. Its GCC only so don't expect it to work on MSVC or anything:


#define NEXT ({ int *ptr = NULL; int next_op = fetch_op(); ptr = table + next_op; goto **ptr; })
void *table = { &&LABEL1, && LABEL2, ... };
LABEL1:
  do_something();
  NEXT;

LABEL2:
  do_somethingelse();
  NEXT;


And why would I do something so evil? Its about ten times faster than this:
do {
   switch(op) {
      case LABEL1:
         do_something();
         break;
       case LABEL2:
          do_somethingelse();
          break;
   }
} while(!end);


:)
<Zapata Prime> I smell Stanley... And he smells good!!!

Mr. Analog

Quote from: Tom on September 26, 2008, 05:01:20 PM
Know what I like even more than the ternary operator?

computed gotos. Its GCC only so don't expect it to work on MSVC or anything:


#define NEXT ({ int *ptr = NULL; int next_op = fetch_op(); ptr = table + next_op; goto **ptr; })
void *table = { &&LABEL1, && LABEL2, ... };
LABEL1:
  do_something();
  NEXT;

LABEL2:
  do_somethingelse();
  NEXT;


And why would I do something so evil? Its about ten times faster than this:
do {
   switch(op) {
      case LABEL1:
         do_something();
         break;
       case LABEL2:
          do_somethingelse();
          break;
   }
} while(!end);


:)

Not to single you out, but I just noticed a HUGE no-no which would net you a ding in a code review (if I were doing it).

Where's your default on your switch? I see a lot of programmers make this mistake because "op" will always be LABEL1 or LABEL2... y'know, except when we add LABEL3 after about a year.

Draconian maybe, but realistic... ;)

I should post my (C#) Coding Standards doc its got a lot of goodies in it.

For example: In the .NET Framework why would you want to make a private constructor on an object that has no member data?

I'm thinking most of you get it, but I'm surprised how many people came back and asked me about it when I wrote my draft doc.
By Grabthar's Hammer

Tom

Quote from: Mr. Analog on April 21, 2009, 10:33:53 PM
Quote from: Tom on September 26, 2008, 05:01:20 PM
Know what I like even more than the ternary operator?

computed gotos. Its GCC only so don't expect it to work on MSVC or anything:


#define NEXT ({ int *ptr = NULL; int next_op = fetch_op(); ptr = table + next_op; goto **ptr; })
void *table = { &&LABEL1, && LABEL2, ... };
LABEL1:
  do_something();
  NEXT;

LABEL2:
  do_somethingelse();
  NEXT;


And why would I do something so evil? Its about ten times faster than this:
do {
   switch(op) {
      case LABEL1:
         do_something();
         break;
       case LABEL2:
          do_somethingelse();
          break;
   }
} while(!end);


:)

Not to single you out, but I just noticed a HUGE no-no which would net you a ding in a code review (if I were doing it).

Where's your default on your switch? I see a lot of programmers make this mistake because "op" will always be LABEL1 or LABEL2... y'know, except when we add LABEL3 after about a year.

Draconian maybe, but realistic... ;)

I should post my (C#) Coding Standards doc its got a lot of goodies in it.

For example: In the .NET Framework why would you want to make a private constructor on an object that has no member data?

I'm thinking most of you get it, but I'm surprised how many people came back and asked me about it when I wrote my draft doc.
It was an example ;) IIRC my real code has a default, which jumps to error handling code. But the loop-switch exec loop isn't even enabled by default in that project. Its too slow.

The fun part about the computed goto is that if the opcode is horribly wrong, bad things will happen ;D and not just "falling out of the switch" bad, but "app crashing" bad. So its something you have to be very careful with. If it were run on win9x, and the opcode was wrong, it could crash the entire OS by jumping to some random memory address in the system and doing all kinds of horrible things.
<Zapata Prime> I smell Stanley... And he smells good!!!

Mr. Analog

Quote from: Tom on April 22, 2009, 10:10:59 PM
Quote from: Mr. Analog on April 21, 2009, 10:33:53 PM
Quote from: Tom on September 26, 2008, 05:01:20 PM
Know what I like even more than the ternary operator?

computed gotos. Its GCC only so don't expect it to work on MSVC or anything:


#define NEXT ({ int *ptr = NULL; int next_op = fetch_op(); ptr = table + next_op; goto **ptr; })
void *table = { &&LABEL1, && LABEL2, ... };
LABEL1:
  do_something();
  NEXT;

LABEL2:
  do_somethingelse();
  NEXT;


And why would I do something so evil? Its about ten times faster than this:
do {
   switch(op) {
      case LABEL1:
         do_something();
         break;
       case LABEL2:
          do_somethingelse();
          break;
   }
} while(!end);


:)

Not to single you out, but I just noticed a HUGE no-no which would net you a ding in a code review (if I were doing it).

Where's your default on your switch? I see a lot of programmers make this mistake because "op" will always be LABEL1 or LABEL2... y'know, except when we add LABEL3 after about a year.

Draconian maybe, but realistic... ;)

I should post my (C#) Coding Standards doc its got a lot of goodies in it.

For example: In the .NET Framework why would you want to make a private constructor on an object that has no member data?

I'm thinking most of you get it, but I'm surprised how many people came back and asked me about it when I wrote my draft doc.
It was an example ;) IIRC my real code has a default, which jumps to error handling code. But the loop-switch exec loop isn't even enabled by default in that project. Its too slow.

The fun part about the computed goto is that if the opcode is horribly wrong, bad things will happen ;D and not just "falling out of the switch" bad, but "app crashing" bad. So its something you have to be very careful with. If it were run on win9x, and the opcode was wrong, it could crash the entire OS by jumping to some random memory address in the system and doing all kinds of horrible things.

Well, that's good to hear, some people honestly don't "get it" and will blindly assume that because something is a struct or an enum that it is written in stone and incorruptible.
By Grabthar's Hammer