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