Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Magic of if...else...programming

Magic of if...else...programming

Scheduled Pinned Locked Moved The Weird and The Wonderful
43 Posts 23 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G GibbleCH

    Those are the people you don't have working for you for long...one would hope.

    V Offline
    V Offline
    vaghelabhavesh
    wrote on last edited by
    #13

    Yeah Its been two years. I asked the HR department to improve filtering & recruitment process but as they are looking for cheap labor I don't think this will ever happen.

    G 1 Reply Last reply
    0
    • L Luc Pattyn

      Nah. This is much more difficult to debug, how would you set a breakpoint anywhere inside such a complex expression?

      Luc Pattyn [Forum Guidelines] [My Articles]


      Love, happiness and fewer bugs for 2009!


      A Offline
      A Offline
      Andrew Rissing
      wrote on last edited by
      #14

      If the statement fails, just put a breakpoint on the entire statement. Then, highlight each portion, right click, and select 'Add to Watch'. Viola! You'll see whether or not the statement passed. Rinse and repeat.

      A L 2 Replies Last reply
      0
      • A Andrew Rissing

        If the statement fails, just put a breakpoint on the entire statement. Then, highlight each portion, right click, and select 'Add to Watch'. Viola! You'll see whether or not the statement passed. Rinse and repeat.

        A Offline
        A Offline
        Andrew Rissing
        wrote on last edited by
        #15

        And yes...I know it was a joke. But still...not sure if everyone knows of such ;)

        P 1 Reply Last reply
        0
        • A Andrew Rissing

          If the statement fails, just put a breakpoint on the entire statement. Then, highlight each portion, right click, and select 'Add to Watch'. Viola! You'll see whether or not the statement passed. Rinse and repeat.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #16

          Thank you for the tip; I rarely use those fancy debug features. It does widen the scope of the breakpoint, hence requires more human intervention to narrow it down, but it works. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Love, happiness and fewer bugs for 2009!


          1 Reply Last reply
          0
          • A Andrew Rissing

            And yes...I know it was a joke. But still...not sure if everyone knows of such ;)

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #17

            I certainly didn't, and I probably won't the next time I could use it.

            1 Reply Last reply
            0
            • T Timothy Byrd

              Oddly enough, I just got rid of some code that looked a bit like that...

              bool InitRoutine()
              {
              bool failed=false;

              ValType val;
              
              HRESULT ans = GetValue1(val);
              if (ans==S\_OK)
                  {
                  globalVal1 = val;
                  }
              else
                  {
                  failed = failed || true;
                  }
              
              ans = GetValue2(val);
              if (ans==S\_OK)
                  {
                  globalVal2 = val;
                  }
              else
                  {
                  failed = failed || true;
                  }
              
              ans = GetValue3(val);
              if (ans==S\_OK)
                  {
                  globalVal3 = val;
                  }
              else
                  {
                  failed = failed || true;
                  }
              
              ans = GetValue4(val);
              if (ans==S\_OK)
                  {
                  globalVal4 = val;
                  }
              else
                  {
                  failed = failed || true;
                  }
              
              
              return !failed;
              }
              
              S Offline
              S Offline
              Shaun Wilde
              wrote on last edited by
              #18

              Ah a follower of the 'there must be only one return statement per method cult'.

              I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it. - pTerry
              BizSquawk

              P 1 Reply Last reply
              0
              • V vaghelabhavesh

                Yeah Its been two years. I asked the HR department to improve filtering & recruitment process but as they are looking for cheap labor I don't think this will ever happen.

                G Offline
                G Offline
                GibbleCH
                wrote on last edited by
                #19

                That's not cheap though, it's way more expensive in the long run if they aren't competent. More fixing bugs, and longer development time. And what the heck kind of code would they put in the else if you don't technically need an else?

                V K 2 Replies Last reply
                0
                • S Shaun Wilde

                  Ah a follower of the 'there must be only one return statement per method cult'.

                  I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it. - pTerry
                  BizSquawk

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #20

                  A misguided one, gives the rest of us a bad name. Though I don't see where any other returns would go.

                  1 Reply Last reply
                  0
                  • G GibbleCH

                    That's not cheap though, it's way more expensive in the long run if they aren't competent. More fixing bugs, and longer development time. And what the heck kind of code would they put in the else if you don't technically need an else?

                    V Offline
                    V Offline
                    vaghelabhavesh
                    wrote on last edited by
                    #21

                    GibbleCH wrote:

                    And what the heck kind of code would they put in the else if you don't technically need an else?

                    I have no idea man....Thank god I am in a different company now :-)

                    1 Reply Last reply
                    0
                    • R Robert Rohde

                      How would you think of this?

                      return dt != null && dt.Rows.Count > 0 && (int)t.Rows[0]["Number"] == 1;

                      The last part depends on the data the table contains. But if its clear that the column is filled with integers then this should be more efficient.

                      B Offline
                      B Offline
                      BadKarma
                      wrote on last edited by
                      #22

                      Just a question: What if there is no value integer or otherwise in t.Rows[0]["Number"]? Wouldn't this result into a crash.

                      Learn from the mistakes of others, you may not live long enough to make them all yourself.

                      R C 2 Replies Last reply
                      0
                      • T Timothy Byrd

                        Oddly enough, I just got rid of some code that looked a bit like that...

                        bool InitRoutine()
                        {
                        bool failed=false;

                        ValType val;
                        
                        HRESULT ans = GetValue1(val);
                        if (ans==S\_OK)
                            {
                            globalVal1 = val;
                            }
                        else
                            {
                            failed = failed || true;
                            }
                        
                        ans = GetValue2(val);
                        if (ans==S\_OK)
                            {
                            globalVal2 = val;
                            }
                        else
                            {
                            failed = failed || true;
                            }
                        
                        ans = GetValue3(val);
                        if (ans==S\_OK)
                            {
                            globalVal3 = val;
                            }
                        else
                            {
                            failed = failed || true;
                            }
                        
                        ans = GetValue4(val);
                        if (ans==S\_OK)
                            {
                            globalVal4 = val;
                            }
                        else
                            {
                            failed = failed || true;
                            }
                        
                        
                        return !failed;
                        }
                        
                        M Offline
                        M Offline
                        Malli_S
                        wrote on last edited by
                        #23

                        Err ! Was that guy thinking to leave the company while writing this code ? :^)

                        [Delegates]      [Virtual Desktop]      [Tray Me !]
                        -Malli...! :rose:****

                        1 Reply Last reply
                        0
                        • S Stephen Hewitt

                          What's so bad about that? I'd do it like the following but I've seen far worse than that code:

                          // Only proceed id if 'dt' is valid and contains at least one row.
                          if (dt == NULL)
                          return false;
                          if (dt.Rows.Count == 0)
                          return false;
                           
                          return (t.Rows[0]["Number"].ToString() == "1");

                          K Offline
                          K Offline
                          KarstenK
                          wrote on last edited by
                          #24

                          Thats fine code: logical development (right order) and easy to understand and maintain. :-O

                          Greetings from Germany

                          1 Reply Last reply
                          0
                          • V vaghelabhavesh

                            This code reminds me one developer working under me, according to her every if has to have else, you can't use if alone. :-)

                            K Offline
                            K Offline
                            KarstenK
                            wrote on last edited by
                            #25

                            Did your hear about the Zune (aka as Y2k9) bug? There was missing an else. X|

                            Greetings from Germany

                            1 Reply Last reply
                            0
                            • B BadKarma

                              Just a question: What if there is no value integer or otherwise in t.Rows[0]["Number"]? Wouldn't this result into a crash.

                              Learn from the mistakes of others, you may not live long enough to make them all yourself.

                              R Offline
                              R Offline
                              Robert Rohde
                              wrote on last edited by
                              #26

                              Thats why I wrote the following after the code block:

                              The last part depends on the data the table contains. But if its clear that the column is filled with integers then this should be more efficient.

                              Probably I should have added: ... and otherwise the code will explode. :-D

                              1 Reply Last reply
                              0
                              • B BadKarma

                                Just a question: What if there is no value integer or otherwise in t.Rows[0]["Number"]? Wouldn't this result into a crash.

                                Learn from the mistakes of others, you may not live long enough to make them all yourself.

                                C Offline
                                C Offline
                                che3358
                                wrote on last edited by
                                #27

                                I believe it will be another if...else IF the developer knew to deal with the DBNull issue that you mentioned. Now, how many ELSE he has? :)

                                1 Reply Last reply
                                0
                                • R Robert Rohde

                                  How would you think of this?

                                  return dt != null && dt.Rows.Count > 0 && (int)t.Rows[0]["Number"] == 1;

                                  The last part depends on the data the table contains. But if its clear that the column is filled with integers then this should be more efficient.

                                  Q Offline
                                  Q Offline
                                  qualitychecker
                                  wrote on last edited by
                                  #28

                                  Nice (and compact) solution !! Still depends on the precedence priorities of the language / the optimization of the underlaying compiler .. More safe and maintainable code: ------------------------------------ bool res = false; if (null == dt) else if (null == dt.Rows) else if (dt.Rows.Count < 0) else res = (1 == (int)dt.Rows[0]["Number"]); return res; ------------------------------------ Rules to be applied : (1) : prevent against '=' instead of '==' : always put constants first (2) : always control potential nulls even if seems useless versus construction rules (ex null == dt.Rows) (3) : provide debugging / tracing points in case of future problems (4) : write readable code (5) : single return output point Shears and happy new year.

                                  modified on Saturday, January 10, 2009 5:03 AM

                                  P D U T A 5 Replies Last reply
                                  0
                                  • Q qualitychecker

                                    Nice (and compact) solution !! Still depends on the precedence priorities of the language / the optimization of the underlaying compiler .. More safe and maintainable code: ------------------------------------ bool res = false; if (null == dt) else if (null == dt.Rows) else if (dt.Rows.Count < 0) else res = (1 == (int)dt.Rows[0]["Number"]); return res; ------------------------------------ Rules to be applied : (1) : prevent against '=' instead of '==' : always put constants first (2) : always control potential nulls even if seems useless versus construction rules (ex null == dt.Rows) (3) : provide debugging / tracing points in case of future problems (4) : write readable code (5) : single return output point Shears and happy new year.

                                    modified on Saturday, January 10, 2009 5:03 AM

                                    P Offline
                                    P Offline
                                    PIEBALDconsult
                                    wrote on last edited by
                                    #29

                                    qualitychecker wrote:

                                    Still depends on the precedence priorities of the language

                                    Uh, yeah, so? Are we going to get into that again?

                                    qualitychecker wrote:

                                    always put constants first

                                    If you can remember to do that, you're smart enough not to make that mistake in the first place.

                                    qualitychecker wrote:

                                    provide debugging / tracing points in case of future problems

                                    No thanks.

                                    L 1 Reply Last reply
                                    0
                                    • P PIEBALDconsult

                                      qualitychecker wrote:

                                      Still depends on the precedence priorities of the language

                                      Uh, yeah, so? Are we going to get into that again?

                                      qualitychecker wrote:

                                      always put constants first

                                      If you can remember to do that, you're smart enough not to make that mistake in the first place.

                                      qualitychecker wrote:

                                      provide debugging / tracing points in case of future problems

                                      No thanks.

                                      L Offline
                                      L Offline
                                      Luc Pattyn
                                      wrote on last edited by
                                      #30

                                      PIEBALDconsult wrote:

                                      If you can remember to do that, you're smart enough not to make that mistake in the first place.

                                      I agree, although one might argue that on the rare occasion one might forget to apply this strange habit, the statement could still be correct (i.e. one could accidentally forget to drop one of the = signs). :-D

                                      Luc Pattyn [Forum Guidelines] [My Articles]


                                      Love, happiness and fewer bugs for 2009!


                                      1 Reply Last reply
                                      0
                                      • C che3358

                                        The following code was found in someone's web template. Hope you enjoy the best logic.

                                        if(dt != null)
                                        {
                                        if(dt.Rows.Count != 0)
                                        {
                                        if(dt.Rows[0]["Number"].ToString() == "1")
                                        {
                                        return true;
                                        }
                                        else
                                        {
                                        return false;
                                        }
                                        }
                                        else
                                        {
                                        return false;
                                        }
                                        }
                                        else
                                        {
                                        return false;
                                        }

                                        T Offline
                                        T Offline
                                        Tony Pottier
                                        wrote on last edited by
                                        #31

                                        There's nothing "horrible" in this code. Industry is all about making something that works and that's easily understandable by your co-workers. Sure, this is going over the top, and I'd do something that was suggested above, like:

                                        if (dt == NULL)
                                        return false;
                                        if (dt.Rows.Count == 0)
                                        return false;

                                        return (t.Rows[0]["Number"].ToString() == "1");

                                        But, seriously, who cares? It's readable and it will run as fast as return dt != null && dt.Rows.Count > 0 && (int)t.Rows[0]["Number"] == 1;

                                        1 Reply Last reply
                                        0
                                        • L Luc Pattyn

                                          Nah. This is much more difficult to debug, how would you set a breakpoint anywhere inside such a complex expression?

                                          Luc Pattyn [Forum Guidelines] [My Articles]


                                          Love, happiness and fewer bugs for 2009!


                                          S Offline
                                          S Offline
                                          Stephen Hewitt
                                          wrote on last edited by
                                          #32

                                          Agreed.

                                          Steve

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups