Code Neatness
-
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?
Declaring variables at the top of a function or program is bad idea. Consider the following:
void function()
{
int i;// use 'i' for purpose 1 ... // use 'i' for purpose 2 ... // use 'i' for purpose 3 ... if(i == 14) { // crash the program }
}
At the bottom of the function, 'i' is the accidental work product of the last place that it was used. Suppose this function is 200 lines long? Is the user supposed to hand examine every single place a variable is modified in order to understand what its value is currently being used for? Instead, declare variables as close to the point that they are actually used and make sure that they go out of scope as quickly after that as possible to reduce confusion to later maintainers. Confusion is the root of all evil in s/w. Lowell used.
-
I am already using two monitors, so I am talking about REALLY long lines! :-D
mhaines@1amadeus.com wrote:
I am already using two monitors, so I am talking about REALLY long lines!
so use two 30" monitors.... come on, use it as an excuse... you know you want them!
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
-
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?
Ri Qen-Sin wrote:
I'm trying to seek agreement… so was I right when I said so?
yes, and no. If your code loops or branches, it matters where you declare your variables. For instance declaring all your variables up front pulls a lot of space off the stack or heap, creates overhead, and if you branch or exit early, you have created unnecessary overhead. If your code branches with an if or a switch, you never want to incur the overhead associated with unused branches. on the other side, declaring a variable inside of a loop means that this variable will be allocated and deallocated often. By moving the declaration outside of the loop, your cost is significantly less performance wise. BUT! The rule above still applies. You do not have to move it to the front of the function, though you can if you don't branch or exit early.
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
-
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!
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)
-
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:
Shog9 wrote:
Good grief. Do people not know how to generate assembler output from their compilers anymore?
No, most do not.... :sigh: :sigh:
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
-
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
-
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...
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
-
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?
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
-
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
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! ) |)””’) -”-”-
-
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?
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
-
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:
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! ) |)””’) -”-”-
-
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
-
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
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! ) |)””’) -”-”-
-
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
-
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)
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
-
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?
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 -
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! ) |)””’) -”-”-
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
-
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
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)
-
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
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)
-
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:
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)