C# coding style question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruno Jouhier [MVP]

    #31
    Re: C# coding style question

    "Jon Skeet [C# MVP]" <skeet@pobox.co m> a écrit dans le message de
    news:MPG.1af2d8 6e258d03c198a73 8@msnews.micros oft.com...[color=blue]
    > Bruno Jouhier [MVP] <bjouhier@clu b-internet.fr> wrote:[color=green]
    > > * it is fairly common. I have seen it in a number of places, including[/color][/color]
    ..NET[color=blue][color=green]
    > > source code that MS made available publicly.[/color]
    >
    > Unfortunately that's the case for any number of conventions. Just
    > looking at some of the Rotor classes, it's easy to see:
    >
    > m_memberName (eg Char)
    > _memberName (eg Console)
    > memberName (eg DateTime)[/color]

    Good point :-)
    Bruno.
    [color=blue]
    >
    > --
    > Jon Skeet - <skeet@pobox.co m>
    > http://www.pobox.com/~skeet
    > If replying to the group, please do not mail me too[/color]


    Comment

    • Daniel O'Connell [C# MVP]

      #32
      Re: C# coding style question


      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.1af2d8 6e258d03c198a73 8@msnews.micros oft.com...[color=blue]
      > Bruno Jouhier [MVP] <bjouhier@clu b-internet.fr> wrote:[color=green]
      >> * it is fairly common. I have seen it in a number of places, including
      >> .NET
      >> source code that MS made available publicly.[/color]
      >
      > Unfortunately that's the case for any number of conventions. Just
      > looking at some of the Rotor classes, it's easy to see:
      >
      > m_memberName (eg Char)
      > _memberName (eg Console)
      > memberName (eg DateTime)
      >[/color]
      Part of waht makes rotor interesting is that it was written while the
      platform evolved, obviously. I wonder if we were to take the classes with a
      given convention and determine when they were written if a pattern would
      emerge or if it is entirely based on *who* wrote it.[color=blue]
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Jon Skeet [C# MVP]

        #33
        Re: C# coding style question

        Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:[color=blue]
        > Part of waht makes rotor interesting is that it was written while the
        > platform evolved, obviously. I wonder if we were to take the classes with a
        > given convention and determine when they were written if a pattern would
        > emerge or if it is entirely based on *who* wrote it.[/color]

        I suspect it really is just a case of who wrote it. I'm pretty sure
        I've seen a mixture of styles in MSDN too.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Daniel O'Connell [C# MVP]

          #34
          Re: C# coding style question


          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.1af2dd 28635bda7298a73 a@msnews.micros oft.com...[color=blue]
          > Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:[color=green]
          >> Part of waht makes rotor interesting is that it was written while the
          >> platform evolved, obviously. I wonder if we were to take the classes with
          >> a
          >> given convention and determine when they were written if a pattern would
          >> emerge or if it is entirely based on *who* wrote it.[/color]
          >
          > I suspect it really is just a case of who wrote it. I'm pretty sure
          > I've seen a mixture of styles in MSDN too.[/color]
          I have as well, however it isn't certain as to who wrote them or when.
          A*lot* of the m_ prefixed code I see is sourced from people who either
          havn't given up C++ yet or are just starting on it. I suppose its a habit
          that is hard to break if you've been using it for years.
          I still see m_ prefixed code in MSDN mag now and then, infact I think the
          newest issue had a C# 2.0 example using the m_ prefix(which irks me to no
          end...IMHO, someone who is writing for a magazine or other publication
          should atleast try to follow the widest convention, which I think is no
          prefix at all).

          I am curious about the trends primarily because of the age of the source
          base and the fact that it was written while the transition between hungarian
          and camel\Pascal casing was coming into the framework, the earliest files in
          rotor were probably written 5 or 6 years ago and I do wonder if there are
          trends that can be seen...perhaps something like a code 'Sociology'.
          Conventions and the code written are, after all, in many cases social
          things, probably prone to the same forces that affect fashion and
          culture...just on a considerably smaller scale.

          I dunno, its pretty late and I'm probably rambling and not realizing it at
          this point. I think I'll end this here.
          [color=blue]
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too[/color]


          Comment

          • Sharon

            #35
            Re: C# coding style question

            I agree that we all should use conventions.
            But who made Dot Net convention anyway?
            Who decided to remove prefixes? and why?
            I like prefixes.
            They make the code more clear.
            They identify the scope of a field, instantly.
            Ugly or not is a matter of opinion.


            "Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
            message news:eDpZTzQKEH A.1312@TK2MSFTN GP12.phx.gbl...[color=blue]
            >
            > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
            > news:MPG.1af2dd 28635bda7298a73 a@msnews.micros oft.com...[color=green]
            > > Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:[color=darkred]
            > >> Part of waht makes rotor interesting is that it was written while the
            > >> platform evolved, obviously. I wonder if we were to take the classes[/color][/color][/color]
            with[color=blue][color=green][color=darkred]
            > >> a
            > >> given convention and determine when they were written if a pattern[/color][/color][/color]
            would[color=blue][color=green][color=darkred]
            > >> emerge or if it is entirely based on *who* wrote it.[/color]
            > >
            > > I suspect it really is just a case of who wrote it. I'm pretty sure
            > > I've seen a mixture of styles in MSDN too.[/color]
            > I have as well, however it isn't certain as to who wrote them or when.
            > A*lot* of the m_ prefixed code I see is sourced from people who either
            > havn't given up C++ yet or are just starting on it. I suppose its a habit
            > that is hard to break if you've been using it for years.
            > I still see m_ prefixed code in MSDN mag now and then, infact I think the
            > newest issue had a C# 2.0 example using the m_ prefix(which irks me to no
            > end...IMHO, someone who is writing for a magazine or other publication
            > should atleast try to follow the widest convention, which I think is no
            > prefix at all).
            >
            > I am curious about the trends primarily because of the age of the source
            > base and the fact that it was written while the transition between[/color]
            hungarian[color=blue]
            > and camel\Pascal casing was coming into the framework, the earliest files[/color]
            in[color=blue]
            > rotor were probably written 5 or 6 years ago and I do wonder if there are
            > trends that can be seen...perhaps something like a code 'Sociology'.
            > Conventions and the code written are, after all, in many cases social
            > things, probably prone to the same forces that affect fashion and
            > culture...just on a considerably smaller scale.
            >
            > I dunno, its pretty late and I'm probably rambling and not realizing it at
            > this point. I think I'll end this here.
            >[color=green]
            > >
            > > --
            > > Jon Skeet - <skeet@pobox.co m>
            > > http://www.pobox.com/~skeet
            > > If replying to the group, please do not mail me too[/color]
            >
            >[/color]


            Comment

            • Bruno Jouhier [MVP]

              #36
              Re: C# coding style question


              "Sharon" <talsharon@hotm ail.com> a écrit dans le message de
              news:O8bNJGVKEH A.3436@tk2msftn gp13.phx.gbl...[color=blue]
              > I agree that we all should use conventions.[/color]

              I agree too, even if they are not the ones we would have chosen...
              [color=blue]
              > But who made Dot Net convention anyway?[/color]

              Microsoft did it.

              [color=blue]
              > Who decided to remove prefixes? and why?
              > I like prefixes.[/color]

              If you are talking about the famous Hungarian notations, you will find lots
              of contradictors, and I'll be one of them. If you are talking about a single
              underscore prefix on private fields, you will get some support (mine
              included).
              [color=blue]
              > They make the code more clear.
              > They identify the scope of a field, instantly.[/color]

              There is a rather widespread agreement on the fact that fields should always
              be private. If you follow this rule, there is no need to identify the scope
              of fields! Which does not mean that it is not interesting to differentiate
              fields from parameters and local variables, with a prefix (I support this
              view), but this is different from "scope of fields".
              [color=blue]
              > Ugly or not is a matter of opinion.[/color]

              Yes, but you are being subjective too when you say "I like prefixes". The
              people who define the "convention s" have to take a subjective stand at some
              point, because they are dealing with complex issues that include "liking or
              not", "looking nice or ugly". And they have decided to remove prefixes from
              all non private APIs (with a few exceptions, for example the I prefix on
              interfaces). They did not fully specify what you should do with private
              stuff, so there is some room for debate around prefix vs. no prefix here.
              Also, there seems to be limited room for prefix on class names. For example,
              all the XML related classes are prefixed by Xml in the framework, which
              makes a lot of sense (more than calling them just Document, Element, Node,
              etc., which is what the basic naming rules seem to favor).

              Bruno.
              [color=blue]
              >
              >
              > "Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
              > message news:eDpZTzQKEH A.1312@TK2MSFTN GP12.phx.gbl...[color=green]
              > >
              > > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
              > > news:MPG.1af2dd 28635bda7298a73 a@msnews.micros oft.com...[color=darkred]
              > > > Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
              > > >> Part of waht makes rotor interesting is that it was written while the
              > > >> platform evolved, obviously. I wonder if we were to take the classes[/color][/color]
              > with[color=green][color=darkred]
              > > >> a
              > > >> given convention and determine when they were written if a pattern[/color][/color]
              > would[color=green][color=darkred]
              > > >> emerge or if it is entirely based on *who* wrote it.
              > > >
              > > > I suspect it really is just a case of who wrote it. I'm pretty sure
              > > > I've seen a mixture of styles in MSDN too.[/color]
              > > I have as well, however it isn't certain as to who wrote them or when.
              > > A*lot* of the m_ prefixed code I see is sourced from people who either
              > > havn't given up C++ yet or are just starting on it. I suppose its a[/color][/color]
              habit[color=blue][color=green]
              > > that is hard to break if you've been using it for years.
              > > I still see m_ prefixed code in MSDN mag now and then, infact I think[/color][/color]
              the[color=blue][color=green]
              > > newest issue had a C# 2.0 example using the m_ prefix(which irks me to[/color][/color]
              no[color=blue][color=green]
              > > end...IMHO, someone who is writing for a magazine or other publication
              > > should atleast try to follow the widest convention, which I think is no
              > > prefix at all).
              > >
              > > I am curious about the trends primarily because of the age of the source
              > > base and the fact that it was written while the transition between[/color]
              > hungarian[color=green]
              > > and camel\Pascal casing was coming into the framework, the earliest[/color][/color]
              files[color=blue]
              > in[color=green]
              > > rotor were probably written 5 or 6 years ago and I do wonder if there[/color][/color]
              are[color=blue][color=green]
              > > trends that can be seen...perhaps something like a code 'Sociology'.
              > > Conventions and the code written are, after all, in many cases social
              > > things, probably prone to the same forces that affect fashion and
              > > culture...just on a considerably smaller scale.
              > >
              > > I dunno, its pretty late and I'm probably rambling and not realizing it[/color][/color]
              at[color=blue][color=green]
              > > this point. I think I'll end this here.
              > >[color=darkred]
              > > >
              > > > --
              > > > Jon Skeet - <skeet@pobox.co m>
              > > > http://www.pobox.com/~skeet
              > > > If replying to the group, please do not mail me too[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Joerg Jooss

                #37
                Re: C# coding style question

                Patrick wrote:[color=blue]
                > The c# code style guide that I follow suggests that class variables
                > (fields) be coded with camel casing, like this:
                >
                > int recordId;
                > string name;
                >
                > It also suggests that variables within methods and method parameters
                > use camel casing, like this:
                >
                > void SetName(int id, string newName)
                > {
                > recordId = id;
                > name = newName;
                > return;
                > }
                >
                > This is all fine and dandy, but some of my methods are getting a bit
                > difficult to read. It's hard to differentiate between class fields
                > (that have a class-wide scope) and method variables (that have a
                > method-wide scope). I've started to use the "this" keyword with class
                > variables, example:
                >
                > void SetName(int recordId, string name)
                > {
                > this.recordId = recordId;
                > this.name = name;
                > }
                >
                > BUT, when you do that, you start to get really long lines of code.
                > Here's a real line of my code that exhibits this problem:
                >
                > this.currencyMa nager = (CurrencyManage r)
                > this.grdPerson. BindingContext[this.dataView];
                >
                > I'd love to know what other c# developers do.[/color]

                I use "this", but only for accessing fields, and add a property for each
                field that needs to be exposed to clients. It all boils down to

                class Record {
                private int id;
                private string name;

                public int Id {
                get { return this.id; }
                set { this.id = value; }
                }

                public string Name {
                get { return this.name; }
                set { this.name = value; }
                }

                public override string ToString() {
                return String.Format(" {0} [{1}]", Name, Id);
                }
                }

                Thus, "this" will only appear in property definitions or in methods that
                access strictly private attributes (i.e. that are not to be exposed using
                properties).

                Cheers,

                --
                Joerg Jooss
                joerg.jooss@gmx .net

                Comment

                • skc

                  #38
                  Re: C# coding style question

                  I am surprised C# allows a sort of 'duplicate' names within a method. Even
                  though allowed, my contention is that parameters should be distinguished
                  from variables, so that we can easily tell straightway which is which. it's
                  unlikely then, we will make mistake in this area even if we switch our minds
                  to "lazy" mode. Since you pointed out the webpage on the guideline (and
                  I am reading it for the first time) that parameters shouldn't have prefix, I
                  can accept that. In which case, it's private variables then that must have
                  a prefix, or like you suggest an underscore.

                  Then again, for those who don't intend to sell or give away their classes,
                  I guess it doesn't matter that much..... :-). yea, I know it's better to
                  do so....


                  Regards
                  Skc




                  Bruno Jouhier [MVP] <bjouhier@clu b-internet.fr> wrote in message
                  news:OdUVdWVKEH A.3316@tk2msftn gp13.phx.gbl...[color=blue]
                  >
                  > "Sharon" <talsharon@hotm ail.com> a écrit dans le message de
                  > news:O8bNJGVKEH A.3436@tk2msftn gp13.phx.gbl...[color=green]
                  > > I agree that we all should use conventions.[/color]
                  >
                  > I agree too, even if they are not the ones we would have chosen...
                  >[color=green]
                  > > But who made Dot Net convention anyway?[/color]
                  >
                  > Microsoft did it.
                  >[/color]

                  ml/cpconnamingguid elines.asp[color=blue]
                  >[color=green]
                  > > Who decided to remove prefixes? and why?
                  > > I like prefixes.[/color]
                  >
                  > If you are talking about the famous Hungarian notations, you will find[/color]
                  lots[color=blue]
                  > of contradictors, and I'll be one of them. If you are talking about a[/color]
                  single[color=blue]
                  > underscore prefix on private fields, you will get some support (mine
                  > included).
                  >[color=green]
                  > > They make the code more clear.
                  > > They identify the scope of a field, instantly.[/color]
                  >
                  > There is a rather widespread agreement on the fact that fields should[/color]
                  always[color=blue]
                  > be private. If you follow this rule, there is no need to identify the[/color]
                  scope[color=blue]
                  > of fields! Which does not mean that it is not interesting to differentiate
                  > fields from parameters and local variables, with a prefix (I support this
                  > view), but this is different from "scope of fields".
                  >[color=green]
                  > > Ugly or not is a matter of opinion.[/color]
                  >
                  > Yes, but you are being subjective too when you say "I like prefixes". The
                  > people who define the "convention s" have to take a subjective stand at[/color]
                  some[color=blue]
                  > point, because they are dealing with complex issues that include "liking[/color]
                  or[color=blue]
                  > not", "looking nice or ugly". And they have decided to remove prefixes[/color]
                  from[color=blue]
                  > all non private APIs (with a few exceptions, for example the I prefix on
                  > interfaces). They did not fully specify what you should do with private
                  > stuff, so there is some room for debate around prefix vs. no prefix here.
                  > Also, there seems to be limited room for prefix on class names. For[/color]
                  example,[color=blue]
                  > all the XML related classes are prefixed by Xml in the framework, which
                  > makes a lot of sense (more than calling them just Document, Element, Node,
                  > etc., which is what the basic naming rules seem to favor).
                  >
                  > Bruno.
                  >[color=green]
                  > >
                  > >
                  > > "Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
                  > > message news:eDpZTzQKEH A.1312@TK2MSFTN GP12.phx.gbl...[color=darkred]
                  > > >
                  > > > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                  > > > news:MPG.1af2dd 28635bda7298a73 a@msnews.micros oft.com...
                  > > > > Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
                  > > > >> Part of waht makes rotor interesting is that it was written while[/color][/color][/color]
                  the[color=blue][color=green][color=darkred]
                  > > > >> platform evolved, obviously. I wonder if we were to take the[/color][/color][/color]
                  classes[color=blue][color=green]
                  > > with[color=darkred]
                  > > > >> a
                  > > > >> given convention and determine when they were written if a pattern[/color]
                  > > would[color=darkred]
                  > > > >> emerge or if it is entirely based on *who* wrote it.
                  > > > >
                  > > > > I suspect it really is just a case of who wrote it. I'm pretty sure
                  > > > > I've seen a mixture of styles in MSDN too.
                  > > > I have as well, however it isn't certain as to who wrote them or when.
                  > > > A*lot* of the m_ prefixed code I see is sourced from people who either
                  > > > havn't given up C++ yet or are just starting on it. I suppose its a[/color][/color]
                  > habit[color=green][color=darkred]
                  > > > that is hard to break if you've been using it for years.
                  > > > I still see m_ prefixed code in MSDN mag now and then, infact I think[/color][/color]
                  > the[color=green][color=darkred]
                  > > > newest issue had a C# 2.0 example using the m_ prefix(which irks me to[/color][/color]
                  > no[color=green][color=darkred]
                  > > > end...IMHO, someone who is writing for a magazine or other publication
                  > > > should atleast try to follow the widest convention, which I think is[/color][/color][/color]
                  no[color=blue][color=green][color=darkred]
                  > > > prefix at all).
                  > > >
                  > > > I am curious about the trends primarily because of the age of the[/color][/color][/color]
                  source[color=blue][color=green][color=darkred]
                  > > > base and the fact that it was written while the transition between[/color]
                  > > hungarian[color=darkred]
                  > > > and camel\Pascal casing was coming into the framework, the earliest[/color][/color]
                  > files[color=green]
                  > > in[color=darkred]
                  > > > rotor were probably written 5 or 6 years ago and I do wonder if there[/color][/color]
                  > are[color=green][color=darkred]
                  > > > trends that can be seen...perhaps something like a code 'Sociology'.
                  > > > Conventions and the code written are, after all, in many cases social
                  > > > things, probably prone to the same forces that affect fashion and
                  > > > culture...just on a considerably smaller scale.
                  > > >
                  > > > I dunno, its pretty late and I'm probably rambling and not realizing[/color][/color][/color]
                  it[color=blue]
                  > at[color=green][color=darkred]
                  > > > this point. I think I'll end this here.
                  > > >
                  > > > >
                  > > > > --
                  > > > > Jon Skeet - <skeet@pobox.co m>
                  > > > > http://www.pobox.com/~skeet
                  > > > > If replying to the group, please do not mail me too
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • skc

                    #39
                    Re: C# coding style question


                    Thanks for your agreement and your explanation. Actually, I am surprised
                    somewhat that C# allows this sort of 'duplicate' names within a method.
                    Just read the naming guidelines you mentioned elsewhere (and I am reading it
                    for the first time) that parameters shouldn't have prefix - I can accept
                    that. In which case, it's private variables then that should have a
                    prefix, or like you suggest an _.

                    Then again, for those who don't intend to sell or give away their classes,
                    I guess it shouldn't matter that much whether or not MS standards are
                    followed as long as there is a kind of internal standards within the
                    organisation... .. :-).

                    Regards
                    Skc



                    Comment

                    • skc

                      #40
                      Re: C# coding style question

                      Hullo Bruno

                      Thanks for your agreement and your explanation. Actually, I am surprised
                      somewhat that C# allows this sort of 'duplicate' names within a method. Just
                      read the naming guidelines you mentioned elsewhere (and I am reading it for
                      the first time) that parameters shouldn't have prefix - I can accept that.
                      In which case, it's private variables then that should have a prefix, or
                      like you suggest an _.

                      Then again, for those who don't intend to sell or give away their classes,
                      I guess it shouldn't matter that much whether or not MS standards are
                      followed as long as there is a kind of internal standards within the
                      organisation... .. :-).

                      Regards
                      Skc
                      [ This posting was posted to the wrong place twice and deleted - apologize
                      if anyone sees 3 similar postings ].



                      Comment

                      Working...