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. Programming style

Programming style

Scheduled Pinned Locked Moved The Lounge
c++architecture
46 Posts 27 Posters 4 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.
  • realJSOPR realJSOP

    If the function is relatively short (will fit on my screen without scrolling) I go with option 1. If it's longer, I go with option 2. I don't mind a lot of indentation as long as the code is formatted appropriately and there's a dearth of comments. Also, if a function does exceed the height of my screen, I try to find ways to abstract some the functionality into smaller re-usable functions. Some of the code I've had to maintain over the last three years had functions in it that exceeded 500 lines, and some files exceeded 6000 lines. Those were a pain in the ass to debug.

    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
    -----
    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

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

    John Simmons / outlaw programmer wrote:

    Some of the code I've had to maintain over the last three years had functions in it that exceeded 500 lines, and some files exceeded 6000 lines.

    That is way too excessive. Gotta stick to it being able to fit on the screen all at once. Hope the original coder doesn't work there anymore...

    "The clue train passed his station without stopping." - John Simmons / outlaw programmer

    realJSOPR 1 Reply Last reply
    0
    • C Christian Graus

      As a rule, I like to have one exit point for functions, but I sometimes make an exception for initial state checks. Which do you prefer Button btn = sender as Button; if (btn != null) { } OR Button btn = sender as Button; if (btn == null) return;

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

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

      The first, always. Though of course I'd probably use is rather than as.

      1 Reply Last reply
      0
      • C Christian Graus

        As a rule, I like to have one exit point for functions, but I sometimes make an exception for initial state checks. Which do you prefer Button btn = sender as Button; if (btn != null) { } OR Button btn = sender as Button; if (btn == null) return;

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #38

        neither. i try to avoid dogmatically following arbitrary rules. i let the problem design the code, instead of forcing the problem into a template.

        image processing toolkits | batch image processing | blogging

        1 Reply Last reply
        0
        • C Christian Graus

          As a rule, I like to have one exit point for functions, but I sometimes make an exception for initial state checks. Which do you prefer Button btn = sender as Button; if (btn != null) { } OR Button btn = sender as Button; if (btn == null) return;

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          M Offline
          M Offline
          Michael P Butler
          wrote on last edited by
          #39

          I too use the second style for initial state checks, but sometimes I get a little lazy and use it in other places. Of course, in these modern days where methods throw exceptions, the single exit point has become a bit moot.

          Michael Thanks to all for your kind words and support on my return to CP. This place and you guys and gals are just the best

          1 Reply Last reply
          0
          • S Shog9 0

            achimera wrote:

            Excessive indentation, however (as per best programming practices), is not solved by multiple returns -- but instead through other means.

            Wait, are you implying i need to reduce my tab size... :suss: ;)

            every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

            R Offline
            R Offline
            Ravi Bhavnani
            wrote on last edited by
            #40

            Shog9 wrote:

            Excessive indentation

            This has ceased to be a problem for me ever since I started writing code in MS Word. /ravi PS: Your sig is unreadable (moved below the boundary of the light blue reply area) in IE.  Dunno if that's intentional. See this[^] screenshot.

            This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

            1 Reply Last reply
            0
            • C Christian Graus

              As a rule, I like to have one exit point for functions, but I sometimes make an exception for initial state checks. Which do you prefer Button btn = sender as Button; if (btn != null) { } OR Button btn = sender as Button; if (btn == null) return;

              Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              E Offline
              E Offline
              ed welch
              wrote on last edited by
              #41

              The secound one is handy if you have a lot of nested if-then-else statements - it avoids the inner most code block being indented so much that it runs of the end of the screen

              1 Reply Last reply
              0
              • P Paul Conrad

                John Simmons / outlaw programmer wrote:

                Some of the code I've had to maintain over the last three years had functions in it that exceeded 500 lines, and some files exceeded 6000 lines.

                That is way too excessive. Gotta stick to it being able to fit on the screen all at once. Hope the original coder doesn't work there anymore...

                "The clue train passed his station without stopping." - John Simmons / outlaw programmer

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

                Paul Conrad wrote:

                Hope the original coder doesn't work there anymore.

                He dopes, but it doesn't matter. I turned in my two-week notice last week. :)

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                P 1 Reply Last reply
                0
                • realJSOPR realJSOP

                  Paul Conrad wrote:

                  Hope the original coder doesn't work there anymore.

                  He dopes, but it doesn't matter. I turned in my two-week notice last week. :)

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

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

                  John Simmons / outlaw programmer wrote:

                  He dopes, but it doesn't matter.

                  Yeah, it does. People on dope can't code...

                  "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                  1 Reply Last reply
                  0
                  • C Christian Graus

                    As a rule, I like to have one exit point for functions, but I sometimes make an exception for initial state checks. Which do you prefer Button btn = sender as Button; if (btn != null) { } OR Button btn = sender as Button; if (btn == null) return;

                    Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #44

                    I like the second one. With the first style I often find I end up with code like this if(something) { ... if(somethingelse) { ... if(anotherthing) { DoSomthingUseful(); } } }

                    1 Reply Last reply
                    0
                    • C Christian Graus

                      As a rule, I like to have one exit point for functions, but I sometimes make an exception for initial state checks. Which do you prefer Button btn = sender as Button; if (btn != null) { } OR Button btn = sender as Button; if (btn == null) return;

                      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                      T Offline
                      T Offline
                      Todd Smith
                      wrote on last edited by
                      #45

                      I prefer the 1st one since 1) its easier to debug w/one exit point 2) its easier to enforce.

                      Todd Smith

                      1 Reply Last reply
                      0
                      • C Christian Graus

                        As a rule, I like to have one exit point for functions, but I sometimes make an exception for initial state checks. Which do you prefer Button btn = sender as Button; if (btn != null) { } OR Button btn = sender as Button; if (btn == null) return;

                        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                        P Offline
                        P Offline
                        Phil Harding
                        wrote on last edited by
                        #46

                        Second style usually, if the method/function is short, maybe I'll go for the first. Sometimes I just flip a coin :jig:


                        - "I'm not lying, I'm just writing fiction with my mouth"

                        Phil Harding.
                        myBlog [^] | mySite [^]

                        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