Click here to Skip to main content
15,905,782 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Coding Standards Pin
Matt U.13-Feb-14 10:37
Matt U.13-Feb-14 10:37 
GeneralRe: Coding Standards Pin
Dennis E White13-Feb-14 12:52
professionalDennis E White13-Feb-14 12:52 
GeneralRe: Coding Standards Pin
jschell14-Feb-14 10:06
jschell14-Feb-14 10:06 
GeneralRe: Coding Standards Pin
Matt U.14-Feb-14 10:10
Matt U.14-Feb-14 10:10 
GeneralRe: Coding Standards Pin
_Maxxx_13-Feb-14 16:47
professional_Maxxx_13-Feb-14 16:47 
GeneralRe: Coding Standards Pin
Stefan_Lang14-Feb-14 0:03
Stefan_Lang14-Feb-14 0:03 
GeneralRe: Coding Standards Pin
Stefan_Lang13-Feb-14 23:58
Stefan_Lang13-Feb-14 23:58 
GeneralRe: Coding Standards Pin
User 754579914-Feb-14 3:54
User 754579914-Feb-14 3:54 
I too am on a project (about a million SLOC) that forces outdated coding rules that stem from the 1970's. The project has been ongoing for almost 17 years. I have tried to change the rules to reflect more modern thinking but the tech lead won't change claiming that consistency trumps everything including common sense, and that "the customer wants it that way" even though the customer developers don't follow their own rules.

One thing that was not mentioned about function-top declarations - it wastes processor time and stack space (for those of us who develop embedded systems with constraints on such things). Why should a class object be unnecessarily declared and created at function top and destroyed upon function termination when it is only used in the very rare case of an error?

Following are justifications for function-top declaration and initialization used by our project, and my responses to them.

Justification: Uninitialized access errors are easy to catch, either by a good compiler, by proper testing, or by a memory access testing tool.
Response: Meaningless. A good compiler, proper testing, or a memory access tool will catch uninitialized access errors regardless of where the variable is declared. Furthermore, declaring and initializing a variable close to where it is first used reduces the likelihood of an uninitialized access error.

Justification: Default initialization often does not incur a large performance penalty.
Response: Maybe for simple bool or int types. But unnecessary creation and initialization of class objects, and the associated destruction of said objects, when multiplied by thousands of functions in the system, can begin to impact performance and memory usage.

Justification: Placing all declarations at the top of the block makes them easy to find.
Response: Placing declarations close to first use makes them easier to find! This may have been true back in the 1970’s when programming was done with punched cards, but today, any editor can find a variable in seconds.

Justification: In some cases, variables must be initialized within a sub-block (e.g. a variable given different values in each branch of an if/else construct), in which case the variable must be declared before initialization.
Response: The phrase “variable must be declared before initialization” implies that the variable must be declared earlier in the code, which may or may not be true, depending on the context. It is obvious that the developer must declare the variable in the next higher scope if the variable has use beyond the if/else construct. But if the variable has meaning only within the confines of the if/else construct, there is nothing wrong with the following construction which also emphasizes the short-lived nature of the variable:
C++
// i meaningless and not needed here
if ( itemCount > 0 )
{
    int i = itemCount;
    …
}
else
{
    int i = MAX_INT;
    …
}
// i meaningless and not needed here 


Justification: Maintenance often extends the usage of a variable beyond the area in which it was originally expected to be used.
Response: So? If that happens, the declaration is moved out to the next higher block scope. Furthermore, maintenance often deletes all code that references a variable, leaving a function-top variable declared but never used. A compiler may catch that if its warning level is set high enough, otherwise it will be missed and left hanging around in the code. However, if variables are declared close to first use, chances are that when the code that uses it is deleted/moved, the declaration will be deleted/moved as well.

An additional argument to be made for any { } block declarations is that over time, as the program is maintained, a variable initialized at function-top may have its value updated prior to when it was first used in the original code. For example (fictitious), when first written, myVar is function-top initialized at line 10 and first used at line 95 (85 lines away from initialization). Over time, a maintenance cycle used myVar at line 40 but failed to reinitialize it prior to its later use. A review didn’t catch it and neither did testing. The code failed in the field; the customer is angry. A scoped-down declare/initialize would/may have precluded this problem.

Variables should be initialized to a meaningful value. That's why they should not be declared and initialized until there is a meaningful value to use. Often times at function-top, you don't know what a meaningful initialization value is so it is initialized to essentially a meaningless value such as 0. In that case you might as well initialize it to a random value for all the good it does.

modified 30-Apr-21 21:01pm.

GeneralRe: Coding Standards Pin
agolddog14-Feb-14 4:11
agolddog14-Feb-14 4:11 
GeneralRe: Coding Standards Pin
Member 999720314-Feb-14 5:48
Member 999720314-Feb-14 5:48 
GeneralRe: Coding Standards Pin
Ralph Little14-Feb-14 6:10
Ralph Little14-Feb-14 6:10 
GeneralRe: Coding Standards Pin
Kirk 1038982114-Feb-14 8:12
Kirk 1038982114-Feb-14 8:12 
GeneralRe: Coding Standards Pin
Daniel R. Przybylski14-Feb-14 8:21
Daniel R. Przybylski14-Feb-14 8:21 
GeneralRe: Coding Standards Pin
Matt U.14-Feb-14 10:08
Matt U.14-Feb-14 10:08 
GeneralRe: Coding Standards Pin
James Lonero17-Feb-14 5:19
James Lonero17-Feb-14 5:19 
GeneralIs anyone else... Pin
Fredrik Bornander13-Feb-14 2:53
professionalFredrik Bornander13-Feb-14 2:53 
GeneralRe: Is anyone else... PinPopular
Duncan Edwards Jones13-Feb-14 3:03
professionalDuncan Edwards Jones13-Feb-14 3:03 
GeneralRe: Is anyone else... Pin
Nicholas Marty13-Feb-14 3:39
professionalNicholas Marty13-Feb-14 3:39 
GeneralRe: Is anyone else... Pin
Duncan Edwards Jones13-Feb-14 4:13
professionalDuncan Edwards Jones13-Feb-14 4:13 
GeneralRe: Is anyone else... Pin
Nicholas Marty13-Feb-14 4:22
professionalNicholas Marty13-Feb-14 4:22 
GeneralRe: Is anyone else... Pin
Richard Deeming13-Feb-14 3:09
mveRichard Deeming13-Feb-14 3:09 
GeneralRe: Is anyone else... Pin
Dinesh.V.Kumar13-Feb-14 3:12
Dinesh.V.Kumar13-Feb-14 3:12 
GeneralRe: Is anyone else... Pin
HobbyProggy13-Feb-14 3:49
professionalHobbyProggy13-Feb-14 3:49 
GeneralRe: Is anyone else... Pin
Nagy Vilmos13-Feb-14 3:10
professionalNagy Vilmos13-Feb-14 3:10 
GeneralRe: Is anyone else... Pin
Kornfeld Eliyahu Peter13-Feb-14 3:31
professionalKornfeld Eliyahu Peter13-Feb-14 3:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.