Writing correct code
-
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
-
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
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!
-
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!
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
-
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
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
-
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
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. -
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
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