• (cs)
    Jakeypoo:
    "smorgasbord"

    WTF! It's called "Smörgåsbord".
    Don't you speak swedish. :oP

  • Mike5 (unregistered) in reply to Tom
    Anonymous:
    Well, if LoadPageData had some sort of way to tell without being passed any information what the page state was, that _might_ make sense...


    In what way exactly? If LoadPageData has some way of telling what the page state is, what do you need select statement for?

    Cool, my captcha was zork.. ZOOOOOOOOOOOOORK!!!!!!
  • (cs)

    It looks like that code was written by a programmer who gets paid for the number of lines of code (s)he produces...

  • (cs) in reply to chb
    chb:
    Anonymous:
    Well, if LoadPageData had some sort of way to tell without being passed any information what the page state was, that _might_ make sense...or maybe the programmer was being paid by the line.

    Cool!
    Endless Switch-Statements make you rich!

    int s = 0;
    switch(s) {
       0:  //Load Page Data
       
    LoadPageData();
           break;
        1: // Make Money
           dontBotherBreaking();
        2: // Make Money
        ...
        default: // You should allways have a default
                   // Make Money!
    }

    I'd love to live in that world  <:o)




    I think you meant:

    int s = 0;

    switch(s) {

       0:  //Load Page Data
       
    LoadPageData();
           break;
        1: // ????
           dontBotherBreaking();
        2: // ????
           Profit!
    }

        dZ.

  • (cs) in reply to Bytecodes is not codebytes
    Bytecodes is not codebytes:

    Volmarias:
    To be honest, this isn't all THAT bad. In both cases, one possible explaination is that at one time, different states did different things. However, they were changed such that they would all do the same thing, with the expectation that they would do different things at some time soon in the future, something that never came to pass. The first case actually seems more like someone just doesn't understand how switch works, and performed a monkey-see-monkey-do to make the code work.

     

    Spot on! I think both cases is something like you suggest. There is no WTF in this code, the big WTF here is how little imagination people here have (except Volmaris), and obviously can't see the forest behind all the tree's.



    What do you mean??  I can see the forest.  Of course I can see the forest!!  Its just all this damn trees that keep getting in the way!

        dZ.

  • (cs) in reply to dubwai
    dubwai:


    Yaaaaaay! I was almost done with the first page and wondered when the "repost" jokes were going to kick in.

        dZ.

  • (cs) in reply to OMG
    OMG:
    DOH! - Make that:
    switch (YouDontSucceed)
    {
       case At.First:
        Try();
        break;
       case At.Second:
        Try();
        break;
       case At.Third:
        Try();
        break;
      default:
        Try();
    }


    A little bit of copy+pasting trouble there, uh? :)

        dZ.
  • (cs) in reply to Generic
    Generic:
    Jakeypoo:
    "smorgasbord"

    WTF! It's called "Smörgåsbord".
    Don't you speak swedish. :oP



    This reminds me of an Asterix comic, where the vikings' dialogs were shown  normally but with umlauts on their 'u's and slashes through their 'o's to show that they were speaking a different language.

        dZ.

  • (cs) in reply to Sean Connery
    Anonymous:
    Volmarias:
    To be honest, this isn't all THAT bad. In both cases, one possible explaination is that at one time, different states did different things. However, they were changed such that they would all do the same thing, with the expectation that they would do different things at some time soon in the future, something that never came to pass. The first case actually seems more like someone just doesn't understand how switch works, and performed a monkey-see-monkey-do to make the code work.


    If the language is C#, the WTF is that they used a swithc instead of an if. C# doesnt allow fall-through like C++.

    so C++ lets you do case A: case B: case C: // more readable than c# version
    but C# forces you to do case A: break; case B: break; case C: break;

    should use an if and say if(A || B || C) // more readable

    Firstly you're wrong - C# does it the C way.

    Als, as was pointed out by someone else, VB is the most elegant for this sort of thing by far.

    Select Case PlantHeightInMM
        Case Is < 1
           Msgbox "WTF?"
        Case 60, 71, 80
           Msgbox "That is the same size as one of my plants. OMG WOW!"
        Case 1 to 50, Is > 100
           Msgbox "That's not a valid plant size."
        Case 51 to 100
           Msgbox "Just tall enough"
        Case
    End Select

    Of course, Selects can be abused, even in VB.


  • (cs) in reply to Drak
    Drak:

    Anonymous:
    If the language is C#, the WTF is that they used a swithc instead of an if. C# doesnt allow fall-through like C++.

    so C++ lets you do case A: case B: case C: // more readable than c# version
    but C# forces you to do case A: break; case B: break; case C: break;

    should use an if and say if(A || B || C) // more readable

    Are you sure C# doesn't have a better method? VB.NET uses the following:

    select case (something)
       case A, B, C: dostuff
       case D, E: dootherstuff
    end select

    I'm sure C# has a similar construct, ad after testing, it looks like it works exactly the same as the C++ variant:

    <font color="#0000ff" size="2"> </font>

    <font color="#0000ff" size="2">switch</font><font size="2">(i)
    {
       </font><font color="#0000ff" size="2">case</font><font size="2"> 0:
       </font><font color="#0000ff" size="2">case</font><font size="2"> 1:
    </font><font color="#0000ff" size="2">   case</font><font size="2"> 2:
          i = 5;
          </font><font color="#0000ff" size="2">break</font><font size="2">;
       </font><font color="#0000ff" size="2">case</font><font size="2"> 3:
          i = 6;
          </font><font color="#0000ff" size="2">break</font><font size="2">;
    }</font>

    <font size="2">This results in i ==5 if i starts as 0, 1 or 2, and i==6 if i starts as 3.</font>

    <font size="2">Drak</font>



    Never touched c# ... bad karma.

    I think what Anonymous meant was that c# does not allow fall-through. The do allow multiple labels though.
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfTheSwitchStatement.asp
    So unless you want to beautify your code with nice-looking goto - no fall-through for you  <:o)

    poor duff

  • (cs) in reply to Mung Kee

    Mung Kee:
    I have seen, on many occassions, people do this very same thing with exception handling.  For example:

    try{
        ...
    }catch(IOException ioe){
        log.debug("There was a IOException", ioe);
        throw ioe;
    }catch(SAXException saxe){
        log.debug("There was a SAXException", saxe);
        throw saxe;
    }catch(NumberFormatException nfe){
        log.debug("There was a NumberFormatException", nfe);
        throw saxe;
    }

    Heh, that won't compile.

  • (cs) in reply to OneFactor

    OneFactor:
    switch (state)
    {
        case Enumerations.State.Gollum:
          filthyHobbits.GetPreciousFrom()
          break;
       case Enumerations.State.Smeagol:
          filthyHobbits.GetPreciousFrom()
          break;
       default:
          filthyHobbits.GetPreciousFrom()
          break;
    }

    I totally read this method as <FONT face="Courier New">GetPreciousForm()</FONT>. I was wondering what the class declaration would be like:

    <FONT face="Courier New"><FONT style="BACKGROUND-COLOR: #ffffff" color=#0000ff>using</FONT> System.Windows.Forms;
    </FONT><FONT face="Courier New">
    <FONT color=#0000ff>public class</FONT> MyPreciousssssssssssssssssssss : Form
    {
    }</FONT>

  • (cs) in reply to lucio
    lucio:
    I totally read this method as <FONT face="Courier New">GetPreciousForm()</FONT>. I was wondering what the class declaration would be like:

    <FONT face="Courier New"><FONT style="BACKGROUND-COLOR: #ffffff" color=#0000ff>using</FONT> System.Windows.Forms;
    </FONT><FONT face="Courier New">
    <FONT color=#0000ff>public class</FONT> MyPreciousssssssssssssssssssss : Form
    {
    }</FONT>

    Oh, and you can't color-code any of your posts... otherwise it looks like [6]

    (in time: only a few of the available emoticons actually render as one... a lot don't: [pi] [au] [um] [mo] [C] [{] [}] [W] [B] etc...

  • (cs) in reply to OMG
    OMG:

    Well, I think it is sound axiomatic advise, let alone programming:

    switch (YouDontSucceed)
    {
       case At.First:
        Try();
        break;
       case At.Second:
        Try();
        break;
       case At.Second:
        Try();
        break;
      default:
        Try();
    }

     

    If at first you don't succeed, give up.  -  Homer Simpson.

     

  • (cs)

    Reminds me of production code in a live app:

    private string AppID
    {
        get { return Request.QueryString["AppID"] != string.Empty ? Request.QueryString["AppID"] : string.Empty; }
    }

    Some programmers are good at writing tautology. Others are good at dribbling.

  • trav (unregistered) in reply to Sean Connery

    C# allows fall through switch statements IF there are no statements in the case block

    For example:

    switch( foo )
    {
         case 1: case 2: case 3:  bar();  break;  // this works fine because of the empty case labels
    }

    switch( foo )
    {
         case 1:  bar();   //compile error b/c there is no break
         case 2: bar(); 
    }

  • Sam (unregistered) in reply to RayS

    RayS:

    Als, as was pointed out by someone else, VB is the most elegant for this sort of thing by far.

     

    Ha!  Like VB is "most elegant" at anything (not that I think it doesn't have it's uses, but elegance simply isn't how I'd characterize it.

    Compare perl (can use integers, strings, slices, arrays, regex patterns, hashes, and subroutines in its switch statement):

            switch ($val) {
                    case 1          { print "number 1" }
                    case "a"        { print "string a" }
                    case [1..10,42] { print "number in list" }
                    case (@array)   { print "number in list" }
                    case /\w+/      { print "pattern" }
                    case qr/\w+/    { print "pattern" }
                    case (%hash)    { print "entry in hash" }
                    case (\%hash)   { print "entry in hash" }
                    case (\&sub)    { print "arg to subroutine" }
                    else            { print "previous case not true" }
            }
  • (cs) in reply to lucio
    lucio:

    Mung Kee:
    I have seen, on many occassions, people do this very same thing with exception handling.  For example:

    try{
        ...
    }catch(IOException ioe){
        log.debug("There was a IOException", ioe);
        throw ioe;
    }catch(SAXException saxe){
        log.debug("There was a SAXException", saxe);
        throw saxe;
    }catch(NumberFormatException nfe){
        log.debug("There was a NumberFormatException", nfe);
        throw saxe;
    }

    Heh, that won't compile.



    For the love of <insert your deity here>, I hope you're not referring to the "...", or the fact that I didn't include the import statements.  Of course it won't compile!
  • Jamison Roberts (unregistered) in reply to Mung Kee

    Probably referring to the fact that you have a throw saxe; in the final catch, which of course would be out of scope...

  • (cs) in reply to Jamison Roberts
    Anonymous:
    Probably referring to the fact that you have a throw saxe; in the final catch, which of course would be out of scope...


    Ah, fair enough.  What do you want for an example?!?!  :^)
  • (cs) in reply to Mung Kee
    Mung Kee:
    lucio:

    Mung Kee:
    I have seen, on many occassions, people do this very same thing with exception handling.  For example:

    try{
        ...
    }catch(IOException ioe){
        log.debug("There was a IOException", ioe);
        throw ioe;
    }catch(SAXException saxe){
        log.debug("There was a SAXException", saxe);
        throw saxe;
    }catch(NumberFormatException nfe){
        log.debug("There was a NumberFormatException", nfe);
        throw saxe;
    }

    Heh, that won't compile.



    For the love of <INSERT here deity your>, I hope you're not referring to the "...", or the fact that I didn't include the import statements.  Of course it won't compile!

    I think lucio was referring to the throw saxe in the catch nfe section. Though getting back to your original example, how else can you log the exceptions and then rethrow and retain the throws signature of the method? As long as you have checked exceptions, you will be stuck with having to write crap like this.

  • (cs) in reply to RayS
    RayS:

    Als, as was pointed out by someone else, VB is the most elegant for this sort of thing by far.

    Select Case PlantHeightInMM
        Case Is < 1
           Msgbox "WTF?"
        Case 60, 71, 80
           Msgbox "That is the same size as one of my plants. OMG WOW!"
        Case 1 to 50, Is > 100
           Msgbox "That's not a valid plant size."
        Case 51 to 100
           Msgbox "Just tall enough"
        Case
    End Select

    Of course, Selects can be abused, even in VB.


    What about if the range is not inclusive as in >=51 to <100?

    I prefer xBASE.  The condition can be arbitrary:

    <font size="2">do case
    case PlantHeightInMM<1
       ? "WTF?"
    case inlist(PlantHeightInMM,60,71,80)
       ? "That is the same size as one of my plants. OMG WOW!"
    case between(PlantHeightInMM,1,50) or PlantHeightInMM>100
       ? "That's not a valid plant size."
    case between(PlantHeightinMM,51,100)
       ? "Just tall enough"
       endcase
    </font>
    Repeating the variable name is not ideal, but it is very handy when the condition involves more than one variable.  I have some code where there are three subexpressions to iterate through.  It is much more readable without nested cases.  (YM and preferences MV.)

    Sincerely,

    Gene Wirchenko

  • RaolinDarksbane (unregistered) in reply to OneFactor

    Shouldn't you be doing something like this:
    Gollum.GetPreciousFrom(filthyHobbits)
    or even
    this.GetPreciousFrom(filthyHobbits)
    assuming this is the object in which state is held

    It is counter intuitive to have the filthyHobbits perform GetPreciousFrom() when the desired goal is to have the precious leave the possession of the filthyHobbits.

    "Return with your shield, or on it"

  • (cs) in reply to OneFactor
    OneFactor:
    Mung Kee:
    lucio:

    Mung Kee:
    I have seen, on many occassions, people do this very same thing with exception handling.  For example:

    try{
        ...
    }catch(IOException ioe){
        log.debug("There was a IOException", ioe);
        throw ioe;
    }catch(SAXException saxe){
        log.debug("There was a SAXException", saxe);
        throw saxe;
    }catch(NumberFormatException nfe){
        log.debug("There was a NumberFormatException", nfe);
        throw saxe;
    }

    Heh, that won't compile.



    For the love of <insert here="" deity="" your="">, I hope you're not referring to the "...", or the fact that I didn't include the import statements.  Of course it won't compile!
    </insert>

    I think lucio was referring to the throw saxe in the catch nfe section. Though getting back to your original example, how else can you log the exceptions and then rethrow and retain the throws signature of the method? As long as you have checked exceptions, you will be stuck with having to write crap like this.



    The answer to that question depends a lot on where in the application the Exception was thrown.  If it's thrown somewhere within the business layer, once it propagates up to the public interface of the business layer you, at a minimum, wrap it in a generic or application specific Exception type.

    throw new Exception("Some error", saxe)
    or
    throw new BusinessLayerException("Some error", saxe)
    or
    throw new RemoteException("Some error", saxe)
  • (cs) in reply to Gene Wirchenko

    Gene Wirchenko:

    I prefer xBASE.  The condition can be arbitrary:

    <FONT size=2>do case
    case PlantHeightInMM<1
       ? "WTF?"
    case inlist(PlantHeightInMM,60,71,80)
       ? "That is the same size as one of my plants. OMG WOW!"
    case between(PlantHeightInMM,1,50) or PlantHeightInMM>100
       ? "That's not a valid plant size."
    case between(PlantHeightinMM,51,100)
       ? "Just tall enough"
       endcase
    </FONT>
    Repeating the variable name is not ideal, but it is very handy when the condition involves more than one variable.

    That's possible in VB too (.net and the oldies):

    Select Case True
    Case PlantHeightInMM<1
          ...
    Case PlantHeightInMM>100
          ...
    End Select

    (Don't know about C#)

  • (cs) in reply to Mung Kee
    Mung Kee:
    OneFactor:
    Mung Kee:
    lucio:

    Mung Kee:
    I have seen, on many occassions, people do this very same thing with exception handling.  For example:

    try{
        ...
    }catch(IOException ioe){
        log.debug("There was a IOException", ioe);
        throw ioe;
    }catch(SAXException saxe){
        log.debug("There was a SAXException", saxe);
        throw saxe;
    }catch(NumberFormatException nfe){
        log.debug("There was a NumberFormatException", nfe);
        throw saxe;
    }

    Heh, that won't compile.



    For the love of <INSERT your="" deity="" here="">, I hope you're not referring to the "...", or the fact that I didn't include the import statements.  Of course it won't compile!
    </INSERT>

    I think lucio was referring to the throw saxe in the catch nfe section. Though getting back to your original example, how else can you log the exceptions and then rethrow and retain the throws signature of the method? As long as you have checked exceptions, you will be stuck with having to write crap like this.



    The answer to that question depends a lot on where in the application the Exception was thrown.  If it's thrown somewhere within the business layer, once it propagates up to the public interface of the business layer you, at a minimum, wrap it in a generic or application specific Exception type.

    throw new Exception("Some error", saxe)
    or
    throw new BusinessLayerException("Some error", saxe)
    or
    throw new RemoteException("Some error", saxe)

    But what if I want to log and rethrow WITHOUT wrapping? After all, the catch block may have access to critical information that I wish to log (for example, the name of a file that was not found, the SQL of a query which failed, or the UID of a row which failed validation) without being the ideal place to wrap/handle.

  • (cs) in reply to SteveM

    SelectedValue returns the "Selected Value" not the "Text".  The select value could be an ID and not the Text displayed in the dropdown list.  Jakeypoo's code sucks but your suggestion is a bug and that is worse. 

    A better way to get the text, that doesn't introduce a bug would be the following:

       lblNavigation.Text = ddlOperationList.SelectedItem.Text

    Also, writing   lblNavigation.Text = ddlOperation.SelectValue.ToString(); would be foolish since SelectedValue clearly returns a string value.<FONT size=1>

    </FONT>
  • (cs) in reply to OneFactor
    OneFactor:

    But what if I want to log and rethrow WITHOUT wrapping? After all, the catch block may have access to critical information that I wish to log (for example, the name of a file that was not found, the SQL of a query which failed, or the UID of a row which failed validation) without being the ideal place to wrap/handle.


    If you want to log and throw without wrapping got right ahead.  But, in addition to throwing an exception, you may be throwing out modularity.  The general principal is that the caller of a method shouldn't have specific knowledge of how the method does it's work.  If you see a method that throws an IOException, a SAXException and a NumberFormatException, you have a very good idea of what it's doing.  I would give you 1000 to 1 odds, after seeing only the signature, that that method is opening an XML file, parsing it and trying to convert some of its data to numbers.  As I said, the granularity of exceptions thrown depends a lot on the context.
  • Daruku (unregistered)

    At least the first one is commented.

  • (cs)

    For some reason, i have a feeling that the writer of the first code would do another "switch" check in "

    LoadPageData();

    Function... Hey, i am not the one who wrote it though..
    but I kind of seem to remember the time i made a similar architectural
    mistakes like above...

  • (cs) in reply to Sam
    Anonymous:

    RayS:

    Als, as was pointed out by someone else, VB is the most elegant for this sort of thing by far.

     

    Ha!  Like VB is "most elegant" at anything (not that I think it doesn't have it's uses, but elegance simply isn't how I'd characterize it.

    Compare perl (can use integers, strings, slices, arrays, regex patterns, hashes, and subroutines in its switch statement):

            switch ($val) {
                    case 1          { print "number 1" }
    case "a" { print "string a" }
    case [1..10,42] { print "number in list" }
    case (@array) { print "number in list" }
    case /\w+/ { print "pattern" }
    case qr/\w+/ { print "pattern" }
    case (%hash) { print "entry in hash" }
    case (\%hash) { print "entry in hash" }
    case (\?) { print "arg to subroutine" }
    else { print "previous case not true" }
    }


    There's a switch statement in Perl?? Where the fsck have I been??!

        dZ.

  • (cs) in reply to DZ-Jay
    DZ-Jay:
    There's a switch statement in Perl?? Where the fsck have I been??!


    Apparently as of 5.8 there is.  It's pretty awkward though.  I think there are some PMs out there too.
  • (cs) in reply to Gene Wirchenko
    Gene Wirchenko:
    RayS:

    Als, as was pointed out by someone else, VB is the most elegant for this sort of thing by far.

    Select Case PlantHeightInMM
        Case Is < 1
           Msgbox "WTF?"
        Case 60, 71, 80
           Msgbox "That is the same size as one of my plants. OMG WOW!"
        Case 1 to 50, Is > 100
           Msgbox "That's not a valid plant size."
        Case 51 to 100
           Msgbox "Just tall enough"
        Case
    End Select

    Of course, Selects can be abused, even in VB.


    What about if the range is not inclusive as in >=51 to <100?

    I prefer xBASE.  The condition can be arbitrary:

    <font size="2">do case
    case PlantHeightInMM<1
       ? "WTF?"
    case inlist(PlantHeightInMM,60,71,80)
       ? "That is the same size as one of my plants. OMG WOW!"
    case between(PlantHeightInMM,1,50) or PlantHeightInMM>100
       ? "That's not a valid plant size."
    case between(PlantHeightinMM,51,100)
       ? "Just tall enough"
       endcase
    </font>
    Repeating the variable name is not ideal, but it is very handy when the condition involves more than one variable.  I have some code where there are three subexpressions to iterate through.  It is much more readable without nested cases.  (YM and preferences MV.)

    Sincerely,

    Gene Wirchenko


    Well my point wasn't to say "VB is the best!", just to show that C has probably the least useful case system I've ever seen. VB just happens to be the one I'm most used to. xBase's  one looks pretty neat.
  • (cs) in reply to RayS
    RayS:
    Well my point wasn't to say "VB is the best!", just to show that C has probably the least useful case system I've ever seen. VB just happens to be the one I'm most used to. xBase's  one looks pretty neat.


    I was pointing out alternatives, too.

    C's switch statement is braindead.  It is very good for generating very efficient object code.  I can see how it can be compiled easily into a jump table.  The problem is that I rarely code in such a way that C's switch statement is of use to me.  The only time I do that is when writing a menu (and the switch does not save much there) or some sort of interpreter.

    C gives too much to the corner cases and causes trouble for the more usual cases.  (Omit a break statement to see what I mean.)

    Sincerely,

    Gene Wirchenko

  • Brilliantnut (unregistered) in reply to Michael Casadevall
    Sonic McTails:
    Actually, the later one isn't too bad, and there are reasons why someone would want to do it like that, like if they want it to do said action if the variable isn't set, or set to Y. It might be that for all other cases (variable set to non-zero, non-Y value) that another action should take place, or this one shouldn't. Still, it could have been condensed onto one line.

    That makes sense, but only if the code is an If...Else If...Else... How does the If...Else... of the original post handle the situation that you point out?

  • anonymouse (unregistered) in reply to DD
    Anonymous:
    For example:

    switch (state)
    {
    case Person.State.Happy:
    Drink();
    break;
    case Person.State.Unhappy:
    Drink();
    break;
    case Person.State.JusGotDumpedByGirlfriend:
    Drink();
    break;
    case
    Person.State.JusGotPayRise:
    Drink();
    break
    ;
    default:
    Drink();
    }


    no it doesn't. for that to for this to make sense you'd need to remove the breaks...

  • (cs)

    Obviously, this programmer is paid by the line.

Leave a comment on “1, 1, or 1, or if you really want, 1”

Log In or post as a guest

Replying to comment #:

« Return to Article