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 4 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.
  • 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
              • 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

                D Offline
                D Offline
                Deflinek
                wrote on last edited by
                #33

                qualitychecker wrote:

                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

                You cannot satisfy rule (4) AND all the others :)

                -- "My software never has bugs. It just develops random features."

                1 Reply 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

                  U Offline
                  U Offline
                  User 4483848
                  wrote on last edited by
                  #34

                  qualitychecker wrote:

                  (1) : prevent against '=' instead of '==' : always put constants first

                  I used to do this a bit when I was writting C, but I don't think there is any benefit with C#. Try doing a single = in a in a if statement and the compiler will complain. So can anybody see a benefit of doing (null == x)? Actually, I've just checked, it doesn't always complain, although I think it does sometimes complain.

                  modified on Monday, January 12, 2009 9:22 AM

                  P 1 Reply Last reply
                  0
                  • U User 4483848

                    qualitychecker wrote:

                    (1) : prevent against '=' instead of '==' : always put constants first

                    I used to do this a bit when I was writting C, but I don't think there is any benefit with C#. Try doing a single = in a in a if statement and the compiler will complain. So can anybody see a benefit of doing (null == x)? Actually, I've just checked, it doesn't always complain, although I think it does sometimes complain.

                    modified on Monday, January 12, 2009 9:22 AM

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

                    Member 4487083 wrote:

                    the compiler will complain

                    Except when using booleans.

                    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?

                      K Offline
                      K Offline
                      kenrentz
                      wrote on last edited by
                      #36

                      With thinking like that you'll never make it in HR :-D

                      G 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;
                        }
                        
                        J Offline
                        J Offline
                        Jeremy Tierman
                        wrote on last edited by
                        #37

                        Beautiful... failed = failed || true;

                        A 1 Reply 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

                          T Offline
                          T Offline
                          Timothy Byrd
                          wrote on last edited by
                          #38

                          qualitychecker wrote:

                          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; ------------------------------------

                          Oops - hate to say this, but if dt.Rows.Count == 0 - i.e. the table is empty - then that code will throw an array-out-of-bounds exception. You meant else if (dt.Rows.Count <= 0), right? -- T

                          1 Reply Last reply
                          0
                          • K kenrentz

                            With thinking like that you'll never make it in HR :-D

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

                            There goes my 7 year plan

                            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;
                              }

                              V Offline
                              V Offline
                              vnike
                              wrote on last edited by
                              #40

                              Other than what has been already pointed out..."code trimming" for lesser cycles, I also suggest not to use the string literals as such. They break the clients when modified. static readonly string colName = "Number"; static readonly string colVal = "1"; -------------------------- if(dt != null && dt.Rows.Count > 0) { return dt.Rows[0][colName].ToString().Equals(colVal); } else return false;

                              S 1 Reply Last reply
                              0
                              • V vnike

                                Other than what has been already pointed out..."code trimming" for lesser cycles, I also suggest not to use the string literals as such. They break the clients when modified. static readonly string colName = "Number"; static readonly string colVal = "1"; -------------------------- if(dt != null && dt.Rows.Count > 0) { return dt.Rows[0][colName].ToString().Equals(colVal); } else return false;

                                S Offline
                                S Offline
                                S Senthil Kumar
                                wrote on last edited by
                                #41

                                vnike wrote:

                                They break the clients when modified.

                                Only if the client is a different assembly. I would imagine that column names and indexes would be private to a class, so I don't see any issue with using const there.

                                Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                                1 Reply Last reply
                                0
                                • J Jeremy Tierman

                                  Beautiful... failed = failed || true;

                                  A Offline
                                  A Offline
                                  Adrian Cole
                                  wrote on last edited by
                                  #42

                                  Short circuit evaluation, man! You don't want to be setting failed to true if it is already! :)

                                  1 Reply 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

                                    A Offline
                                    A Offline
                                    Aerman4567
                                    wrote on last edited by
                                    #43

                                    would the same work for a switch statement?

                                    switch (null)
                                    {
                                    case dt:
                                    {} break;
                                    case dt.rows:
                                    {} break;
                                    default:
                                    {/*actual exec., we have asserted everything's cool*/}
                                    }

                                    or how about this?

                                    switch (true)
                                    {
                                    case (dt == null):
                                    {/*handle the case that dt is null,
                                    i.e. assign it something*/} break;
                                    case (dt.rows == null):
                                    {/*handle the case that dt.rows is null*/} break;
                                    case (dt.rows[0]["number"] == 0):
                                    {/*this element equals 0, it now needs to be assigned*/} break;
                                    default:
                                    {/*computer finally does something right*/} break;
                                    }

                                    maybe nest either one in a do loop till the computer gets its sh*t straight. please tell me why this is wrong, as i am just learning.

                                    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