Is it acceptable to use { } blocks to improve readability and
maintainability of the code?
Example:
//add the checkbox column
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
column.HeaderText = "Include";
column.Name = "IsUsedInNotification";
column.FlatStyle = FlatStyle.Standard;
column.ThreeState = false;
column.ValueType = typeof(bool);
column.TrueValue = true;
column.FalseValue = false;
}
thank you 12 1515
On Sep 23, 12:31*pm, "G.S." <gstoy...@gmail.comwrote:
Is it acceptable to use { } blocks to improve readability and
maintainability of the code?
Example:
//add the checkbox column
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
* * * * column.HeaderText = "Include";
* * * * column.Name = "IsUsedInNotification";
* * * * column.FlatStyle = FlatStyle.Standard;
* * * * column.ThreeState = false;
* * * * column.ValueType = typeof(bool);
* * * * column.TrueValue = true;
* * * * column.FalseValue = false;
}
thank you
Its perfectly valid code.. however I personally prefer using #regions
for the same.
-Cnu
G.S. <gs******@gmail.comwrote:
Is it acceptable to use { } blocks to improve readability and
maintainability of the code?
Example:
//add the checkbox column
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
column.HeaderText = "Include";
column.Name = "IsUsedInNotification";
column.FlatStyle = FlatStyle.Standard;
column.ThreeState = false;
column.ValueType = typeof(bool);
column.TrueValue = true;
column.FalseValue = false;
}
It's a little bit odd, but generally okay. If you're using C# 3,
however, you can do even better with an object initializer:
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn
{
HeaderText = "Include";
Name = "IsUsedInNotification";
FlatStyle = FlatStyle.Standard;
ThreeState = false;
ValueType = typeof(bool);
TrueValue = true;
FalseValue = false;
};
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Ah. I did this some time ago when using XmlTextWriter. I will revise my
newest code that uses it and put some in :-)
writer.WriteStartElement("thing");
{
writer.WriteAttributeString("name", "eggs");
writer.WriteAttributeString("size", "7");
writer.WriteStartElement("innerThing");
{
etc
}
writer.WriteEndElement();
}
writer.WriteEndElement();
Not sure where to put the { and }, but it looks better than this
writer.WriteStartElement("thing");
writer.WriteAttributeString("name", "eggs");
writer.WriteAttributeString("size", "7");
writer.WriteStartElement("innerThing");
etc
writer.WriteEndElement();
writer.WriteEndElement();
Would be great if WriteStartElement returned an IDisposable wouldn't it.
Pete
On Sep 23, 3:54*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
G.S. <gstoy...@gmail.comwrote:
Is it acceptable to use { } blocks to improve readability and
maintainability of the code?
Example:
//add the checkbox column
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
* *column.HeaderText = "Include";
* *column.Name = "IsUsedInNotification";
* *column.FlatStyle = FlatStyle.Standard;
* *column.ThreeState = false;
* *column.ValueType = typeof(bool);
* *column.TrueValue = true;
* *column.FalseValue = false;
}
It's a little bit odd, but generally okay. If you're using C# 3,
however, you can do even better with an object initializer:
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn
{
* * HeaderText = "Include";
* * Name = "IsUsedInNotification";
* * FlatStyle = FlatStyle.Standard;
* * ThreeState = false;
* * ValueType = typeof(bool);
* * TrueValue = true;
* * FalseValue = false;
};
--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com- Hide quoted text -
- Show quoted text -
yes, it's a bit odd 'cause it may throw you off to think that it's a
using statement (like PeterMoris suggests). And it confuses VS2005
auto-format feature, but overall I've liked it and used it in long and
tedious initialization routines
On Sep 23, 3:31 pm, "G.S." <gstoy...@gmail.comwrote:
Is it acceptable to use { } blocks to improve readability and
maintainability of the code?
Example:
//add the checkbox column
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
column.HeaderText = "Include";
column.Name = "IsUsedInNotification";
column.FlatStyle = FlatStyle.Standard;
column.ThreeState = false;
column.ValueType = typeof(bool);
column.TrueValue = true;
column.FalseValue = false;
}
thank you
I also prefer using #regions.
I at least once I have used something similar but for different
reason.
I receive an object reference that I know is one of a given set of
types. Depending f the type I need one property or the other, so I
use somethign like
Contact c=null;
while(true)
{
Individual i = obj as Individual;
if ( i!= null )
{
c = Individual;
break;
}
Supplier s = obj as Supplier ;
if ( s!= null )
{
c = s.ContactPerson;
break;
}
....
break;
}
On Sep 23, 2:31*pm, "G.S." <gstoy...@gmail.comwrote:
Is it acceptable to use { } blocks to improve readability and
maintainability of the code?
Example:
//add the checkbox column
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
* * * * column.HeaderText = "Include";
* * * * column.Name = "IsUsedInNotification";
* * * * column.FlatStyle = FlatStyle.Standard;
* * * * column.ThreeState = false;
* * * * column.ValueType = typeof(bool);
* * * * column.TrueValue = true;
* * * * column.FalseValue = false;
}
thank you
I have done it in the past. Though, I can't remember off hand when
the last time I did. The reason I did was for scoping purposes. In
most cases it makes more since to declare a new method or just use
different variable names to avoid scoping issues, but occasionally you
may find that you want to declare the same variable name multiple
times in the same method without isolating them in control flow
statement such as for, if, while, foreach, etc.
Thanks to your reminder I have now just added the following class to a
common assembly....
public static class XmlWriterHelper
{
public static IDisposable StartElement(this XmlWriter writer, string
elementName)
{
return new DisposableElementWriter(writer, elementName);
}
#region IDisposableElementWriter
private class DisposableElementWriter : IDisposable
{
private XmlWriter Writer;
public DisposableElementWriter(XmlWriter writer, string elementName)
{
Writer = writer;
Writer.WriteStartElement(elementName);
}
public void Dispose()
{
Writer.WriteEndElement();
}
}
#endregion
}
Now I can write code like this
using (writer.StartElement("data"))
{
writer.WriteAttributeString("1", "1");
writer.WriteAttributeString("2", "2");
writer.WriteAttributeString("3", "3");
using (writer.StartElement("systemData"))
{
writer.WriteAttributeString("a", "a");
writer.WriteAttributeString("b", "b");
}//systemData
}//data
Which is easier to read than this:
writer.WriteStartElement("data");
writer.WriteAttributeString("1", "1");
writer.WriteAttributeString("2", "2");
writer.WriteAttributeString("3", "3");
writer.WriteStartElement("systemData");
writer.WriteAttributeString("a", "a");
writer.WriteAttributeString("b", "b");
writer.WriteEndElement();//systemData
writer.WriteEndElement();//data
Now I can write code like this
.... [snip]
Obviously XmlWriter will be more efficient for bulk work, but for mid-
size xml, XDocument etc (.NET 3.5) are useful for this:
var data = new XElement("data",
new XAttribute("a1","1"),
new XAttribute("a2","2"),
new XAttribute("a3","3"),
new XElement("systemData",
new XAttribute("a","a"),
new XAttribute("b","b")
)
);
Different approach, but a similar readability improvement.
Marc
Hi Marc
Would that also work for something like
using (DocumentWriter.StartElement("airports"))
{
foreach (Airport airport in Airports)
using (DocumentWriter.StartElement("airport"))
{
blah
}
}
Regards
Pete
"Peter Morris" <mr*********@SPAMgmail.comwrote in message
news:uj**************@TK2MSFTNGP04.phx.gbl...
Ah. I did this some time ago when using XmlTextWriter. I will revise my
newest code that uses it and put some in :-)
writer.WriteStartElement("thing");
{
writer.WriteAttributeString("name", "eggs");
writer.WriteAttributeString("size", "7");
writer.WriteStartElement("innerThing");
{
etc
}
writer.WriteEndElement();
}
writer.WriteEndElement();
Not sure where to put the { and }, but it looks better than this
writer.WriteStartElement("thing");
writer.WriteAttributeString("name", "eggs");
writer.WriteAttributeString("size", "7");
writer.WriteStartElement("innerThing");
etc
writer.WriteEndElement();
writer.WriteEndElement();
Would be great if WriteStartElement returned an IDisposable wouldn't it.
Pete
The way you wrote it, sure it looks a bit off...but you could write it like
this as well:
// Write the start element and it's attributes.
writer.WriteStartElement("thing");
writer.WriteAttributeString("name", "eggs");
writer.WriteAttributeString("size", "7");
// Write the innerThing child element and it's attributes.
writer.WriteStartElement("innerThing");
writer.WriteAttributeString("name", "chickens");
writer.WriteAttributeString("size", "0.80"); // has head cut off
etc...
// Write the closing of the innerThing element.
writer.WriteEndElement();
// Write the closing of the thing element.
writer.WriteEndElement();
Doing it like the above helps keep code documented....and uses up less
screen space (due to excluding the extra indentations)...
HTH,
Mythran
The way you wrote it, sure it looks a bit off...but you could write it
like this as well:
<snip>
Problem is when you are 4 or 5 levels deep it REALLY looks bad. This code
though is very intuitive I think....
using (writer.StartElement("data"))
{
writer.WriteAttributeString("1", "1");
writer.WriteAttributeString("2", "2");
writer.WriteAttributeString("3", "3");
using (writer.StartElement("systemData"))
{
writer.WriteAttributeString("a", "a");
writer.WriteAttributeString("b", "b");
}//systemData
}//data
Regards
Pete
"Peter Morris" <mr*********@SPAMgmail.comwrote in message
news:Oy**************@TK2MSFTNGP05.phx.gbl...
>The way you wrote it, sure it looks a bit off...but you could write it like this as well:
<snip>
Problem is when you are 4 or 5 levels deep it REALLY looks bad. This code
though is very intuitive I think....
using (writer.StartElement("data"))
{
writer.WriteAttributeString("1", "1");
writer.WriteAttributeString("2", "2");
writer.WriteAttributeString("3", "3");
using (writer.StartElement("systemData"))
{
writer.WriteAttributeString("a", "a");
writer.WriteAttributeString("b", "b");
}//systemData
}//data
Regards
Pete
4 or 5 levels deep I would probably break it into functional parts...maybe
create a method to do each element or block of elements....to break it
apart...I mean, at some point...you may end up 20 levels deep...are you
going to indent your code to 20 indent levels? This is theoretic of course
and should be done some other way for many levels deep... In any case, it is
all personal opinion and up to you in the end :)
Mythran This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: hokiegal99 |
last post by:
A few questions about the following code. How would I "wrap" this in a
function, and do I need to?
Also, how can I make the code smart enough to realize that when a file
has 2 or more bad...
|
by: james545 |
last post by:
I saw some code written where member functions were written inside
struct definition blocks and was wondering if this is an advisable or
common style of programming in C/C++?
The type of code I...
|
by: peterbe |
last post by:
I have an XML string coming in from one system that I'd like to tidy up
and return in a very particular format. I'm picky!
If the input is
<SOMETHING attr1="foo1"
attr2='foo2' >
Then the...
|
by: rincewind |
last post by:
I remember reading an article (was it Herb Sutter's?) that recommended
avoiding using directives. While I quite understand this recommendation
for headers, what's wrong in using directive in .cpp...
|
by: brenocon |
last post by:
Hi all --
Compared to the Python I know and love, Ruby isn't quite the same.
However, it has at least one terrific feature: "blocks". Whereas in
Python a
"block" is just several lines of...
|
by: Velochicdunord |
last post by:
Hi everyone,
I've been lurking for two weeks now, learning as much as I can about
CSS.
Still in the bottom half of the curve, but looking to figure out how to
do more stuff.
I've just tossed...
|
by: Martin |
last post by:
I have a question regarding *use* of blocks.
In Plauger's THE STANDARD C LIBRARY in xfmtval.c in Chapter 6 I noticed in
function _Fmtval() that after some processing of 50 lines or so he creates...
|
by: Frank Hauptlorenz |
last post by:
Hello,
I recognized some days ago, that returning a DataTable blocks my
WCF-Service. Is this a known bug?
If I add this table to a new DataSet() and return this, it works.
Thank you,
Frank
|
by: =?Utf-8?B?QU9UWCBTYW4gQW50b25pbw==?= |
last post by:
Hi,
I have been using the code (some of it has been removed for simplicity)
below to allow authenticated (using ASP.NET membership database) users to get
a file from their archive area. It...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |