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. Code Neatness

Code Neatness

Scheduled Pinned Locked Moved The Lounge
c++question
82 Posts 42 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.
  • C Chris Austin

    Shog9 wrote:

    Do people not know how to generate assembler output from their compilers anymore?

    Nope. And if they did can they read it?

    A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects. - -Lazarus Long

    S Offline
    S Offline
    Shog9 0
    wrote on last edited by
    #9

    :laugh: There's a reason why, after an hour spent carefully formatting the diff of the compiler-generated assembler for in-loop and function start variables, i scrapped it and just declared that it was identical. Some arguments just aren't worth the time... :sigh:

    1 Reply Last reply
    0
    • R Ri Qen Sin

      So somewhere on the internet, I pointed out to a newbie that his code was pretty messy and that he should start by consolidating some of his variable declarations to the beginning of the program (which was consisted of only a main(…) function/method/entry point and a lot of improperly formatted code). In walks another forum member and criticizes me for my comment saying that "it's a feature of the C++ language to be able to declare variables anywhere you need it." I was obviously pissed at that comment showing his utter disregard for code neatness and readability (and likely a malicious attack on my intelligence). I'm trying to seek agreement… so was I right when I said so?

      So the creationist says: Everything must have a designer. God designed everything. I say: Why is God the only exception? Why not make the "designs" (like man) exceptions and make God a creation of man?

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #10

      In addition to the other replies... Besides readability, there's also variable scope in C++.  Everything at the top means method-wide scope - that's not always desired! Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      R 1 Reply Last reply
      0
      • M Mark Salsbery

        In addition to the other replies... Besides readability, there's also variable scope in C++.  Everything at the top means method-wide scope - that's not always desired! Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        R Offline
        R Offline
        Ri Qen Sin
        wrote on last edited by
        #11

        Yes, I'm aware of that: my style is to declare it as close to the beginning as possible while keeping all the local variables deifnitions in the most local scope. I like to keep definitions in one spot so I know what variables I have.

        So the creationist says: Everything must have a designer. God designed everything. I say: Why is God the only exception? Why not make the "designs" (like man) exceptions and make God a creation of man?

        C M 2 Replies Last reply
        0
        • F Franc Morales

          Matter of preference, really, as long as you don't declare inside a loop... but, yeah, I generally put them at the top if the method is not long. Otherwise, I group them in blocks near where they are used...

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

          Franc Morales wrote:

          as long as you don't declare inside a loop

          Why not? :confused: /ravi

          My new year resolution: 2048 x 1536 Home | Music | Articles | Freeware ravib(at)ravib(dot)com

          A 1 Reply Last reply
          0
          • R Ri Qen Sin

            Yes, I'm aware of that: my style is to declare it as close to the beginning as possible while keeping all the local variables deifnitions in the most local scope. I like to keep definitions in one spot so I know what variables I have.

            So the creationist says: Everything must have a designer. God designed everything. I say: Why is God the only exception? Why not make the "designs" (like man) exceptions and make God a creation of man?

            C Offline
            C Offline
            cp9876
            wrote on last edited by
            #13

            Ri Qen-Sin wrote:

            I like to keep definitions in one spot so I know what variables I have.

            Why? Whilst much of this is 'in the eye of the beholder' and so there is no right answer, if the objective is clear and understandable code, what advantange is there in separating the declaration of a variable from its first use? Additionally, in C++, for classes with a constructor, the declaration is a function call and so the location of this call is often determined otherwise, e.g. CString fname; // work out and assign something to fname ofstream out(fname); // do something with out I can't see how reorganising the code to declare 'out' at the beginning 'so that you know what variables you have' in any way would improve the code.

            Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

            1 Reply Last reply
            0
            • R Ri Qen Sin

              So somewhere on the internet, I pointed out to a newbie that his code was pretty messy and that he should start by consolidating some of his variable declarations to the beginning of the program (which was consisted of only a main(…) function/method/entry point and a lot of improperly formatted code). In walks another forum member and criticizes me for my comment saying that "it's a feature of the C++ language to be able to declare variables anywhere you need it." I was obviously pissed at that comment showing his utter disregard for code neatness and readability (and likely a malicious attack on my intelligence). I'm trying to seek agreement… so was I right when I said so?

              So the creationist says: Everything must have a designer. God designed everything. I say: Why is God the only exception? Why not make the "designs" (like man) exceptions and make God a creation of man?

              Steve EcholsS Offline
              Steve EcholsS Offline
              Steve Echols
              wrote on last edited by
              #14

              Also, in addition to above comments, declaring variables at the top could stem from variable name re-use in C, C++: So at the top of a function you'd have: int i, j; Then you could re-use those variables in your function multiple times and the memory would only be allocated once. for (i = 0; i < blah, i++) { for (j = 0; j < blahblah; j++) do something; } for (i = 0; i < blah, i++) { for (j = 0; j < blahblah; j++) do something else; } In todays languages it would be: for (int i = 0; i < blah, i++) { for (int j = 0; j < blahblah; j++) do something; } for (int i = 0; i < blah, i++) { for (int j = 0; j < blahblah; j++) do something else; } ... but if you try that in C++ you get a duplicate definition error on i. Just trying to justify declaring variables at the top, I don't think there's a right or wrong way. Personally, I declare them as close to the point of use, as other people have mentioned, but if they're generic, reusable index variables I do them at the top. Then again, I'm old school. Compilers optimize this stuff for us now, but it's a hard habit to break. In my head, defining int j inside another loop will cause a memory allocation each time through the first loop. Ramblin' on....


              - S 50 cups of coffee and you know it's on!

              • S
                50 cups of coffee and you know it's on!
                Code, follow, or get out of the way.
              A E U N 4 Replies Last reply
              0
              • R Ri Qen Sin

                Yes, I'm aware of that: my style is to declare it as close to the beginning as possible while keeping all the local variables deifnitions in the most local scope. I like to keep definitions in one spot so I know what variables I have.

                So the creationist says: Everything must have a designer. God designed everything. I say: Why is God the only exception? Why not make the "designs" (like man) exceptions and make God a creation of man?

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #15

                You're still talking readability.... I'm referring to taking advantage of the scoping of variables within functions in C++. Besides that, to me, it's more readable to me if the declarations are close by. Mark

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                T 1 Reply Last reply
                0
                • R Ravi Bhavnani

                  Franc Morales wrote:

                  as long as you don't declare inside a loop

                  Why not? :confused: /ravi

                  My new year resolution: 2048 x 1536 Home | Music | Articles | Freeware ravib(at)ravib(dot)com

                  A Offline
                  A Offline
                  AmazingMo
                  wrote on last edited by
                  #16

                  Ravi Bhavnani wrote:

                  as long as you don't declare inside a loop Why not?

                  Because every time around the loop the stack will be adjusted for the "new" variable, and then re-adjusted as the variable goes "out of scope". Try it a million times or so and see what happens. Seriously, most compilers will hoist the variable declaration out of the loop scope, but do you want to take the risk that the particular compiler that you're using will? Apropos the OP's question. You started programming in C, didn't you... Do you still use Hungarian notation? Some times it's better to just let go. ;-) You could try using more blocks if you absolutely must have declarations at the opening paren. Cheers, P.

                  R T T 3 Replies Last reply
                  0
                  • A AmazingMo

                    Ravi Bhavnani wrote:

                    as long as you don't declare inside a loop Why not?

                    Because every time around the loop the stack will be adjusted for the "new" variable, and then re-adjusted as the variable goes "out of scope". Try it a million times or so and see what happens. Seriously, most compilers will hoist the variable declaration out of the loop scope, but do you want to take the risk that the particular compiler that you're using will? Apropos the OP's question. You started programming in C, didn't you... Do you still use Hungarian notation? Some times it's better to just let go. ;-) You could try using more blocks if you absolutely must have declarations at the opening paren. Cheers, P.

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

                    AmazingMo wrote:

                    Try it a million times or so and see what happens.

                    If the code loops a million times, I think we have a much bigger problem. :)

                    AmazingMo wrote:

                    most compilers will hoist the variable declaration out of the loop scope, but do you want to take the risk that the particular compiler that you're using will?

                    [edit] I can't imagine I'd use one that didn't do that as part of standard optimization! Code hoisting has been around for decades and is pretty mature technology. [/edit]

                    AmazingMo wrote:

                    You started programming in C, didn't you

                    No.  I started programming before the first mainstream C compilers came to market. /ravi

                    My new year resolution: 2048 x 1536 Home | Music | Articles | Freeware ravib(at)ravib(dot)com

                    modified on Monday, March 10, 2008 2:17 AM

                    C T M 3 Replies Last reply
                    0
                    • Steve EcholsS Steve Echols

                      Also, in addition to above comments, declaring variables at the top could stem from variable name re-use in C, C++: So at the top of a function you'd have: int i, j; Then you could re-use those variables in your function multiple times and the memory would only be allocated once. for (i = 0; i < blah, i++) { for (j = 0; j < blahblah; j++) do something; } for (i = 0; i < blah, i++) { for (j = 0; j < blahblah; j++) do something else; } In todays languages it would be: for (int i = 0; i < blah, i++) { for (int j = 0; j < blahblah; j++) do something; } for (int i = 0; i < blah, i++) { for (int j = 0; j < blahblah; j++) do something else; } ... but if you try that in C++ you get a duplicate definition error on i. Just trying to justify declaring variables at the top, I don't think there's a right or wrong way. Personally, I declare them as close to the point of use, as other people have mentioned, but if they're generic, reusable index variables I do them at the top. Then again, I'm old school. Compilers optimize this stuff for us now, but it's a hard habit to break. In my head, defining int j inside another loop will cause a memory allocation each time through the first loop. Ramblin' on....


                      - S 50 cups of coffee and you know it's on!

                      A Offline
                      A Offline
                      AmazingMo
                      wrote on last edited by
                      #18

                      Steve Echols wrote:

                      n todays languages it would be: for (int i = 0; i < blah, i++) { for (int j = 0; j < blahblah; j++) do something; } for (int i = 0; i < blah, i++) { for (int j = 0; j < blahblah; j++) do something else; } ... but if you try that in C++ you get a duplicate definition error on i.

                      I was under the impression that this is an error in your compiler. If you're using Visual Studio, go to the Project Property Pages, C/C++ -> Language -> Force Conformance in For Loop Scope, and select "Yes". That ought to fix your problem. Cheers, P.

                      Steve EcholsS 1 Reply Last reply
                      0
                      • A AmazingMo

                        Steve Echols wrote:

                        n todays languages it would be: for (int i = 0; i < blah, i++) { for (int j = 0; j < blahblah; j++) do something; } for (int i = 0; i < blah, i++) { for (int j = 0; j < blahblah; j++) do something else; } ... but if you try that in C++ you get a duplicate definition error on i.

                        I was under the impression that this is an error in your compiler. If you're using Visual Studio, go to the Project Property Pages, C/C++ -> Language -> Force Conformance in For Loop Scope, and select "Yes". That ought to fix your problem. Cheers, P.

                        Steve EcholsS Offline
                        Steve EcholsS Offline
                        Steve Echols
                        wrote on last edited by
                        #19

                        Yeah, you're right, I was thinking in C#/VB. :)


                        - S 50 cups of coffee and you know it's on!

                        • S
                          50 cups of coffee and you know it's on!
                          Code, follow, or get out of the way.
                        1 Reply Last reply
                        0
                        • A AmazingMo

                          Ravi Bhavnani wrote:

                          as long as you don't declare inside a loop Why not?

                          Because every time around the loop the stack will be adjusted for the "new" variable, and then re-adjusted as the variable goes "out of scope". Try it a million times or so and see what happens. Seriously, most compilers will hoist the variable declaration out of the loop scope, but do you want to take the risk that the particular compiler that you're using will? Apropos the OP's question. You started programming in C, didn't you... Do you still use Hungarian notation? Some times it's better to just let go. ;-) You could try using more blocks if you absolutely must have declarations at the opening paren. Cheers, P.

                          T Offline
                          T Offline
                          T Mac Oz
                          wrote on last edited by
                          #20

                          AmazingMo wrote:

                          Do you still use Hungarian notation? Some times it's better to just let go.

                          The original concept of Hungarian Notation was actually quite different from M$s butchery of it. I forget the actual words used to define the concept but the prefix was really meant to indicate the usage of the variable, not its data-type. E.g. //M$: DWORD dwVar1, dwVar2; // M$ uses prefix to denote type // not very useful in the grand scheme of things, // since the compiler picks up type mismatches // What is the variable used for? // You have to scan the code to find out for (dwVar1 = 0UL; dwVar1 ... { for (dwVar2 = 0UL; dwVar2 ... //Original Hungarian: DWORD outerIterVar, innerIterVar; // Original Hungarian uses prefix to denote usage (e.g. "outer iterator"), // giving the programmer an idea of the intended usage of the variable // without having to scan the code for (outerIterVar = 0UL; outerIterVar ... { for (innerIterVar = 0UL; innerIterVar ... Having worked with MFC and WinAPI for so many years, it came as a surprise to learn this but I saw the sense in it right away & have tried to "do it properly" ever since. Unfortunately, old habits die hard... sigh.

                          T-Mac-Oz

                          J A A M C 5 Replies Last reply
                          0
                          • R Ravi Bhavnani

                            AmazingMo wrote:

                            Try it a million times or so and see what happens.

                            If the code loops a million times, I think we have a much bigger problem. :)

                            AmazingMo wrote:

                            most compilers will hoist the variable declaration out of the loop scope, but do you want to take the risk that the particular compiler that you're using will?

                            [edit] I can't imagine I'd use one that didn't do that as part of standard optimization! Code hoisting has been around for decades and is pretty mature technology. [/edit]

                            AmazingMo wrote:

                            You started programming in C, didn't you

                            No.  I started programming before the first mainstream C compilers came to market. /ravi

                            My new year resolution: 2048 x 1536 Home | Music | Articles | Freeware ravib(at)ravib(dot)com

                            modified on Monday, March 10, 2008 2:17 AM

                            C Offline
                            C Offline
                            Chris Austin
                            wrote on last edited by
                            #21

                            Ravi Bhavnani wrote:

                            If the code loops a million times, I think we have a much bigger problem.

                            Like tons of legacy data stored in a stupid system....I could go on but I'd be moved to the soapbox.

                            A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects. - -Lazarus Long

                            1 Reply Last reply
                            0
                            • R Ravi Bhavnani

                              AmazingMo wrote:

                              Try it a million times or so and see what happens.

                              If the code loops a million times, I think we have a much bigger problem. :)

                              AmazingMo wrote:

                              most compilers will hoist the variable declaration out of the loop scope, but do you want to take the risk that the particular compiler that you're using will?

                              [edit] I can't imagine I'd use one that didn't do that as part of standard optimization! Code hoisting has been around for decades and is pretty mature technology. [/edit]

                              AmazingMo wrote:

                              You started programming in C, didn't you

                              No.  I started programming before the first mainstream C compilers came to market. /ravi

                              My new year resolution: 2048 x 1536 Home | Music | Articles | Freeware ravib(at)ravib(dot)com

                              modified on Monday, March 10, 2008 2:17 AM

                              T Offline
                              T Offline
                              T Mac Oz
                              wrote on last edited by
                              #22

                              Ravi Bhavnani wrote:

                              If the code loops a million times, I think we have a much bigger problem.

                              Usually the problem that requires millions of iterations in it's solution :-D Search engine query? How many hash buckets would you need to eliminate child node searches altogether? "grep"ing a large text (or set of) files. SQL query on a large database (somebody's got to write the query engine!) Sure, these are examples of stuff that typically has a multitude of pre-existing, mature solutions but a) they're just examples to show that there are domains where millions of iterations can be reasonably expected b) Somebody's got to maintain it!

                              T-Mac-Oz

                              F 1 Reply Last reply
                              0
                              • T T Mac Oz

                                AmazingMo wrote:

                                Do you still use Hungarian notation? Some times it's better to just let go.

                                The original concept of Hungarian Notation was actually quite different from M$s butchery of it. I forget the actual words used to define the concept but the prefix was really meant to indicate the usage of the variable, not its data-type. E.g. //M$: DWORD dwVar1, dwVar2; // M$ uses prefix to denote type // not very useful in the grand scheme of things, // since the compiler picks up type mismatches // What is the variable used for? // You have to scan the code to find out for (dwVar1 = 0UL; dwVar1 ... { for (dwVar2 = 0UL; dwVar2 ... //Original Hungarian: DWORD outerIterVar, innerIterVar; // Original Hungarian uses prefix to denote usage (e.g. "outer iterator"), // giving the programmer an idea of the intended usage of the variable // without having to scan the code for (outerIterVar = 0UL; outerIterVar ... { for (innerIterVar = 0UL; innerIterVar ... Having worked with MFC and WinAPI for so many years, it came as a surprise to learn this but I saw the sense in it right away & have tried to "do it properly" ever since. Unfortunately, old habits die hard... sigh.

                                T-Mac-Oz

                                J Offline
                                J Offline
                                joerg smn
                                wrote on last edited by
                                #23

                                Wouldn't one name those variables dwOuter and dwInner instead? Having "var" in a variable's name looks quite redundant to me.

                                T 1 Reply Last reply
                                0
                                • T T Mac Oz

                                  AmazingMo wrote:

                                  Do you still use Hungarian notation? Some times it's better to just let go.

                                  The original concept of Hungarian Notation was actually quite different from M$s butchery of it. I forget the actual words used to define the concept but the prefix was really meant to indicate the usage of the variable, not its data-type. E.g. //M$: DWORD dwVar1, dwVar2; // M$ uses prefix to denote type // not very useful in the grand scheme of things, // since the compiler picks up type mismatches // What is the variable used for? // You have to scan the code to find out for (dwVar1 = 0UL; dwVar1 ... { for (dwVar2 = 0UL; dwVar2 ... //Original Hungarian: DWORD outerIterVar, innerIterVar; // Original Hungarian uses prefix to denote usage (e.g. "outer iterator"), // giving the programmer an idea of the intended usage of the variable // without having to scan the code for (outerIterVar = 0UL; outerIterVar ... { for (innerIterVar = 0UL; innerIterVar ... Having worked with MFC and WinAPI for so many years, it came as a surprise to learn this but I saw the sense in it right away & have tried to "do it properly" ever since. Unfortunately, old habits die hard... sigh.

                                  T-Mac-Oz

                                  A Offline
                                  A Offline
                                  AmazingMo
                                  wrote on last edited by
                                  #24

                                  Thanks T-Mac-Oz. I had heard something along these lines some time ago, in a post by Joel Spolsky. See if I can find it again... http://www.joelonsoftware.com/articles/Wrong.html[^] Unfortunately, as you say, the "names carries the type" version of Hungarian notation is the one that most people understand. Personally, I don't understand why people try to perservere with this (name carries type) in modern C++. I argue that C++ removes the value in doing this. Apart from the simple fact that you aren't going to attempt to put the type into variables that are instances of classes, isn't one of the original design goals of C++ to limit the scope of the code that needs access to the instances of fundamental types? Sadly enough, I usually lose these arguments. ;-) Just the other day we had a coding standards meeting for a new project. Sure enough, in an hour we didn't manage to progress beyond the standard for encoding scope and type into variable names. (I'm happy enough to prepend something for scope, btw.) But m_kpkdwName (member data const pointer to const dWord)??? Seriously? It just aggravates me. I'll go home and pop some Prozac and tomorrow it'll all be good again. Cheers, P.

                                  T 1 Reply Last reply
                                  0
                                  • R Ri Qen Sin

                                    So somewhere on the internet, I pointed out to a newbie that his code was pretty messy and that he should start by consolidating some of his variable declarations to the beginning of the program (which was consisted of only a main(…) function/method/entry point and a lot of improperly formatted code). In walks another forum member and criticizes me for my comment saying that "it's a feature of the C++ language to be able to declare variables anywhere you need it." I was obviously pissed at that comment showing his utter disregard for code neatness and readability (and likely a malicious attack on my intelligence). I'm trying to seek agreement… so was I right when I said so?

                                    So the creationist says: Everything must have a designer. God designed everything. I say: Why is God the only exception? Why not make the "designs" (like man) exceptions and make God a creation of man?

                                    M Offline
                                    M Offline
                                    Maxwell Chen
                                    wrote on last edited by
                                    #25

                                    Robo George should come and answer this question! :-D

                                    Maxwell Chen

                                    1 Reply Last reply
                                    0
                                    • J joerg smn

                                      Wouldn't one name those variables dwOuter and dwInner instead? Having "var" in a variable's name looks quite redundant to me.

                                      T Offline
                                      T Offline
                                      T Mac Oz
                                      wrote on last edited by
                                      #26

                                      joerg-schumann wrote:

                                      Having "var" in a variable's name looks quite redundant to me.

                                      There might be occasion to distinguish between locally defined* vars & consts, sheesh, I don't know, it was only an example! *actually, there's a good example of Hungarian gone right, the oft used "g" prefix for global vars - though not by M$ (just off the top of my head! no flames please if I've forgotten one somewhere!). The "m_" prefix is a positive example frequently used by M$ which makes it very clear that a member variable is being used, though this one seems to get bagged a lot, usually by non (or even some former) C++ programmers.

                                      T-Mac-Oz

                                      1 Reply Last reply
                                      0
                                      • A AmazingMo

                                        Thanks T-Mac-Oz. I had heard something along these lines some time ago, in a post by Joel Spolsky. See if I can find it again... http://www.joelonsoftware.com/articles/Wrong.html[^] Unfortunately, as you say, the "names carries the type" version of Hungarian notation is the one that most people understand. Personally, I don't understand why people try to perservere with this (name carries type) in modern C++. I argue that C++ removes the value in doing this. Apart from the simple fact that you aren't going to attempt to put the type into variables that are instances of classes, isn't one of the original design goals of C++ to limit the scope of the code that needs access to the instances of fundamental types? Sadly enough, I usually lose these arguments. ;-) Just the other day we had a coding standards meeting for a new project. Sure enough, in an hour we didn't manage to progress beyond the standard for encoding scope and type into variable names. (I'm happy enough to prepend something for scope, btw.) But m_kpkdwName (member data const pointer to const dWord)??? Seriously? It just aggravates me. I'll go home and pop some Prozac and tomorrow it'll all be good again. Cheers, P.

                                        T Offline
                                        T Offline
                                        T Mac Oz
                                        wrote on last edited by
                                        #27

                                        AmazingMo wrote:

                                        in a post by Joel Spolsky

                                        I think that's where I read it too. Joel is sometimes controversial (read as: I don't agree with him) and sometimes very insightful (read as: damn! why didn't someone tell me this years ago!).

                                        T-Mac-Oz

                                        1 Reply Last reply
                                        0
                                        • R Ri Qen Sin

                                          So somewhere on the internet, I pointed out to a newbie that his code was pretty messy and that he should start by consolidating some of his variable declarations to the beginning of the program (which was consisted of only a main(…) function/method/entry point and a lot of improperly formatted code). In walks another forum member and criticizes me for my comment saying that "it's a feature of the C++ language to be able to declare variables anywhere you need it." I was obviously pissed at that comment showing his utter disregard for code neatness and readability (and likely a malicious attack on my intelligence). I'm trying to seek agreement… so was I right when I said so?

                                          So the creationist says: Everything must have a designer. God designed everything. I say: Why is God the only exception? Why not make the "designs" (like man) exceptions and make God a creation of man?

                                          A Offline
                                          A Offline
                                          Andrew Drummond
                                          wrote on last edited by
                                          #28

                                          Try looking up Scope and Span in 'Code Complete'. Having a lot of distance between the variable declaration and its last usage technically increases the complexity of the code. It also decreases the readability. Having a large span/scope increases the chances of misuse of a variable, and therefore bugs. Keeping a variables scope down to a mimimum also relates to good practises like information hiding. You were wrong. It may seem more readable to you because that is what you are used to. Try programming with reduced span/scope for a few days, you will soon switch over. regards, Andrew

                                          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