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. 10 Commandments

10 Commandments

Scheduled Pinned Locked Moved The Lounge
delphicode-review
70 Posts 39 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.
  • P Pascal Ganaye

    I have been asked to work on our Code Review Procedure. This sort of thing: Pascal casing used for type and method names Camel casing used for local variable names and method arguments ... A single file should only contribute types to a single namespace. Avoid having multiple namespaces in the same file. Avoid files with more than 500 lines (excluding machine-generated code). Avoid methods with more than 25 lines. ... Never hard-code a numeric value, always declare a constant instead. ... I have the feeling it is missing what really make maintenance go bad. Things like re use existing classes, don't duplicate code, separate logic, data access, and display. Can anyone point me to some documents with good recommendations of this type. The idea is to minimize defect and certainly not to alienate the programmers (including me).

    G Offline
    G Offline
    Gary Wheeler
    wrote on last edited by
    #4

    Pascal Ganaye wrote:

    Never hard-code a numeric value, always declare a constant instead

    Be careful with this one; sometimes you get morons who do this:

    const int IntZero = 0;
    const double DblZero = 0.0;
    const int IntOne = 1;
    const double DblOne = 1.0;
    const int IntTwo = 2;
    ...

    Software Zen: delete this;

    P D R 3 Replies Last reply
    0
    • P Pascal Ganaye

      I have been asked to work on our Code Review Procedure. This sort of thing: Pascal casing used for type and method names Camel casing used for local variable names and method arguments ... A single file should only contribute types to a single namespace. Avoid having multiple namespaces in the same file. Avoid files with more than 500 lines (excluding machine-generated code). Avoid methods with more than 25 lines. ... Never hard-code a numeric value, always declare a constant instead. ... I have the feeling it is missing what really make maintenance go bad. Things like re use existing classes, don't duplicate code, separate logic, data access, and display. Can anyone point me to some documents with good recommendations of this type. The idea is to minimize defect and certainly not to alienate the programmers (including me).

      R Offline
      R Offline
      Rama Krishna Vavilala
      wrote on last edited by
      #5

      The things you have mentioned in the first part of you post are already checked by FxCop [^] and StyleCop[^] and can be automated. They may or may not directly impact the number of defects. FxCop to some extent finds certain things which might be problematic. Using StyleCop, on the other hand, you can enforce all the styling. This will prepare you for the next step which is Manual Code Review. Manual Code Review should be to catch subtle things: not protecting shared variables, potential memory leak, better way to do a certain thing (use an API instead of writing code to implement the functionality of API). Code Review can catch lot of subtle bugs and that is why it is so effective. Also, if there are many programmers in your team it is essential that you use automated tool to analyze the style and code before they sit and do a code review. This is because programmers usually get nit-picky of style and miss the more important things. If you have common auto enforceable guidelines, it helps you get to the next step.

      T 1 Reply Last reply
      0
      • G Gary Wheeler

        Pascal Ganaye wrote:

        Never hard-code a numeric value, always declare a constant instead

        Be careful with this one; sometimes you get morons who do this:

        const int IntZero = 0;
        const double DblZero = 0.0;
        const int IntOne = 1;
        const double DblOne = 1.0;
        const int IntTwo = 2;
        ...

        Software Zen: delete this;

        P Offline
        P Offline
        Pascal Ganaye
        wrote on last edited by
        #6

        I personally would prefer Don't hard-code a non trivial numeric value that appears, declare a constant instead. This is particularly true when the value appears more than once.

        S 1 Reply Last reply
        0
        • H Henry Minute

          Here's[^] one from M$. This [^] has links to several alternatives.

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

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

          :wtf: I agree with most of the MS one... how'd that happen? :confused:

          H P 2 Replies Last reply
          0
          • P Pascal Ganaye

            I have been asked to work on our Code Review Procedure. This sort of thing: Pascal casing used for type and method names Camel casing used for local variable names and method arguments ... A single file should only contribute types to a single namespace. Avoid having multiple namespaces in the same file. Avoid files with more than 500 lines (excluding machine-generated code). Avoid methods with more than 25 lines. ... Never hard-code a numeric value, always declare a constant instead. ... I have the feeling it is missing what really make maintenance go bad. Things like re use existing classes, don't duplicate code, separate logic, data access, and display. Can anyone point me to some documents with good recommendations of this type. The idea is to minimize defect and certainly not to alienate the programmers (including me).

            P Offline
            P Offline
            peterchen
            wrote on last edited by
            #8

            Pascal Ganaye wrote:

            Avoid methods with more than 25 lines.

            That seems awfully low. Maybe for this newfangled stuff where you make a color gradient animation in one line of code, and all people goo "ooh" and "aah" and forget to ask what that software is for, but for real work? (admittedly, that's from the guy who has to maintain a 4KLOC switch statement)

            Agh! Reality! My Archnemesis![^]
            | FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server.

            R 1 Reply Last reply
            0
            • G Gary Wheeler

              Pascal Ganaye wrote:

              Never hard-code a numeric value, always declare a constant instead

              Be careful with this one; sometimes you get morons who do this:

              const int IntZero = 0;
              const double DblZero = 0.0;
              const int IntOne = 1;
              const double DblOne = 1.0;
              const int IntTwo = 2;
              ...

              Software Zen: delete this;

              D Offline
              D Offline
              Daniel Grunwald
              wrote on last edited by
              #9

              And after the same morons start doing maintenance, you'll see stuff like:

              const int IntTwo = 3;

              G 1 Reply Last reply
              0
              • D Daniel Grunwald

                And after the same morons start doing maintenance, you'll see stuff like:

                const int IntTwo = 3;

                G Offline
                G Offline
                Gary Wheeler
                wrote on last edited by
                #10

                If I found someone doing that, I would be hard pressed not to nuke their cube from orbit; it's the only way to be sure.

                Software Zen: delete this;

                1 Reply Last reply
                0
                • P Pascal Ganaye

                  I have been asked to work on our Code Review Procedure. This sort of thing: Pascal casing used for type and method names Camel casing used for local variable names and method arguments ... A single file should only contribute types to a single namespace. Avoid having multiple namespaces in the same file. Avoid files with more than 500 lines (excluding machine-generated code). Avoid methods with more than 25 lines. ... Never hard-code a numeric value, always declare a constant instead. ... I have the feeling it is missing what really make maintenance go bad. Things like re use existing classes, don't duplicate code, separate logic, data access, and display. Can anyone point me to some documents with good recommendations of this type. The idea is to minimize defect and certainly not to alienate the programmers (including me).

                  D Offline
                  D Offline
                  dan sh
                  wrote on last edited by
                  #11

                  Look into tools like FxCop, StyleCop and ReSharper. These should take care of formatting and redundant code and things like that. Once you are done with doing all this look into the logic if it adheres to the requirement.

                  M 1 Reply Last reply
                  0
                  • P peterchen

                    Pascal Ganaye wrote:

                    Avoid methods with more than 25 lines.

                    That seems awfully low. Maybe for this newfangled stuff where you make a color gradient animation in one line of code, and all people goo "ooh" and "aah" and forget to ask what that software is for, but for real work? (admittedly, that's from the guy who has to maintain a 4KLOC switch statement)

                    Agh! Reality! My Archnemesis![^]
                    | FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server.

                    R Offline
                    R Offline
                    Rama Krishna Vavilala
                    wrote on last edited by
                    #12

                    peterchen wrote:

                    That seems awfully low

                    Not in the Java/C#/Linq world. For instance, in my code base of around 100,000 Lines, the maximum number of lines in a manually written method is 28. I think 25-30 is a reasonable maximum. Some Java frameworks have as much as 15 as the maximum.

                    M P 2 Replies Last reply
                    0
                    • P PIEBALDconsult

                      :wtf: I agree with most of the MS one... how'd that happen? :confused:

                      H Offline
                      H Offline
                      Henry Minute
                      wrote on last edited by
                      #13

                      Take two Aspirin, lie down in a dark room and if not better by morning, call the doctor.

                      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                      1 Reply Last reply
                      0
                      • P Pascal Ganaye

                        I personally would prefer Don't hard-code a non trivial numeric value that appears, declare a constant instead. This is particularly true when the value appears more than once.

                        S Offline
                        S Offline
                        supercat9
                        wrote on last edited by
                        #14

                        Pascal Ganaye wrote:

                        Don't hard-code a non trivial numeric value that appears, declare a constant instead. This is particularly true when the value appears more than once.

                        Any advice for how to best organize such declarations so they make sense? To be sure, automated tools make the layout less important than it would have been in the days when printouts were used a lot more, but I often find that I have constants which really don't "fit" in any of my "constants" areas. Also, btw, in VB I sometimes end up using constants cb1, cb2, etc. in places where I need a small byte value, since VB will squawk at "bytevar = bytevar or 1" but will accept "bytevar = bytevar or ByteConstantWhichEqualsOne" without complaint. What would have been nicer would be if there were a suffix to denote that a particular value should be regarded as a byte, but no such thing exists.

                        1 Reply Last reply
                        0
                        • R Rama Krishna Vavilala

                          peterchen wrote:

                          That seems awfully low

                          Not in the Java/C#/Linq world. For instance, in my code base of around 100,000 Lines, the maximum number of lines in a manually written method is 28. I think 25-30 is a reasonable maximum. Some Java frameworks have as much as 15 as the maximum.

                          M Offline
                          M Offline
                          Mustafa Ismail Mustafa
                          wrote on last edited by
                          #15

                          Rama Krishna Vavilala wrote:

                          I think 25-30 is a reasonable maximum.

                          I hate large methods as much as the next guy, but in several cases, I have methods running to almost 200 lines. On the code base I'm working on, it seems counter-intuitive to break it up to several methods.

                          If the post was helpful, please vote, eh! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?

                          R 1 Reply Last reply
                          0
                          • P Pascal Ganaye

                            I have been asked to work on our Code Review Procedure. This sort of thing: Pascal casing used for type and method names Camel casing used for local variable names and method arguments ... A single file should only contribute types to a single namespace. Avoid having multiple namespaces in the same file. Avoid files with more than 500 lines (excluding machine-generated code). Avoid methods with more than 25 lines. ... Never hard-code a numeric value, always declare a constant instead. ... I have the feeling it is missing what really make maintenance go bad. Things like re use existing classes, don't duplicate code, separate logic, data access, and display. Can anyone point me to some documents with good recommendations of this type. The idea is to minimize defect and certainly not to alienate the programmers (including me).

                            B Offline
                            B Offline
                            Brady Kelly
                            wrote on last edited by
                            #16

                            Pascal Ganaye wrote:

                            Never hard-code a numeric value, always declare a constant instead.

                            Yikes! What about if errors.Count() > 0? Surely I don't need a ZERO_ONLY_ZERO constant? :~

                            L 1 Reply Last reply
                            0
                            • D dan sh

                              Look into tools like FxCop, StyleCop and ReSharper. These should take care of formatting and redundant code and things like that. Once you are done with doing all this look into the logic if it adheres to the requirement.

                              M Offline
                              M Offline
                              Mustafa Ismail Mustafa
                              wrote on last edited by
                              #17

                              +100 ReSharper. I just love that software

                              If the post was helpful, please vote, eh! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?

                              1 Reply Last reply
                              0
                              • M Mustafa Ismail Mustafa

                                Rama Krishna Vavilala wrote:

                                I think 25-30 is a reasonable maximum.

                                I hate large methods as much as the next guy, but in several cases, I have methods running to almost 200 lines. On the code base I'm working on, it seems counter-intuitive to break it up to several methods.

                                If the post was helpful, please vote, eh! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?

                                R Offline
                                R Offline
                                Rama Krishna Vavilala
                                wrote on last edited by
                                #18

                                Mustafa Ismail Mustafa wrote:

                                counter-intuitive to break it up to several methods.

                                That is where I normally take opinion of someone else. Someone, figures out a clever way, usually. The best way to break a method is in chunks of logic. The bad way is to do the following:

                                void Method()
                                {
                                Portion1();
                                Portion2();
                                Portion3();
                                }

                                M W 2 Replies Last reply
                                0
                                • P PIEBALDconsult

                                  :wtf: I agree with most of the MS one... how'd that happen? :confused:

                                  P Offline
                                  P Offline
                                  Paul Conrad
                                  wrote on last edited by
                                  #19

                                  I agree with most of them, but one that really bugs me is: for (int i=0; i<100; i++) { DoSomething(i); } I prefer: for (int i=0; i<100; i++) DoSomething(i); or:

                                  for (int i=0; i<100; i++)
                                  {
                                  DoSomething(i);
                                  }

                                  With the last one, who knows what future statements may need to be added... Another one that bugs me is stating that 4 spaces should be used instead of Tab, sorry, Tab tab tab ;P

                                  "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

                                  A P S 3 Replies Last reply
                                  0
                                  • R Rama Krishna Vavilala

                                    peterchen wrote:

                                    That seems awfully low

                                    Not in the Java/C#/Linq world. For instance, in my code base of around 100,000 Lines, the maximum number of lines in a manually written method is 28. I think 25-30 is a reasonable maximum. Some Java frameworks have as much as 15 as the maximum.

                                    P Offline
                                    P Offline
                                    peterchen
                                    wrote on last edited by
                                    #20

                                    Hmm... I've checked a few of recent functions. Observations: It's C++, which has a notable overhead mostly for error handling. However, in most cases I don't see any benefit of breaking them up. One of them, quite simple by this project's standards, is 62 lines, there are 14 opening/closing braces, 6 full-line comments helping understand the code flow, and 11 empty lines to group "logic blocks" (each of the full-line comment is preceded by one). Breaking up the functionality (finding a best match) into three logical pieces was possible (Check A, Check B, Generate Diagnostics), but that would expose a lot of context. With the additional boilerplate, documentation and definition of the functions' contracts, I'd say I'd end up with worse code. Of course, being a C++ programmer, I could write that code in 25 lines, but you really don't want me to :rolleyes:

                                    Agh! Reality! My Archnemesis![^]
                                    | FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server.

                                    R 2 Replies Last reply
                                    0
                                    • R Rama Krishna Vavilala

                                      Mustafa Ismail Mustafa wrote:

                                      counter-intuitive to break it up to several methods.

                                      That is where I normally take opinion of someone else. Someone, figures out a clever way, usually. The best way to break a method is in chunks of logic. The bad way is to do the following:

                                      void Method()
                                      {
                                      Portion1();
                                      Portion2();
                                      Portion3();
                                      }

                                      M Offline
                                      M Offline
                                      Mustafa Ismail Mustafa
                                      wrote on last edited by
                                      #21

                                      Rama Krishna Vavilala wrote:

                                      That is where I normally take opinion of someone else. Someone, figures out a clever way, usually. The best way to break a method is in chunks of logic.

                                      Well yeah, and normally that's the way I'd prefer it to go, but sometimes, you've got your back to the wall...

                                      Rama Krishna Vavilala wrote:

                                      void Method() { Portion1(); Portion2(); Portion3(); }

                                      X| :thumbsdown: I'll admit in my younger days I did that a few times until a friendly senior explained to me some of the facts of programming life.

                                      If the post was helpful, please vote, eh! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?

                                      1 Reply Last reply
                                      0
                                      • P Pascal Ganaye

                                        I have been asked to work on our Code Review Procedure. This sort of thing: Pascal casing used for type and method names Camel casing used for local variable names and method arguments ... A single file should only contribute types to a single namespace. Avoid having multiple namespaces in the same file. Avoid files with more than 500 lines (excluding machine-generated code). Avoid methods with more than 25 lines. ... Never hard-code a numeric value, always declare a constant instead. ... I have the feeling it is missing what really make maintenance go bad. Things like re use existing classes, don't duplicate code, separate logic, data access, and display. Can anyone point me to some documents with good recommendations of this type. The idea is to minimize defect and certainly not to alienate the programmers (including me).

                                        E Offline
                                        E Offline
                                        eslsys
                                        wrote on last edited by
                                        #22

                                        The first commandment of any commandments is that the sentence begins with "Thou shalt (not) ... "

                                        1 Reply Last reply
                                        0
                                        • P peterchen

                                          Hmm... I've checked a few of recent functions. Observations: It's C++, which has a notable overhead mostly for error handling. However, in most cases I don't see any benefit of breaking them up. One of them, quite simple by this project's standards, is 62 lines, there are 14 opening/closing braces, 6 full-line comments helping understand the code flow, and 11 empty lines to group "logic blocks" (each of the full-line comment is preceded by one). Breaking up the functionality (finding a best match) into three logical pieces was possible (Check A, Check B, Generate Diagnostics), but that would expose a lot of context. With the additional boilerplate, documentation and definition of the functions' contracts, I'd say I'd end up with worse code. Of course, being a C++ programmer, I could write that code in 25 lines, but you really don't want me to :rolleyes:

                                          Agh! Reality! My Archnemesis![^]
                                          | FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server.

                                          R Offline
                                          R Offline
                                          Rama Krishna Vavilala
                                          wrote on last edited by
                                          #23

                                          When I maintain someone's code, I will prefer to see CheckA(); CheckB(); GeneralDiagniostics(); Seeing that I may get an idea of what the method is doing even though I may be new to the team. However, it may become worse if you are passing lot of parameters around. The thing I like about such rules of enforcing maximum lines is programmers are made to think and when they think good solutions may emerge. The eventual aim is to have fewer methods and fewer classes in other words "less code". Less code less bugs.

                                          H P 2 Replies 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