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. General Programming
  3. C#
  4. Writing correct code

Writing correct code

Scheduled Pinned Locked Moved C#
cssquestion
6 Posts 5 Posters 0 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.
  • A Offline
    A Offline
    Akhilesh Yadav
    wrote on last edited by
    #1

    Hi, I've two code snippets just wanna ask which is the right way of writing and why? Fist snippet -------------- DataGridView grid = new DataGridView() foreach(string colName in ColumnNames) { DataGridViewColumn gridCol = null; if() { gridCol = new DataGridViewColumn(); } else { gridCol = new MyColumn(); } grid.Columns.Add(gridCol); } Second Snippet ---------------- DataGridView grid = new DataGridView() DataGridViewColumn gridCol = null; foreach(string colName in ColumnNames) { if() { gridCol = new DataGridViewColumn(); } else { gridCol = new MyColumn(); } grid.Columns.Add(gridCol); }

    --Akki

    P T L 3 Replies Last reply
    0
    • A Akhilesh Yadav

      Hi, I've two code snippets just wanna ask which is the right way of writing and why? Fist snippet -------------- DataGridView grid = new DataGridView() foreach(string colName in ColumnNames) { DataGridViewColumn gridCol = null; if() { gridCol = new DataGridViewColumn(); } else { gridCol = new MyColumn(); } grid.Columns.Add(gridCol); } Second Snippet ---------------- DataGridView grid = new DataGridView() DataGridViewColumn gridCol = null; foreach(string colName in ColumnNames) { if() { gridCol = new DataGridViewColumn(); } else { gridCol = new MyColumn(); } grid.Columns.Add(gridCol); }

      --Akki

      P Offline
      P Offline
      Perry Holman
      wrote on last edited by
      #2

      Obviously, the 2nd snippet is better because many temporary variables will be generated in the 1st snippet. That is, more memories are occupied and more initialization are done.

      Welcome to www.softwaretree.net! You can find many excellent audio/video converter tools there!

      A 1 Reply Last reply
      0
      • P Perry Holman

        Obviously, the 2nd snippet is better because many temporary variables will be generated in the 1st snippet. That is, more memories are occupied and more initialization are done.

        Welcome to www.softwaretree.net! You can find many excellent audio/video converter tools there!

        A Offline
        A Offline
        Akhilesh Yadav
        wrote on last edited by
        #3

        Thanks i was expecting the same. Is it possible to know how much memory is being consumed or utilized. I mean any tool you know or by writing some code.

        --Akki

        M 1 Reply Last reply
        0
        • A Akhilesh Yadav

          Thanks i was expecting the same. Is it possible to know how much memory is being consumed or utilized. I mean any tool you know or by writing some code.

          --Akki

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          As a caveat to that it can be useful for support reasons to have temp vars availableduring the development stage.

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • A Akhilesh Yadav

            Hi, I've two code snippets just wanna ask which is the right way of writing and why? Fist snippet -------------- DataGridView grid = new DataGridView() foreach(string colName in ColumnNames) { DataGridViewColumn gridCol = null; if() { gridCol = new DataGridViewColumn(); } else { gridCol = new MyColumn(); } grid.Columns.Add(gridCol); } Second Snippet ---------------- DataGridView grid = new DataGridView() DataGridViewColumn gridCol = null; foreach(string colName in ColumnNames) { if() { gridCol = new DataGridViewColumn(); } else { gridCol = new MyColumn(); } grid.Columns.Add(gridCol); }

            --Akki

            T Offline
            T Offline
            Thomas Weller 0
            wrote on last edited by
            #5

            Try this:

            DataGridView grid = new DataGridView();

            foreach(string colName in ColumnNames)
            {
            grid.Columns.Add(
            ([condition]) ?
            new DataGridViewColumn() :
            new MyColumn());
            }

            And btw: Don't care about performance at all, as long as you have no hard evidence (i.e. measures or customer complaints) about it. During development, you should only care about the correct functioning, behaviour and readability of your code. Use unit tests to reliably assure this. Regards Thomas

            www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
            Programmer - an organism that turns coffee into software.

            1 Reply Last reply
            0
            • A Akhilesh Yadav

              Hi, I've two code snippets just wanna ask which is the right way of writing and why? Fist snippet -------------- DataGridView grid = new DataGridView() foreach(string colName in ColumnNames) { DataGridViewColumn gridCol = null; if() { gridCol = new DataGridViewColumn(); } else { gridCol = new MyColumn(); } grid.Columns.Add(gridCol); } Second Snippet ---------------- DataGridView grid = new DataGridView() DataGridViewColumn gridCol = null; foreach(string colName in ColumnNames) { if() { gridCol = new DataGridViewColumn(); } else { gridCol = new MyColumn(); } grid.Columns.Add(gridCol); }

              --Akki

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

              it is all the same, in debug build you shouldn't care, and in release build the compiler will optimize and generate the same code for both. However 1 has better style if you ask me since it keeps gridCol's scope inside the loop. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


              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