Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. C# code styling

C# code styling

Scheduled Pinned Locked Moved The Lounge
csharpwpfhelpwcflinq
18 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Super Lloyd
    wrote on last edited by
    #1

    apart from method, field and property casing, which I will be the first to admit is purely cosmetic, yet still rubs me the wrong way when a property start with a lower case and a field start with an uppercase.... I still find, 18 years later (after starting working with C# professionally) that all those discussion about where to put bracket, whether or not to use LinQ or not, or whatever, to return at the end of the method or not, so called micro consistency, are all a monumental waste of time. Never ever ever ever had this any impact on how difficult or easy was a codebase to understand... :~ Nothing particular to moan on... except the codebase I am looking at is quite hard to understand... and no amount of "styling" that could fix it, and for some reason I got memory of endless pointless painful discussion about it... I think I have code styling PTSD :sigh: At best, at risk of being pedantic meself, I would say "proper MVVM" could improve it... i.e. data only model and binding only UI. But I find purist so annoying, and I can see that code has history.... That said "proper MVVM", or an approximation of it, is often an improvement toward reducing code complexity.. :D (though I only argue for an approximation of it, I am here to help my colleague and myself, not annoy everyone for little gain..)

    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

    P O Sander RosselS J L 6 Replies Last reply
    0
    • S Super Lloyd

      apart from method, field and property casing, which I will be the first to admit is purely cosmetic, yet still rubs me the wrong way when a property start with a lower case and a field start with an uppercase.... I still find, 18 years later (after starting working with C# professionally) that all those discussion about where to put bracket, whether or not to use LinQ or not, or whatever, to return at the end of the method or not, so called micro consistency, are all a monumental waste of time. Never ever ever ever had this any impact on how difficult or easy was a codebase to understand... :~ Nothing particular to moan on... except the codebase I am looking at is quite hard to understand... and no amount of "styling" that could fix it, and for some reason I got memory of endless pointless painful discussion about it... I think I have code styling PTSD :sigh: At best, at risk of being pedantic meself, I would say "proper MVVM" could improve it... i.e. data only model and binding only UI. But I find purist so annoying, and I can see that code has history.... That said "proper MVVM", or an approximation of it, is often an improvement toward reducing code complexity.. :D (though I only argue for an approximation of it, I am here to help my colleague and myself, not annoy everyone for little gain..)

      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Personally, I use lowercase for local and private members and Pascal case for anything externally visible -- including parameter names. Just to piss people off.

      1 Reply Last reply
      0
      • S Super Lloyd

        apart from method, field and property casing, which I will be the first to admit is purely cosmetic, yet still rubs me the wrong way when a property start with a lower case and a field start with an uppercase.... I still find, 18 years later (after starting working with C# professionally) that all those discussion about where to put bracket, whether or not to use LinQ or not, or whatever, to return at the end of the method or not, so called micro consistency, are all a monumental waste of time. Never ever ever ever had this any impact on how difficult or easy was a codebase to understand... :~ Nothing particular to moan on... except the codebase I am looking at is quite hard to understand... and no amount of "styling" that could fix it, and for some reason I got memory of endless pointless painful discussion about it... I think I have code styling PTSD :sigh: At best, at risk of being pedantic meself, I would say "proper MVVM" could improve it... i.e. data only model and binding only UI. But I find purist so annoying, and I can see that code has history.... That said "proper MVVM", or an approximation of it, is often an improvement toward reducing code complexity.. :D (though I only argue for an approximation of it, I am here to help my colleague and myself, not annoy everyone for little gain..)

        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

        O Offline
        O Offline
        obermd
        wrote on last edited by
        #3

        I handle discussion of the best way to format code using Ctrl-K, D, which in VS reformats the code to your liking.

        S L 2 Replies Last reply
        0
        • O obermd

          I handle discussion of the best way to format code using Ctrl-K, D, which in VS reformats the code to your liking.

          S Offline
          S Offline
          Super Lloyd
          wrote on last edited by
          #4

          Winner! :)

          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

          1 Reply Last reply
          0
          • S Super Lloyd

            apart from method, field and property casing, which I will be the first to admit is purely cosmetic, yet still rubs me the wrong way when a property start with a lower case and a field start with an uppercase.... I still find, 18 years later (after starting working with C# professionally) that all those discussion about where to put bracket, whether or not to use LinQ or not, or whatever, to return at the end of the method or not, so called micro consistency, are all a monumental waste of time. Never ever ever ever had this any impact on how difficult or easy was a codebase to understand... :~ Nothing particular to moan on... except the codebase I am looking at is quite hard to understand... and no amount of "styling" that could fix it, and for some reason I got memory of endless pointless painful discussion about it... I think I have code styling PTSD :sigh: At best, at risk of being pedantic meself, I would say "proper MVVM" could improve it... i.e. data only model and binding only UI. But I find purist so annoying, and I can see that code has history.... That said "proper MVVM", or an approximation of it, is often an improvement toward reducing code complexity.. :D (though I only argue for an approximation of it, I am here to help my colleague and myself, not annoy everyone for little gain..)

            A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

            Sander RosselS Offline
            Sander RosselS Offline
            Sander Rossel
            wrote on last edited by
            #5

            Super Lloyd wrote:

            apart from method, field and property casing, which I will be the first to admit is purely cosmetic

            Not completely, it helps you make assumptions about your code so you don't have to think about every little detail. Not constantly having to check whether something is a method or variable or whatever, because it's consistently cased, lowers the cognitive load while reading code. Most code is already hard enough to read without you having to worry whether something is a field, variable or property. For example, you could make assumptions about the following code:

            public ReturnTheValue(int a) => a + b + C;

            a is a variable passed to the method, b is probably a private field and C is probably a public property. You'd be surprised if b was the property and C was the field, it'd be a WTF, and as we all know there's only one good metric for code quality... WTFs per minute. So just a little more than cosmetics, I'd argue :)

            Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

            S 1 Reply Last reply
            0
            • S Super Lloyd

              apart from method, field and property casing, which I will be the first to admit is purely cosmetic, yet still rubs me the wrong way when a property start with a lower case and a field start with an uppercase.... I still find, 18 years later (after starting working with C# professionally) that all those discussion about where to put bracket, whether or not to use LinQ or not, or whatever, to return at the end of the method or not, so called micro consistency, are all a monumental waste of time. Never ever ever ever had this any impact on how difficult or easy was a codebase to understand... :~ Nothing particular to moan on... except the codebase I am looking at is quite hard to understand... and no amount of "styling" that could fix it, and for some reason I got memory of endless pointless painful discussion about it... I think I have code styling PTSD :sigh: At best, at risk of being pedantic meself, I would say "proper MVVM" could improve it... i.e. data only model and binding only UI. But I find purist so annoying, and I can see that code has history.... That said "proper MVVM", or an approximation of it, is often an improvement toward reducing code complexity.. :D (though I only argue for an approximation of it, I am here to help my colleague and myself, not annoy everyone for little gain..)

              A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

              J Offline
              J Offline
              Jacquers
              wrote on last edited by
              #6

              But... MVVM means you need a UI framework :P Funny how we don't complain about that with desktop apps :)

              S O 2 Replies Last reply
              0
              • S Super Lloyd

                apart from method, field and property casing, which I will be the first to admit is purely cosmetic, yet still rubs me the wrong way when a property start with a lower case and a field start with an uppercase.... I still find, 18 years later (after starting working with C# professionally) that all those discussion about where to put bracket, whether or not to use LinQ or not, or whatever, to return at the end of the method or not, so called micro consistency, are all a monumental waste of time. Never ever ever ever had this any impact on how difficult or easy was a codebase to understand... :~ Nothing particular to moan on... except the codebase I am looking at is quite hard to understand... and no amount of "styling" that could fix it, and for some reason I got memory of endless pointless painful discussion about it... I think I have code styling PTSD :sigh: At best, at risk of being pedantic meself, I would say "proper MVVM" could improve it... i.e. data only model and binding only UI. But I find purist so annoying, and I can see that code has history.... That said "proper MVVM", or an approximation of it, is often an improvement toward reducing code complexity.. :D (though I only argue for an approximation of it, I am here to help my colleague and myself, not annoy everyone for little gain..)

                A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                J Offline
                J Offline
                Jacquers
                wrote on last edited by
                #7

                I find that consistent code styling (whatever style you use) also helps with code comparisons / merges.

                1 Reply Last reply
                0
                • J Jacquers

                  But... MVVM means you need a UI framework :P Funny how we don't complain about that with desktop apps :)

                  S Offline
                  S Offline
                  Super Lloyd
                  wrote on last edited by
                  #8

                  I rather think of WPF as a UI library. Not a Framework. While Framework force you to work their way to do (arguably new) stuff, UI library empower you to do stuff however you like! :D

                  A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                  1 Reply Last reply
                  0
                  • Sander RosselS Sander Rossel

                    Super Lloyd wrote:

                    apart from method, field and property casing, which I will be the first to admit is purely cosmetic

                    Not completely, it helps you make assumptions about your code so you don't have to think about every little detail. Not constantly having to check whether something is a method or variable or whatever, because it's consistently cased, lowers the cognitive load while reading code. Most code is already hard enough to read without you having to worry whether something is a field, variable or property. For example, you could make assumptions about the following code:

                    public ReturnTheValue(int a) => a + b + C;

                    a is a variable passed to the method, b is probably a private field and C is probably a public property. You'd be surprised if b was the property and C was the field, it'd be a WTF, and as we all know there's only one good metric for code quality... WTFs per minute. So just a little more than cosmetics, I'd argue :)

                    Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

                    S Offline
                    S Offline
                    Super Lloyd
                    wrote on last edited by
                    #9

                    Ok, ok, it's a bit more than just cosmetic. Lucky me, that's the only code style I find worth enforcing! :D

                    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                    1 Reply Last reply
                    0
                    • S Super Lloyd

                      apart from method, field and property casing, which I will be the first to admit is purely cosmetic, yet still rubs me the wrong way when a property start with a lower case and a field start with an uppercase.... I still find, 18 years later (after starting working with C# professionally) that all those discussion about where to put bracket, whether or not to use LinQ or not, or whatever, to return at the end of the method or not, so called micro consistency, are all a monumental waste of time. Never ever ever ever had this any impact on how difficult or easy was a codebase to understand... :~ Nothing particular to moan on... except the codebase I am looking at is quite hard to understand... and no amount of "styling" that could fix it, and for some reason I got memory of endless pointless painful discussion about it... I think I have code styling PTSD :sigh: At best, at risk of being pedantic meself, I would say "proper MVVM" could improve it... i.e. data only model and binding only UI. But I find purist so annoying, and I can see that code has history.... That said "proper MVVM", or an approximation of it, is often an improvement toward reducing code complexity.. :D (though I only argue for an approximation of it, I am here to help my colleague and myself, not annoy everyone for little gain..)

                      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                      L Offline
                      L Offline
                      lmoelleb
                      wrote on last edited by
                      #10

                      Consistency helps a bit when reading code, but it comes with two drawbacks in my view: Endless discussion and code review focusing on formatting while no-one notice the structure of the code is complete crap. We did find a solution to this in our team. A bit too heavy impact to apply in old projects but for anything new we do it from the start: Analyzers. We use Stylecop and NETAnalyzers (or whatever fxcop was renamed to.... or do we still have FXCop and it was Stylecop renamed? Who knows? Who cares?) and a few others. We suppress some things (no file headers, no requirement to document public methods in anything but public APIs). There is no strict rule for what we suppress, but any suppression also goes through code review, and typically "this adds a lot of work and no benefit" will be accepted while "it looks better this way" is unlikely to get through - at least as a global suppression. In the start there is the usual complains about prefixing with "this." and placing using statements inside the namespace declaration. Luckily there is a solution: Simply say "sure, we can change it if you don't like it, just try doing it 3-6 months then we will discuss what we want to change. Most will live with this "mandate" form a senior. After all, in 3-6 months they will have the discussion and they can get this stupidity changed.... Then after 3-6 months, they realized it just doesn't matter and don't want to change anything. Set the release build to treat warnings like errors - and force a build on merge to "develop" or whatever branch you use and formatting is "just taken care of" by tools as it should be.

                      realJSOPR 1 Reply Last reply
                      0
                      • O obermd

                        I handle discussion of the best way to format code using Ctrl-K, D, which in VS reformats the code to your liking.

                        L Offline
                        L Offline
                        lmoelleb
                        wrote on last edited by
                        #11

                        And then you have two developers with different settings at war with each other - and I have to code review the code going back and forth... and try to track actual changes... no thanks. :)

                        realJSOPR 1 Reply Last reply
                        0
                        • L lmoelleb

                          And then you have two developers with different settings at war with each other - and I have to code review the code going back and forth... and try to track actual changes... no thanks. :)

                          realJSOPR Offline
                          realJSOPR Offline
                          realJSOP
                          wrote on last edited by
                          #12
                          1. If I'm editing an existing file, I follow the formatting already in that file. If I'm creating a new file - in the absence of established corporate coding standards - I use my own style. 1) I never reformat a file to my style because source control would mark every line as being changed when I checked it back in. 2) I always put accessors on private elements. 3) I always use this., or in the case of static classes, I use ClassName. to denote local class members. 4) I use lowercase chars to start field/method variable names, and camel-case them if necessary. 5) I start class, property, and method names with uppercase chars, and camel-case them if necessary. 6) I put curly braces on their own lines. 7) I'm a fanatic for code alignment. 8) I use LINQ when it makes sense to do so, mostly because it cleans up the code so well without loosing fidelity. 9) I "keep tabs". After I demonstrated why to the rest of the team, they agree it's the best way to go. We have one guy that was doing 5 spaces instead of four when he hit the tab key. This helped him immensely because VS uses five spaces for him, and four for the rest of us. 10) I comment pretty much everything, at least the "why" of it. Fortunately, I'm currently in a position where I can dictate styling for my team. Even more fortunate, they already used pretty much the same styling techniques.

                          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                          -----
                          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                          -----
                          When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                          L 1 Reply Last reply
                          0
                          • L lmoelleb

                            Consistency helps a bit when reading code, but it comes with two drawbacks in my view: Endless discussion and code review focusing on formatting while no-one notice the structure of the code is complete crap. We did find a solution to this in our team. A bit too heavy impact to apply in old projects but for anything new we do it from the start: Analyzers. We use Stylecop and NETAnalyzers (or whatever fxcop was renamed to.... or do we still have FXCop and it was Stylecop renamed? Who knows? Who cares?) and a few others. We suppress some things (no file headers, no requirement to document public methods in anything but public APIs). There is no strict rule for what we suppress, but any suppression also goes through code review, and typically "this adds a lot of work and no benefit" will be accepted while "it looks better this way" is unlikely to get through - at least as a global suppression. In the start there is the usual complains about prefixing with "this." and placing using statements inside the namespace declaration. Luckily there is a solution: Simply say "sure, we can change it if you don't like it, just try doing it 3-6 months then we will discuss what we want to change. Most will live with this "mandate" form a senior. After all, in 3-6 months they will have the discussion and they can get this stupidity changed.... Then after 3-6 months, they realized it just doesn't matter and don't want to change anything. Set the release build to treat warnings like errors - and force a build on merge to "develop" or whatever branch you use and formatting is "just taken care of" by tools as it should be.

                            realJSOPR Offline
                            realJSOPR Offline
                            realJSOP
                            wrote on last edited by
                            #13

                            lmoelleb wrote:

                            Endless discussion and code review focusing on formatting while no-one notice the structure of the code is complete crap.

                            We don't talk about formatting in our code reviews, unless the format of the target code does not follow the formatting in the rest of the file.

                            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                            -----
                            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                            -----
                            When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                            L 1 Reply Last reply
                            0
                            • realJSOPR realJSOP

                              lmoelleb wrote:

                              Endless discussion and code review focusing on formatting while no-one notice the structure of the code is complete crap.

                              We don't talk about formatting in our code reviews, unless the format of the target code does not follow the formatting in the rest of the file.

                              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                              -----
                              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                              -----
                              When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                              L Offline
                              L Offline
                              lmoelleb
                              wrote on last edited by
                              #14

                              So if I understand you correct: You check (and if needed talk about) formatting in the code reviews. Of course, once developers get used to following whatever you have it is not often it has to be brought up - we work that way in the old code as well. It's just so much easier in the new code. It's trivial work, exactly the thing computers where invented to do.

                              realJSOPR 1 Reply Last reply
                              0
                              • realJSOPR realJSOP
                                1. If I'm editing an existing file, I follow the formatting already in that file. If I'm creating a new file - in the absence of established corporate coding standards - I use my own style. 1) I never reformat a file to my style because source control would mark every line as being changed when I checked it back in. 2) I always put accessors on private elements. 3) I always use this., or in the case of static classes, I use ClassName. to denote local class members. 4) I use lowercase chars to start field/method variable names, and camel-case them if necessary. 5) I start class, property, and method names with uppercase chars, and camel-case them if necessary. 6) I put curly braces on their own lines. 7) I'm a fanatic for code alignment. 8) I use LINQ when it makes sense to do so, mostly because it cleans up the code so well without loosing fidelity. 9) I "keep tabs". After I demonstrated why to the rest of the team, they agree it's the best way to go. We have one guy that was doing 5 spaces instead of four when he hit the tab key. This helped him immensely because VS uses five spaces for him, and four for the rest of us. 10) I comment pretty much everything, at least the "why" of it. Fortunately, I'm currently in a position where I can dictate styling for my team. Even more fortunate, they already used pretty much the same styling techniques.

                                ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                -----
                                You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                -----
                                When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                                L Offline
                                L Offline
                                lmoelleb
                                wrote on last edited by
                                #15

                                I am in a position where I could dictate, but I certainly do not want to if I can avoid it - I try to nudge people in the right direction when possible. For the rest: yes, you use pretty average code standards like we all do. With some variations like most have. I had to change a thing or two to match the tools, but then - if it was important I could have changed the tool settings - but it was not important.

                                1 Reply Last reply
                                0
                                • J Jacquers

                                  But... MVVM means you need a UI framework :P Funny how we don't complain about that with desktop apps :)

                                  O Offline
                                  O Offline
                                  obermd
                                  wrote on last edited by
                                  #16

                                  Frameworks for desktop applications are just as bad.

                                  1 Reply Last reply
                                  0
                                  • L lmoelleb

                                    So if I understand you correct: You check (and if needed talk about) formatting in the code reviews. Of course, once developers get used to following whatever you have it is not often it has to be brought up - we work that way in the old code as well. It's just so much easier in the new code. It's trivial work, exactly the thing computers where invented to do.

                                    realJSOPR Offline
                                    realJSOPR Offline
                                    realJSOP
                                    wrote on last edited by
                                    #17

                                    No we don't make it part of the code review itself. If the formatting is way off, we mention it at the end of the review, but it's not a "topic of discussion".

                                    ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                    -----
                                    You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                    -----
                                    When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                                    L 1 Reply Last reply
                                    0
                                    • realJSOPR realJSOP

                                      No we don't make it part of the code review itself. If the formatting is way off, we mention it at the end of the review, but it's not a "topic of discussion".

                                      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                      -----
                                      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                      -----
                                      When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                                      L Offline
                                      L Offline
                                      lmoelleb
                                      wrote on last edited by
                                      #18

                                      Fair enough, we just use different wordings. Something that is checked and potentially mentioned (even briefly) I see as part of the review, but you do not. In the end I think we in practice do the same thing.

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups