Using { } blocks to improve readability

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • G.S.

    Using { } blocks to improve readability

    Is it acceptable to use { } blocks to improve readability and
    maintainability of the code?

    Example:

    //add the checkbox column
    DataGridViewChe ckBoxColumn column = new DataGridViewChe ckBoxColumn();
    {
    column.HeaderTe xt = "Include";
    column.Name = "IsUsedInNotifi cation";
    column.FlatStyl e = FlatStyle.Stand ard;
    column.ThreeSta te = false;
    column.ValueTyp e = typeof(bool);
    column.TrueValu e = true;
    column.FalseVal ue = false;
    }

    thank you
  • Duggi

    #2
    Re: Using { } blocks to improve readability

    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
    DataGridViewChe ckBoxColumn column = new DataGridViewChe ckBoxColumn();
    {
            column.HeaderTe xt = "Include";
            column.Name = "IsUsedInNotifi cation";
            column.FlatStyl e = FlatStyle.Stand ard;
            column.ThreeSta te = false;
            column.ValueTyp e = typeof(bool);
            column.TrueValu e = true;
            column.FalseVal ue = false;
    >
    }
    >
    thank you
    Its perfectly valid code.. however I personally prefer using #regions
    for the same.

    -Cnu

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Using { } blocks to improve readability

      G.S. <gstoynev@gmail .comwrote:
      Is it acceptable to use { } blocks to improve readability and
      maintainability of the code?
      >
      Example:
      >
      //add the checkbox column
      DataGridViewChe ckBoxColumn column = new DataGridViewChe ckBoxColumn();
      {
      column.HeaderTe xt = "Include";
      column.Name = "IsUsedInNotifi cation";
      column.FlatStyl e = FlatStyle.Stand ard;
      column.ThreeSta te = false;
      column.ValueTyp e = typeof(bool);
      column.TrueValu e = true;
      column.FalseVal ue = 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:

      DataGridViewChe ckBoxColumn column = new DataGridViewChe ckBoxColumn
      {
      HeaderText = "Include";
      Name = "IsUsedInNotifi cation";
      FlatStyle = FlatStyle.Stand ard;
      ThreeState = false;
      ValueType = typeof(bool);
      TrueValue = true;
      FalseValue = false;
      };

      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com

      Comment

      • Peter Morris

        #4
        Re: Using { } blocks to improve readability

        Ah. I did this some time ago when using XmlTextWriter. I will revise my
        newest code that uses it and put some in :-)

        writer.WriteSta rtElement("thin g");
        {
        writer.WriteAtt ributeString("n ame", "eggs");
        writer.WriteAtt ributeString("s ize", "7");
        writer.WriteSta rtElement("inne rThing");
        {
        etc
        }
        writer.WriteEnd Element();
        }
        writer.WriteEnd Element();


        Not sure where to put the { and }, but it looks better than this


        writer.WriteSta rtElement("thin g");
        writer.WriteAtt ributeString("n ame", "eggs");
        writer.WriteAtt ributeString("s ize", "7");
        writer.WriteSta rtElement("inne rThing");
        etc
        writer.WriteEnd Element();
        writer.WriteEnd Element();


        Would be great if WriteStartEleme nt returned an IDisposable wouldn't it.



        Pete

        Comment

        • G.S.

          #5
          Re: Using { } blocks to improve readability

          On Sep 23, 3:54 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
          G.S. <gstoy...@gmail .comwrote:
          Is it acceptable to use { } blocks to improve readability and
          maintainability of the code?
          >
          Example:
          >
          //add the checkbox column
          DataGridViewChe ckBoxColumn column = new DataGridViewChe ckBoxColumn();
          {
             column.HeaderTe xt = "Include";
             column.Name = "IsUsedInNotifi cation";
             column.FlatStyl e = FlatStyle.Stand ard;
             column.ThreeSta te = false;
             column.ValueTyp e = typeof(bool);
             column.TrueValu e = true;
             column.FalseVal ue = 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:
          >
          DataGridViewChe ckBoxColumn column = new DataGridViewChe ckBoxColumn
          {
              HeaderText = "Include";
              Name = "IsUsedInNotifi cation";
              FlatStyle = FlatStyle.Stand ard;
              ThreeState = false;
              ValueType = typeof(bool);
              TrueValue = true;
              FalseValue = false;
          >
          };
          >
          --
          Jon Skeet - <sk...@pobox.co m>
          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

          Comment

          • Ignacio Machin ( .NET/ C# MVP )

            #6
            Re: Using { } blocks to improve readability

            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
            DataGridViewChe ckBoxColumn column = new DataGridViewChe ckBoxColumn();
            {
            column.HeaderTe xt = "Include";
            column.Name = "IsUsedInNotifi cation";
            column.FlatStyl e = FlatStyle.Stand ard;
            column.ThreeSta te = false;
            column.ValueTyp e = typeof(bool);
            column.TrueValu e = true;
            column.FalseVal ue = 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;
            }


            Comment

            • Brian Gideon

              #7
              Re: Using { } blocks to improve readability

              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
              DataGridViewChe ckBoxColumn column = new DataGridViewChe ckBoxColumn();
              {
                      column.HeaderTe xt = "Include";
                      column.Name = "IsUsedInNotifi cation";
                      column.FlatStyl e = FlatStyle.Stand ard;
                      column.ThreeSta te = false;
                      column.ValueTyp e = typeof(bool);
                      column.TrueValu e = true;
                      column.FalseVal ue = 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.

              Comment

              • Peter Morris

                #8
                Re: Using { } blocks to improve readability

                Thanks to your reminder I have now just added the following class to a
                common assembly....


                public static class XmlWriterHelper
                {
                public static IDisposable StartElement(th is XmlWriter writer, string
                elementName)
                {
                return new DisposableEleme ntWriter(writer , elementName);
                }

                #region IDisposableElem entWriter
                private class DisposableEleme ntWriter : IDisposable
                {
                private XmlWriter Writer;

                public DisposableEleme ntWriter(XmlWri ter writer, string elementName)
                {
                Writer = writer;
                Writer.WriteSta rtElement(eleme ntName);
                }

                public void Dispose()
                {
                Writer.WriteEnd Element();
                }

                }
                #endregion
                }


                Now I can write code like this
                using (writer.StartEl ement("data"))
                {
                writer.WriteAtt ributeString("1 ", "1");
                writer.WriteAtt ributeString("2 ", "2");
                writer.WriteAtt ributeString("3 ", "3");
                using (writer.StartEl ement("systemDa ta"))
                {
                writer.WriteAtt ributeString("a ", "a");
                writer.WriteAtt ributeString("b ", "b");
                }//systemData
                }//data

                Which is easier to read than this:


                writer.WriteSta rtElement("data ");
                writer.WriteAtt ributeString("1 ", "1");
                writer.WriteAtt ributeString("2 ", "2");
                writer.WriteAtt ributeString("3 ", "3");

                writer.WriteSta rtElement("syst emData");
                writer.WriteAtt ributeString("a ", "a");
                writer.WriteAtt ributeString("b ", "b");
                writer.WriteEnd Element();//systemData

                writer.WriteEnd Element();//data

                Comment

                • Marc Gravell

                  #9
                  Re: Using { } blocks to improve readability

                  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("syste mData",
                  new XAttribute("a", "a"),
                  new XAttribute("b", "b")
                  )
                  );

                  Different approach, but a similar readability improvement.

                  Marc

                  Comment

                  • Peter Morris

                    #10
                    Re: Using { } blocks to improve readability

                    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

                    Comment

                    • Mythran

                      #11
                      Re: Using { } blocks to improve readability



                      "Peter Morris" <mrpmorrisNO@SP AMgmail.comwrot e in message
                      news:ujalZmbHJH A.2408@TK2MSFTN GP04.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.WriteSta rtElement("thin g");
                      {
                      writer.WriteAtt ributeString("n ame", "eggs");
                      writer.WriteAtt ributeString("s ize", "7");
                      writer.WriteSta rtElement("inne rThing");
                      {
                      etc
                      }
                      writer.WriteEnd Element();
                      }
                      writer.WriteEnd Element();
                      >
                      >
                      Not sure where to put the { and }, but it looks better than this
                      >
                      >
                      writer.WriteSta rtElement("thin g");
                      writer.WriteAtt ributeString("n ame", "eggs");
                      writer.WriteAtt ributeString("s ize", "7");
                      writer.WriteSta rtElement("inne rThing");
                      etc
                      writer.WriteEnd Element();
                      writer.WriteEnd Element();
                      >
                      >
                      Would be great if WriteStartEleme nt 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.WriteSta rtElement("thin g");
                      writer.WriteAtt ributeString("n ame", "eggs");
                      writer.WriteAtt ributeString("s ize", "7");

                      // Write the innerThing child element and it's attributes.
                      writer.WriteSta rtElement("inne rThing");
                      writer.WriteAtt ributeString("n ame", "chickens") ;
                      writer.WriteAtt ributeString("s ize", "0.80"); // has head cut off

                      etc...

                      // Write the closing of the innerThing element.
                      writer.WriteEnd Element();

                      // Write the closing of the thing element.
                      writer.WriteEnd Element();



                      Doing it like the above helps keep code documented....a nd uses up less
                      screen space (due to excluding the extra indentations).. .

                      HTH,
                      Mythran


                      Comment

                      • Peter Morris

                        #12
                        Re: Using { } blocks to improve readability

                        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.StartEl ement("data"))
                        {
                        writer.WriteAtt ributeString("1 ", "1");
                        writer.WriteAtt ributeString("2 ", "2");
                        writer.WriteAtt ributeString("3 ", "3");
                        using (writer.StartEl ement("systemDa ta"))
                        {
                        writer.WriteAtt ributeString("a ", "a");
                        writer.WriteAtt ributeString("b ", "b");
                        }//systemData
                        }//data


                        Regards

                        Pete



                        Comment

                        • Mythran

                          #13
                          Re: Using { } blocks to improve readability



                          "Peter Morris" <mrpmorrisNO@SP AMgmail.comwrot e in message
                          news:OySSME0HJH A.1160@TK2MSFTN GP05.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.StartEl ement("data"))
                          {
                          writer.WriteAtt ributeString("1 ", "1");
                          writer.WriteAtt ributeString("2 ", "2");
                          writer.WriteAtt ributeString("3 ", "3");
                          using (writer.StartEl ement("systemDa ta"))
                          {
                          writer.WriteAtt ributeString("a ", "a");
                          writer.WriteAtt ributeString("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


                          Comment

                          Working...