By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,369 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,369 IT Pros & Developers. It's quick & easy.

Using { } blocks to improve readability

P: n/a
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
Sep 23 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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
Sep 23 '08 #2

P: n/a
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
Sep 23 '08 #3

P: n/a
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

Sep 23 '08 #4

P: n/a
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
Sep 23 '08 #5

P: n/a
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;
}
Sep 23 '08 #6

P: n/a
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.
Sep 24 '08 #7

P: n/a
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

Sep 24 '08 #8

P: n/a
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

Sep 24 '08 #9

P: n/a
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

Sep 24 '08 #10

P: n/a


"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
Sep 24 '08 #11

P: n/a
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

Sep 25 '08 #12

P: n/a


"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
Sep 25 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.