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.
  • M Mike Dimmick

    Ravi Bhavnani wrote:

    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.

    If the object has a constructor or destructor, the C++ standard requires that it is destroyed and a new object created on every iteration of the loop. But the compiler will construct the object in the same space each time.

    DoEvents: Generating unexpected recursion since 1991

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

    Code hoisting applies to code that can be hoisted. :) /ravi

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

    D 1 Reply 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...

      W Offline
      W Offline
      W Balboos GHB
      wrote on last edited by
      #48

      Odd comment - I've found that for managed code (somewhat common these days), if I declare outside the loop and load the values into a containers (e.g., ArrayList), they all have the same value when I'm done. Now, If I declare inside the loop, each is a newly created version and it works as expected. Roughly speaking: This will work as expected ArrayList ^tmp = gcnew ArrayList(); for(int i=0;iLength; ++i) { Object ^x = ContainerFullOfObjPtrs[i]; tmp->Add(x); } This is usually somewhat disappointing ArrayList ^tmp = gcnew ArrayList(); Object ^x = gcnew Object(); for(int i=0; i; Length; ++i) { x = ContainerFullOfObjPtrs[i]; tmp->Add(x); } "The difference between genius and stupidity is that genius has its limits." - Albert Einstein "How do you find out if you're unwanted if everyone you try to ask tells you to go away?" - Balboos HaGadol

      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?

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #49

        Since it was your opinion, it was neither right nor wrong.

        "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        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

          C Offline
          C Offline
          chaiguy1337
          wrote on last edited by
          #50

          As I recall, the way I learned Hungarian notation was developing for the mac in C++, we would use "m" to denote a member variable (eg. mUrl), and maybe "p" for a parameter, etc. Typesafe languages don't need the type as a prefix, but often there is some conflict between what kind of a variable you're using.

          “Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Want a great RSS reader? Try FeedBeast! ) |)””’) -”-”-

          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?

            T Offline
            T Offline
            Timothy W Okrey
            wrote on last edited by
            #51

            I have been programming since 83. I have worked with a whole slew of languages and OS's and I have come to some really hard fought conclusions. First of all, whatever coding standard you implement, be consistent. That way if you discover a problem with the way you are doing things, it will be consistently wrong. I recently ran into a problem that was extremely difficult to track down but it was resulting in a stack corruption. After many hours of going bleary eyed trying to read my over 500,000 lines of code I resorted to a third party code checker. One of my problems was the 'just in time' variable declaration approach. I never expected to find that if you declare a variable within a code block such as an if or a switch that the variable would go out of scope later on. While this was not my only problem it did contribute to my issue. Secondly, I have had many programmers working for me over the years and have found that the KISS principle really applies here. If I allowed my programmers to code any way that they want, I can't support the code. If I insist that the coding, variable declarations, variable naming convention etc all be done in a certain way, then it can be maintained by multiple programmers. For this reason alone, I am an ardant supporter of declaring variables at the top of a function. You may be asking how I ended up with JIT declarations then? Simple answer, other peoples code. 'With hurricanes, tornados, fires out of control,mud slides, flooding, severe thunderstorms tearing up the country! from one end to another, and with the threat of bird flu and terrorist attacks, are we sure this is a good time to take God out of the Pledge of Allegiance?' - Jay Leno

            C 1 Reply Last reply
            0
            • S Shog9 0

              Ri Qen-Sin wrote:

              so was I right when I said so?

              No. C required you to declare variables at the beginning of a function, because C (and C++) allocate all local variables when the stack frame is being constructed, and it simplified the compiler. C++ doesn't require this. Making me scan back through your code looking for a variable declaration isn't being neat. It's just being rude. I actually had to explain this to a new consultant last week, when he tried to argue that throwing a dozen variable declarations at the top of a rather large function increased efficiency by avoiding allocations in a loop. Good grief. Do people not know how to generate assembler output from their compilers anymore? :suss:

              C Offline
              C Offline
              chaiguy1337
              wrote on last edited by
              #52

              Just because there is a new feature doesn't mean it's necessarily better. There have been mistakes made in the past with "new features". In this particular case, however, I agree with you.

              “Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Want a great RSS reader? Try FeedBeast! ) |)””’) -”-”-

              1 Reply Last reply
              0
              • R Ravi Bhavnani

                Code hoisting applies to code that can be hoisted. :) /ravi

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

                D Offline
                D Offline
                Dan Neely
                wrote on last edited by
                #53

                Does being hoisted on its own petard count?

                Otherwise [Microsoft is] toast in the long term no matter how much money they've got. They would be already if the Linux community didn't have it's head so firmly up it's own command line buffer that it looks like taking 15 years to find the desktop. -- Matthew Faithfull

                R 1 Reply Last reply
                0
                • T Timothy W Okrey

                  I have been programming since 83. I have worked with a whole slew of languages and OS's and I have come to some really hard fought conclusions. First of all, whatever coding standard you implement, be consistent. That way if you discover a problem with the way you are doing things, it will be consistently wrong. I recently ran into a problem that was extremely difficult to track down but it was resulting in a stack corruption. After many hours of going bleary eyed trying to read my over 500,000 lines of code I resorted to a third party code checker. One of my problems was the 'just in time' variable declaration approach. I never expected to find that if you declare a variable within a code block such as an if or a switch that the variable would go out of scope later on. While this was not my only problem it did contribute to my issue. Secondly, I have had many programmers working for me over the years and have found that the KISS principle really applies here. If I allowed my programmers to code any way that they want, I can't support the code. If I insist that the coding, variable declarations, variable naming convention etc all be done in a certain way, then it can be maintained by multiple programmers. For this reason alone, I am an ardant supporter of declaring variables at the top of a function. You may be asking how I ended up with JIT declarations then? Simple answer, other peoples code. 'With hurricanes, tornados, fires out of control,mud slides, flooding, severe thunderstorms tearing up the country! from one end to another, and with the threat of bird flu and terrorist attacks, are we sure this is a good time to take God out of the Pledge of Allegiance?' - Jay Leno

                  C Offline
                  C Offline
                  chaiguy1337
                  wrote on last edited by
                  #54

                  Timothy W. Okrey wrote:

                  I never expected to find that if you declare a variable within a code block such as an if or a switch that the variable would go out of scope later on.

                  Well this is simply a case of being uneducated about the rules of the language you were using. I could argue that I never expected: int main( void ) { MakeAwesomeProgram(); } ...to not create the world's awesomest program, but chances are it's not going to. The rules of scope declaration are quite sound: a variable is only good in the scope it's defined in. In fact, you can even use a rarely-seen technique for defining arbitrary blocks of scope solely for this purpose: { int i = 5; ... } { int i = 10; // different 'i' ... }

                  “Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Want a great RSS reader? Try FeedBeast! ) |)””’) -”-”-

                  T 1 Reply Last reply
                  0
                  • D Dan Neely

                    Does being hoisted on its own petard count?

                    Otherwise [Microsoft is] toast in the long term no matter how much money they've got. They would be already if the Linux community didn't have it's head so firmly up it's own command line buffer that it looks like taking 15 years to find the desktop. -- Matthew Faithfull

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

                    That would be an uplifting experience. :) /ravi

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

                    1 Reply Last reply
                    0
                    • E El Corazon

                      Steve Echols wrote:

                      but if you try that in C++ you get a duplicate definition error on i.

                      That was just MS. Other compilers don't have that problem, though as mentioned MS can fix it with an option.

                      _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

                      M Offline
                      M Offline
                      Mike Dimmick
                      wrote on last edited by
                      #56

                      El Corazon wrote:

                      as mentioned MS can fix it with an option

                      ...which is enabled by default in VS2005 and later.

                      DoEvents: Generating unexpected recursion since 1991

                      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?

                        E Offline
                        E Offline
                        Ennis Ray Lynch Jr
                        wrote on last edited by
                        #57

                        Personally I Do Not Like To Pascal Case Everything Which Seems To Be The Absolutely Ludicrous Microsoft Standard. Pick style that you can be consistent with and stick to it. If others use a different style say nothing. If others use no style, then swoop down and attack from your might perch. Only in this way is coding zen obtained.

                        Need a C# Consultant? I'm available.
                        Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

                        1 Reply Last reply
                        0
                        • C chaiguy1337

                          Timothy W. Okrey wrote:

                          I never expected to find that if you declare a variable within a code block such as an if or a switch that the variable would go out of scope later on.

                          Well this is simply a case of being uneducated about the rules of the language you were using. I could argue that I never expected: int main( void ) { MakeAwesomeProgram(); } ...to not create the world's awesomest program, but chances are it's not going to. The rules of scope declaration are quite sound: a variable is only good in the scope it's defined in. In fact, you can even use a rarely-seen technique for defining arbitrary blocks of scope solely for this purpose: { int i = 5; ... } { int i = 10; // different 'i' ... }

                          “Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Want a great RSS reader? Try FeedBeast! ) |)””’) -”-”-

                          T Offline
                          T Offline
                          Timothy W Okrey
                          wrote on last edited by
                          #58

                          I think perhaps you missed my point. My point was to try and present an argument for code standardization for the sake of continued support and readability. The issue of where to define variables is not of primary concern for me. In my experience (note the qualifier), programmers have an easier time picking up on a project when the variables are delcared in one spot (at the beginning). I also believe that there are a lot of lazy programming styles out there where people don't think through their code before they write it. Functions should be small and highly compartmentalized. I realize that this is not always possible, but it should be a design consideration. If you can manage to create compartmentalized code well, then your varaible declarations will mostly occur near their first usage anyways, thus rendering the JIT/beginning argument moot.

                          'With hurricanes, tornados, fires out of control,mud slides, flooding, severe thunderstorms tearing up the country! from one end to another, and with the threat of bird flu and terrorist attacks, are we sure this is a good time to take God out of the Pledge of Allegiance?' - Jay Leno

                          1 Reply Last reply
                          0
                          • M Michael Haines

                            I used to be "top of the block" declarer, that is, until I started to do more code review/maintenance. Now I wish the declares were all JIT. The REAL messy code problem is: Long code lines requiring horizontal scrolling. MH

                            N Offline
                            N Offline
                            nalorin
                            wrote on last edited by
                            #59

                            mhaines@1amadeus.com wrote:

                            Long code lines requiring horizontal scrolling. MH

                            It's a lot better than having "dynamically wrapped" lines, and having to look at all of the "lines" of that dynamically wrapped line of code to figure out which ones are part of that line, and which ones are completely new lines altogether. That being said, lines with 80+ characters and more than one semicolon in them (i.e. more than one 'line' in a line) are the fail! - for() loops included! :wtf:

                            "Silently laughing at silly people is much more satisfying in the long run than rolling around with them in a dusty street, trying to knock out all their teeth. If nothing else, it's better on the clothes." - Belgarath (David Eddings)

                            1 Reply Last reply
                            0
                            • C Chris Maunder

                              Code readability (and set standards) is fundamental to developing consistent, maintainable code. Picking a good, comprehensive standard way of coding means less chance of errors appearing because different parts of the code will be doing things in a similar fashion so there's less chance of conflict. Having one standard of writing code means that when you are browsing code it will all look the same so errors through incorrect implementation will be easier and faster to spot. It's not where you declare your variables that matters. It's how readable and maintainable your code is. Personally I prefer to declare my variables as close to their first use as possible. Other like them all sitting, huddled, at the top of their functions, too scared to mingle with the rest of the crowd. It's kind of like the kitchen of the function.

                              cheers, Chris Maunder

                              CodeProject.com : C++ MVP

                              N Offline
                              N Offline
                              nalorin
                              wrote on last edited by
                              #60

                              Chris Maunder wrote:

                              Code readability (and set standards) is fundamental to developing consistent, maintainable code.

                              But then again, there's always the option of coding to add job security.[^] :laugh:

                              "Silently laughing at silly people is much more satisfying in the long run than rolling around with them in a dusty street, trying to knock out all their teeth. If nothing else, it's better on the clothes." - Belgarath (David Eddings)

                              D 1 Reply Last reply
                              0
                              • S Shog9 0

                                Ri Qen-Sin wrote:

                                so was I right when I said so?

                                No. C required you to declare variables at the beginning of a function, because C (and C++) allocate all local variables when the stack frame is being constructed, and it simplified the compiler. C++ doesn't require this. Making me scan back through your code looking for a variable declaration isn't being neat. It's just being rude. I actually had to explain this to a new consultant last week, when he tried to argue that throwing a dozen variable declarations at the top of a rather large function increased efficiency by avoiding allocations in a loop. Good grief. Do people not know how to generate assembler output from their compilers anymore? :suss:

                                N Offline
                                N Offline
                                nalorin
                                wrote on last edited by
                                #61

                                Shog9 wrote:

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

                                Perhaps I need to look this up :P

                                Shog9 wrote:

                                Making me scan back through your code looking for a variable declaration isn't being neat. It's just being rude.

                                I think the degree of neat/rudeness depends on the coder's personal taste. I, personally, prefer (in most cases) variables declared/initialized at the beginning of their scope. This way, I can see all the information about all the variables in the scope. I find it much easier to debug this way, since I can immediately rule out the declaration and initialization of my variables as the cause of a problem, and seems to help me prevent conditional initializations of variables like: int x; if (sloppy_joes == good) x = 47; If the 2 lines above are sandwiched between 50 lines of code on either side, I've often forgotten in the past that x may not always get initialized, and will think "x is declared here... and is initialized to 47 there. What the heck is wrong?!" If initialization/declaration are placed at the beginning, I think more like: "x is declared here, initialized on the next line, and used down there." I can't help but agree that code is just more readable with variable declarations (in most cases) placed at the top of the variable's scope - but that's just my take on the subject.

                                "Silently laughing at silly people is much more satisfying in the long run than rolling around with them in a dusty street, trying to knock out all their teeth. If nothing else, it's better on the clothes." - Belgarath (David Eddings)

                                S R 2 Replies 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
                                  avoidingwork2
                                  wrote on last edited by
                                  #62

                                  Short Version: Accepting change and trying to work with it broadens you abilities. Long Version: I started coding with all my variables declared at the top. The were grouped by type and then sorted alphabetically. Luckily we used brief and had macros that would sort out a highlighted section of code for us. The language required pre-declaration so there was no use arguing about where we declared our variables only that is was organized the same throughout the department. I then started developing in C++ and noticed that I had a hard time reading code where the variables were declared JIT. I didn't like it, although there wasn't anything wrong with the code, it was just different for me to work with. Finding declarations required a new process and I wasn't happy about about changing. Now several languages, programming shops, and standards later I find I can't be so ridged in my ways, change is a part of the job. Nowadays I find that I am doing both depending on the variable needs. I have noticed that JIT declaration is a little easier to refactor code, but at the same time when I have to pre-declare my variables I find that I still group them. Some habits are hard to break. And that is the crux of this whole thread. It's a habit how you lay your code out. There is no right way or wrong way. The main thing to remember is to not be candidate for the code obfuscation contest. Remember, you can take 20 lines of code and compress it down to 2 but the next time you read it you wont know what you were trying to do. :) Sorry for letting this ramble on so long.

                                  1 Reply Last reply
                                  0
                                  • N nalorin

                                    Shog9 wrote:

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

                                    Perhaps I need to look this up :P

                                    Shog9 wrote:

                                    Making me scan back through your code looking for a variable declaration isn't being neat. It's just being rude.

                                    I think the degree of neat/rudeness depends on the coder's personal taste. I, personally, prefer (in most cases) variables declared/initialized at the beginning of their scope. This way, I can see all the information about all the variables in the scope. I find it much easier to debug this way, since I can immediately rule out the declaration and initialization of my variables as the cause of a problem, and seems to help me prevent conditional initializations of variables like: int x; if (sloppy_joes == good) x = 47; If the 2 lines above are sandwiched between 50 lines of code on either side, I've often forgotten in the past that x may not always get initialized, and will think "x is declared here... and is initialized to 47 there. What the heck is wrong?!" If initialization/declaration are placed at the beginning, I think more like: "x is declared here, initialized on the next line, and used down there." I can't help but agree that code is just more readable with variable declarations (in most cases) placed at the top of the variable's scope - but that's just my take on the subject.

                                    "Silently laughing at silly people is much more satisfying in the long run than rolling around with them in a dusty street, trying to knock out all their teeth. If nothing else, it's better on the clothes." - Belgarath (David Eddings)

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

                                    nalorin wrote:

                                    I, personally, prefer (in most cases) variables declared/initialized at the beginning of their scope.

                                    Generally, i do as well. But if a variable is used only in a loop, it should be declared and initialized (ideally both of these together) within that loop, same for variables that are only used within a conditional block, etc. I prefer that the scope is limited to as small a chunk of code as possible; ideally, i can read the variables used and the operations they're used in without having to scroll or turn the page. And frankly, when i'm unable to limit scope, i'll still err on the side of declaring variables where they're used rather than immediately after the scope begins. Yes, this can introduce errors if variables are re-used, but IMHO that's just an argument in favor of limiting variable re-use.

                                    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
                                      TheGreatAndPowerfulOz
                                      wrote on last edited by
                                      #64

                                      AmazingMo wrote:

                                      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.

                                      wrong. most modern compilers figure out the maximum stack allocation at function entry and allocate that stack space all at once. the reason for not delcaring a non-primitive inside a loop is the constructor overhead.

                                      Silence is the voice of complicity. Strange women lying in ponds distributing swords is no basis for a system of government. -- monty python Might I suggest that the universe was always the size of the cosmos. It is just that at one point the cosmos was the size of a marble. -- Colin Angus Mackay

                                      1 Reply Last reply
                                      0
                                      • N nalorin

                                        Chris Maunder wrote:

                                        Code readability (and set standards) is fundamental to developing consistent, maintainable code.

                                        But then again, there's always the option of coding to add job security.[^] :laugh:

                                        "Silently laughing at silly people is much more satisfying in the long run than rolling around with them in a dusty street, trying to knock out all their teeth. If nothing else, it's better on the clothes." - Belgarath (David Eddings)

                                        D Offline
                                        D Offline
                                        Dan Neely
                                        wrote on last edited by
                                        #65

                                        Choosing The Best Overload Operator : In C++, overload +,-,*,/ to do things totally unrelated to addition, subtraction etc. After all, if the Stroustroup can use the shift operator to do I/O, why should you not be equally creative? If you overload +, make sure you do it in a way that i = i + 5; has a totally different meaning from i += 5; Here is an example of elevating overloading operator obfuscation to a high art. Overload the '!' operator for a class, but have the overload have nothing to do with inverting or negating. Make it return an integer. Then, in order to get a logical value for it, you must use '! !'. However, this inverts the logic, so [drum roll] you must use '! ! !'. Don't confuse ! operator, which returns a boolean 0 or 1, with the ~ bitwise logical negation operator. I think I'm missing something here, how exactly is the ! operator supposed to be implemented to get the desired result from !!!?

                                        Otherwise [Microsoft is] toast in the long term no matter how much money they've got. They would be already if the Linux community didn't have it's head so firmly up it's own command line buffer that it looks like taking 15 years to find the desktop. -- Matthew Faithfull

                                        N 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?

                                          S Offline
                                          S Offline
                                          Sogar Gofin
                                          wrote on last edited by
                                          #66

                                          I've been perpetually seeking the "cleanest" way to code. Here are my thoughts: In C, one doesn't need to consider subtle features like constructor/destructor behavior, operator overloading, inheritance, generic programming, C++ exceptions, and the like. In C++, these possibilities may associate extra functionality (e.g resource aquisition is initialization, RAII) with the declaration of an object, and also with the departure from the object's scope. Say you declare an object variable inside a function or method whose usage is sometimes skipped over. Say also that the object's constructors are designed to acquire some system resource (e.g. a lock, not uncommon in multi-threading ). If you've declared the object at the top of your routine, you'd be unnecessarily acquiring that resource (e.g if the shared data the lock protects isn't referenced ). This can manifest as unnecessary delay or resource starvation. This justifies declaring some object-types close to their usage. As for basic types: since it's already justified for some objects, you may as well do it with basic types to keep standards consistent and code modular. This also makes it easier to break a big routine into sub-routines. Not to mention, you never know if a future code refactor may turn a basic type into an object. As I've experienced, you may need to change your doubles into special fixed-point objects to run better on particular machines ... like the UltraSPARC T1000 ... C++ is subtle. IMO, the compiler ought to be explicit about how much of this it automatically handles. See: http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization[^] - Charles Rojo Software Engineer

                                          Rojo

                                          modified on Monday, March 10, 2008 11:42 PM

                                          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