• (cs) in reply to vhawk

    Anonymous:
    At least the 2005 DevStudio has an intelligent intellisense - there you do not need to revert to this extreme to get a list of possible values

    2003 gives intellisense either way

    Drak

  • anon (unregistered) in reply to Tony Marston

    Doesn't c# throw a compiler error if you don't initialise a local variable?

    e.g. the following results in the error "Use of unassigned local variable 's'"

    <FONT size=1><FONT face=Arial><FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> Main(<FONT color=#0000ff>string</FONT>[] args)</FONT></FONT>

    <FONT face=Arial size=1>{</FONT>

    <FONT size=1><FONT face=Arial><FONT color=#0000ff>string</FONT> s;</FONT></FONT>

    <FONT face=Arial size=1>Console.WriteLine(s);</FONT>

    <FONT face=Arial size=1>}</FONT>

  • Sklivvz (unregistered) in reply to BogusDude

    AFAIK, switch statements are not compiled this way in C... they are build by creating an array of jump distances corresponding to the cases and then jumping directly.
    e.g.

    ;switch (a)
    mov eax, [xxx] ; == a
    mov esx, yyy ; == jumptable
    jr [esx+eax]
    jumptable [addr of case 1, addr of case 2...]
    ; case 1
    ; break
    jmp endswitch
    ; case 2
    ; case 3
    ; case 1
    ; endswitch

    So, basically switch cases against byte values are compiled without comparisons... :-D

  • Magnus (unregistered) in reply to Tony Marston

    Probably wrong. My guess would be that some languages (notbaly C, and in some cases Java) have no guarantee of what declared variables have until they are assigned to. If you try to read from them, you may get just about any behaviour from your program.

    On the other hand, we now have compilers that warn of such usage (and in Java it's even an error to read a variable that the compiler cannot prove is definitly assigned to), so these days it's actually better not to initialize them manually unless you have to - it lets the compiler catch potential bugs in your code for you.

  • (cs) in reply to Tony Marston
    Anonymous:

    UncleMidriff:
    As for at the second part of the WTF:  When I was in college taking computer science courses one of my professors told us about a bajillion times each semester to always, always, initialize our variables explicitly.

    I think the reason for this is that some primitive languages required that the contents of each variable be terminated with a NULL character so that the language would know when to stop reading. When assigning a value to a variable that already contained a value only the number of bytes actually supplied would be overwritten, so if the original contents were longer the surplus bytes would still remain. Thus the following two statements:

    var1 = "FIVE"

    var1 = "TEN"

    would result in the contents of var1 being "TENE".

    By setting the variable to '' (which was supposed to fill it with NULLs, not just set the first byte to NULL) you effectively erase the existing contents before inserting a new value.

    Thankfully modern languages are not so stupid, but some college professors are still behind the times and insist on using outdated practices.

    What you say here may be true but I thought the reason is that if you don't assign a value, there's no way to know what the value will be.  The values of the variables don't default to anything.  So if the variable is a pointer, and you try to dereference it, there's no knowing what you might get.  It might even be accessible creating a bizarre result.

    In languages like Java and I assume C#, the compiler ensures that each variable will be set in one way or another before it is used.

  • (cs) in reply to BogusDude

    Anonymous:
    Anonymous:

    But on a more objective note - back in the old days ....  now I really mean long ago when I was young and Intel was still doing 4 bit processors, memory was stil a factor in programming and tight code was cool, switch (select case or what ever - depending on the language) statements resulted in neat jump tables when the compiler started spitting out machine code. Most of the time this generted more efficient code than any  if else elseif   bla bla bla at nausium would generate. My $0.02


    Very nerdy stuff below. Not for the faint of heart !

    It would still be more efficient, in most languages. You have to understand that VB (up until 4 (and again since 7)) was an interpreted language (just like Java). In non-interpreted languages, nice and neat jump tables makes sense, but when the interpreter has to interpret the code, the switch takes longer to execute. You don't really notice any significant (and by significant I mean microsecond) difference.

    Java builds jump tables for switch statements.  That's why you can only switch on integral types in Java.

  • (cs) in reply to KoFFiE
    KoFFiE:
    dubwai:

    ...

    But switch case is not equivalent to if-else in all languages even though it can generally be replaced by them.  Switch case statements are generally used in Java because they are 'faster' than chained if-else (at least theoretically.)

    WTH? Generally used in Java? I mean - if you have it, you use it, why the f*ck shouldn't I use it as frequently in C as I do in Java, or any language that has some sort of switch/select-case construct? And anyway, someone using a case statement in Java only because it's faster has no clue about performance. Java itself isn't that slow u know, the problem is that java makes it easy to write a program in an inefficient way (C# has the same problem), and the early VM's weren't really that fast, but that changed a lot since then. When you want/need optimalisations, look at the source (not literaly [:P]) of the performance problems - which is in general the main program design - not always the implementation. Case statements should be used when comparing one variable value to multiple constant values, that's what it's there for, to make such a thing clear & readable.

    I agree.  I don't think using switch amounts to a hill of beans in terms of peformance.  But it's still on of the reason often sighted for using switch.  Also, if this wasn't the reason for it's inclusion in the language, why not let developers switch on Strings and use non-literal cases?

  • (cs) in reply to V.

    a. This is not C++
    b. Even in C++, assigning one constant and then another one in the next line makes no sense at all

  • (cs) in reply to drjava

    (This was in response to "V." - the forum is totally broken in Firefox...)

  • (cs) in reply to Matthew
    Anonymous:

    Obviously in this particular case, Select Case objLoginValidation.error should have been used.

    Nonetheless, Select Case True can be a useful feature in VB 6. It allows lazy evaluation.

    <font face="Courier New">If x = 1 OR y = 2 OR z = 3 Then</font>

    will evaluate all three conditions in VB 6.

    <font face="Courier New">Select Case True</font>

    <font face="Courier New">   Case x = 1, y = 2, z = 3</font>

    will lazy evaluate (so if x = 1 then y and z won't be checked). Note that the comma is essentially equivalent to OR.

     



    In Visual Studio 2003, for C++ at least, for that same if statement, if x==1 evaluates to true, I don't think it will evaluate for y==2 or z==3.  That could be a result of compiler optimizations.  In Visual Studio 6.0, I think it would try to evaluate the whole if statement.
  • (cs) in reply to Charles Nadolski

    Charles Nadolski:

    In Visual Studio 2003, for C++ at least, for that same if statement, if x==1 evaluates to true, I don't think it will evaluate for y==2 or z==3.  That could be a result of compiler optimizations.  In Visual Studio 6.0, I think it would try to evaluate the whole if statement.

    Nope.  "Short-Curcuit" if() evaluation is required by the Language Standard going all the way back to the original K&R "C Programming Language". 

    It's really necessary in a language where you can write:

    if (pMyStruct != NULL && pMyStruct->MyVal ==5)

     

     

  • (cs) in reply to Charles Nadolski

    I don't think any C or C++ compiler generates code without shortcut optimization. A lot of C programs would otherwise crash, like the following example:

    if (s != NULL && !strcmp(s, "")) {
      ...
    }

    would cause a null pointer exception (memory fault, access violation, whatever ;-)

  • (cs) in reply to dubwai
    dubwai:

    loneprogrammer:
    I think the time on the web server is off.  The timestamps on the posts look wrong.

    Looks like GMT (or the DST version maybe) to me.

    The time appears correct to me.  Note that it it trying to display the timestamp in your local time zone, so you have to go to your account page and set your time zone correctly there.

     

  • (cs) in reply to JamesCurran
    JamesCurran:

    Charles Nadolski:

    In Visual Studio 2003, for C++ at least, for that same if statement, if x==1 evaluates to true, I don't think it will evaluate for y==2 or z==3.  That could be a result of compiler optimizations.  In Visual Studio 6.0, I think it would try to evaluate the whole if statement.

    Nope.  "Short-Curcuit" if() evaluation is required by the Language Standard going all the way back to the original K&R "C Programming Language". 

    It's really necessary in a language where you can write:

    if (pMyStruct != NULL && pMyStruct->MyVal ==5)

     

     



    you were 10 seconds faster, grrrrr ;)
  • (cs) in reply to ammoQ

    ammoQ:

    if (s != NULL && !strcmp(s, "")) {
      ...
    }

    umm... Ya'know that's kind-of a mini-WTF itself, as it's equivalent to the much simplier/faster/readable:

    if (s != NULL && *s =='\0')

     

  • (cs) in reply to JamesCurran
    JamesCurran:
    The time appears correct to me.  Note that it it trying to display the timestamp in your local time zone, so you have to go to your account page and set your time zone correctly there.

    Ah.  A user error problem.

  • (cs) in reply to loneprogrammer

    loneprogrammer:
    JamesCurran:
    The time appears correct to me.  Note that it it trying to display the timestamp in your local time zone, so you have to go to your account page and set your time zone correctly there.

    Ah.  A user error problem.

    No error as far as I am concerned.  GMT is fine with me.

  • A Ruby Programmer (unregistered) in reply to CornedBee

    Nothing is really bad about "select case true", although it's too verbose.

    Ruby has the same construct and it's actually recommended:

    case when x > 3 puts "x > 3" when x < -5 puts "x < -5" else puts "42" end

    It's allowed to short "case true" to just "case" in Ruby to make it more (cond)y.

    In BASIC, however, ELSE IF-constructs look more sane. But that might be just me.

  • (cs) in reply to JamesCurran
    JamesCurran:

    ammoQ:

    if (s != NULL && !strcmp(s, "")) {
      ...
    }

    umm... Ya'know that's kind-of a mini-WTF itself, as it's equivalent to the much simplier/faster/readable:

    if (s != NULL && *s =='\0')

     



    This is at most a nano-WTF, but honestly, in most cases I prefer the version with strcmp; your version is of course faster, but it does so by expressing "how it is done" instead of "what is done". You notice the difference when you imagine an automatic translation of the above code to other programming languages.
  • PeteM (unregistered) in reply to mxksweeb
    mxksweeb:

    Me too.  And I still do it a lot (although not in SQL).  I get that there isn't any real advantage to doing it in many cases, but does it cause any significant negative impact on the code in question?  I'm honestly curious because I have the habit, and I want to know if I'm doing something truly embarrassing--I already generate my share of WTF candidate code as it is.


    dim x as string
    x = ""    'creates memory space for variable
    x= "something"    'destroys orginal memory space and creates new space


    Calling that code once makes no measurable difference, calling it a million times will
  • (cs) in reply to ammoQ

    ammoQ:
    This is at most a nano-WTF, but honestly, in most cases I prefer the version with strcmp; your version is of course faster, but it does so by expressing "how it is done" instead of "what is done". You notice the difference when you imagine an automatic translation of the above code to other programming languages.

    I'd agree --- IF you had written it as "strcmp(s, "") ==0" rather than "!strcmp(s, "")".  My version implies equality by using the equality operator, while yours implies equality by using the NOT operator.   Hence it would be very easy for someone to read your original line ("if (s != NULL && !strcmp(s, ""))") as "If s is not NULL, and is not empty"  (which is wrong), while mine would be read "If s is not NULL, and is empty"

  • (cs) in reply to ammoQ
    ammoQ:
    I don't think any C or C++ compiler generates code without shortcut optimization. A lot of C programs would otherwise crash, like the following example:

    if (s != NULL && !strcmp(s, "")) {
      ...
    }

    would cause a null pointer exception (memory fault, access violation, whatever ;-)


    See that's the thing... I was writing  a couple programs in VC++ 6.0, and I'd write something like this:

    if(i<array.length() && array[i]==somevalue)
    {
    //do stuff
    }

    and the bugger would crash on me when i==array.length because it was out of bounds!
    so I had to do something lame like this:
    if(i<array.length())
        if(array[i]==somevalue)
        {
           //do stuff
        }

    Now of course, the same code compiled in VS 2003 works fine.

    It would be sad if 6.0 was really that bad...
  • (cs) in reply to JamesCurran
    JamesCurran:

    ammoQ:
    This is at most a nano-WTF, but honestly, in most cases I prefer the version with strcmp; your version is of course faster, but it does so by expressing "how it is done" instead of "what is done". You notice the difference when you imagine an automatic translation of the above code to other programming languages.

    I'd agree --- IF you had written it as "strcmp(s, "") ==0" rather than "!strcmp(s, "")".  My version implies equality by using the equality operator, while yours implies equality by using the NOT operator.   Hence it would be very easy for someone to read your original line ("if (s != NULL && !strcmp(s, ""))") as "If s is not NULL, and is not empty"  (which is wrong), while mine would be read "If s is not NULL, and is empty"



    Hmmm... I don't think I've ever written strmp(...)==0, always !strcmp(...). I aggree it's easy to misread in a proportional font (like in this forum), but I don't see the problem in an editor using a monospaced font. In my world, !strcmp is an idiom for "equality". But maybe it's just me.
  • (cs) in reply to Charles Nadolski
    Charles Nadolski:
    ammoQ:
    I don't think any C or C++ compiler generates code without shortcut optimization. A lot of C programs would otherwise crash, like the following example:

    if (s != NULL && !strcmp(s, "")) {
      ...
    }

    would cause a null pointer exception (memory fault, access violation, whatever ;-)


    See that's the thing... I was writing  a couple programs in VC++ 6.0, and I'd write something like this:

    if(i<array.length (="" &="" array[i="=somevalue)&lt;br"> {
    //do stuff
    }

    and the bugger would crash on me when i==array.length because it was out of bounds!
    so I had to do something lame like this:
    if(i<array.length (="">
        if(array[i]==somevalue)
        {
           //do stuff
        }

    Now of course, the same code compiled in VS 2003 works fine.

    It would be sad if 6.0 was really that bad...


    Crash in the debugger? Or really crash when compiled and executed outside VS? Can't believe...
    </array.length></array.length>
  • (cs) in reply to ammoQ
    ammoQ:
    <array.length (="" &="" array[i="=somevalue)&lt;br"><array.length (="">
    Crash in the debugger? Or really crash when compiled and executed outside VS? Can't believe...
    </array.length></array.length>


    VC++ 6.0 DLL being called by a Delphi EXE.  Memory exception in release mode, array out of bounds error in debug mode.  Working on completely different projects with VC++ 2003 often dealing with arrays I never get that problem.
  • (cs) in reply to Jacob

    I invented it to evaluate which radio button is set:

    Private Sub ApplyOptionButtons(aTransName As String)
      Select Case True
      Case Me.optOff.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmOff
      Case Me.optMark.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmMark
      Case Me.optRepair.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmRepair
      Case Me.optUndo.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmUndo
      Case Me.optDelete.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmDelete
      End Select
    End Sub

    <FONT size=1>(BTW this posting interface is horrible. It's taken me 20 minutes to compose this reply, it doesn't offer PRE, the size setting comes out wrong during preview, and the HTML view disappears.)</FONT>

  • (cs) in reply to reinpost
    reinpost:

    I invented it to evaluate which radio button is set:

    Private Sub ApplyOptionButtons(aTransName As String)
      Select Case True
      Case Me.optOff.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmOff
      Case Me.optMark.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmMark
      Case Me.optRepair.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmRepair
      Case Me.optUndo.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmUndo
      Case Me.optDelete.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmDelete
      End Select
    End Sub

    <FONT size=1>(BTW this posting interface is horrible. It's taken me 20 minutes to compose this reply, it doesn't offer PRE, the size setting comes out wrong during preview, and the HTML view disappears.)</FONT>

    I'm not good with the goofball VB syntax. Haven't used it since 'intro to programming.'   What does:

    INI.AnInteger("PreferredRemedy", aTransName) = cmmDelete

    do? Are you assigning a variable to a method here?

  • (cs) in reply to dubwai
    dubwai:
    reinpost:

    I invented it to evaluate which radio button is set:

    Private Sub ApplyOptionButtons(aTransName As String)
      Select Case True
      Case Me.optOff.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmOff
      Case Me.optMark.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmMark
      Case Me.optRepair.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmRepair
      Case Me.optUndo.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmUndo
      Case Me.optDelete.value
        INI.AnInteger("PreferredRemedy", aTransName) = cmmDelete
      End Select
    End Sub

    <font size="1">(BTW this posting interface is horrible. It's taken me 20 minutes to compose this reply, it doesn't offer PRE, the size setting comes out wrong during preview, and the HTML view disappears.)</font>

    I'm not good with the goofball VB syntax. Haven't used it since 'intro to programming.'   What does:

    INI.AnInteger("PreferredRemedy", aTransName) = cmmDelete

    do? Are you assigning a variable to a method here?



    This is why I'm counting my blessings that I'll never have to touch Visual Basic code ever at my job here... *coworker asking to have Excel macros edited* Oops, I spoke too soon.

    Seriously, as a favor though, for the sake of sanity, can you guys please please please please please please stop using switch(true)?  It makes baby Jesus cry and doesn't make your code run any faster.
  • (cs) in reply to Charles Nadolski
    Charles Nadolski:

    VC++ 6.0 DLL being called by a Delphi EXE.  Memory exception in release mode, array out of bounds error in debug mode.  Working on completely different projects with VC++ 2003 often dealing with arrays I never get that problem.


    Aaaaaaahhh, you were using a competing product together with a Microsoft product. Did you try

    #undef __DETECT_AND_CRASH_DELPHI
  • (cs) in reply to ammoQ
    ammoQ:
    Charles Nadolski:

    VC++ 6.0 DLL being called by a Delphi EXE.  Memory exception in release mode, array out of bounds error in debug mode.  Working on completely different projects with VC++ 2003 often dealing with arrays I never get that problem.


    Aaaaaaahhh, you were using a competing product together with a Microsoft product. Did you try

    #undef __DETECT_AND_CRASH_DELPHI


    Heh, or how abount: #pragma never USE_DELPHI

    Well, since you asked, this is a freeware game called Civ Evolution written in the free version of Delphi. It's written in such a way that you can write an AI Module in any language, so long as it is a DLL. Unsurprisingly, most of the AIs are writting in C++. I'm always surprised that people will actually develop in Pascal. From my understanding, I thought it was only intended as a demonstration language.

  • (cs) in reply to Charles Nadolski
    Charles Nadolski:
    I'm always surprised that people will actually develop in Pascal. From my understanding, I thought it was only intended as a demonstration language.

    From my understanding, C++ was meant as a practical joke, but people use it anyways.
  • (cs) in reply to ammoQ
    ammoQ:
    Charles Nadolski:
    I'm always surprised that people will actually develop in Pascal. From my understanding, I thought it was only intended as a demonstration language.

    From my understanding, C++ was meant as a practical joke, but people use it anyways.
    Java only took off because every geek in the world had the hots for Kim Polese. [;)]
  • Ricardo B&#225;nffy (unregistered) in reply to Matthew

    Is it really wise to sacrifice readability to spare a couple CPU cycles shorting conditions in a Windows environment that renders stuff with subpixel anti-aliased scalable fonts?

    I don't know what the COM object does, but it cannot be that expensive (or someone should really take a look inside it)

    Anyway, Select Case x.error should have been used instead.

  • AssertiveOne (unregistered) in reply to BogusDude
    Anonymous:
    It would still be more efficient, in most languages. You have to understand that VB (up until 4 (and again since 7)) was an interpreted language (just like Java). In non-interpreted languages, nice and neat jump tables makes sense, but when the interpreter has to interpret the code, the switch takes longer to execute.


    Why wouldn't the jump table also be faster for the interpreted language?  The Java bytecode has an instruction for a jump table.  I timed a simple example with the -Xint switch to force the JVM to use interpreted bytecode.  There were five packed cases, 1-5, and the version with the switch was 34% faster than the if/else version.  Of course, with sparse cases it might revert to doing the equivalent of a series of if/elses, but so might C.

    When I let the JVM use the JIT, the times were much closer together.  The switch still won, but only by a few percent.
  • HR (unregistered) in reply to V.
    V.:
    Dim strSQL
    strSQL = ""
    strSQL = "SELECT Column FROM TABLE1"

    ...
    strSQL = ""
    strSQL = "EXEC usp_MyStoredProc"

    Actually this is not really a bad practice, maybe a strange habit in this case, but clearing strings is often enough used (eg.: in C++).  If you don't, strange things can happen.


    I wish you'd post your real name, so I could be sure never to hire you.
  • (cs) in reply to HR
    Anonymous:
    V.:
    Dim strSQL
    strSQL = ""
    strSQL = "SELECT Column FROM TABLE1"

    ...
    strSQL = ""
    strSQL = "EXEC usp_MyStoredProc"

    Actually this is not really a bad practice, maybe a strange habit in this case, but clearing strings is often enough used (eg.: in C++).  If you don't, strange things can happen.


    I wish you'd post your real name, so I could be sure never to hire you.


    It's an example Supersitious Code, perhaps as a result of a Voodoo Chicking Coding experience in the recent past.

    Oooh! Maybe if you're using non-ECC ram, one of your variables will magically change! You better try-catch every line of code just in case!

  • (cs) in reply to Charles Nadolski

    That was supposed to be Voodoo Chicken.  WTF is a chicking anyway... there must be some psycholinguistic explanation to my WTF.

  • reinpost (unregistered) in reply to dubwai

    The syntax is irrelevant.

    The point is that I had to do different assignments depending on which radio button is checked, and that the Select True clause provides a nice uniform way to express this.

  • A Truly Selective Programmer (unregistered) in reply to V.
    V.:
    Actually this is not really a bad practice, maybe a strange habit in this case, but clearing strings is often enough used (eg.: in C++).  If you don't, strange things can happen.

    In the event of "strange things", you have not understood constructors, nor have you read Myers.

    Please stop coding in C++ until you have caught up.

Leave a comment on “A Truly Selective Case”

Log In or post as a guest

Replying to comment #:

« Return to Article