Variable Declaration & Initialization :: C++

Status
You're currently viewing only Mingster's posts. Click here to go back to viewing the entire thread.
Not open for further replies.
There are a few benefits declaring local variables near the top of the function.

It encourages you to think about what you will need to use before start coding.
And that is a good thing.

Also, by concentrating all the variable declaration in one place, it is easier to notice
when the list of local variables is growing too long. (may be it is time to split the
function?)

And PeterB touches a great point: if the function is too long, and it becomes difficult to backtrack and figure out the variable type.

It can possibly forces programmers to use Hungarian Block.

But I feel that if a function is so long that it doesn't fit into one or 2 screens, I believe it is wiser to split the function. In languages where local variables must be declared ahead of time, such as Eiffel, programmers tend to write shorter functions because of that reason.

Of course, this touches style issue and is subject to great debates.

Regarding performance cost, suppose for large objects, one can also allocate by heaps, thus delaying the instantiation until very last moment. So the performance
advantage can be maintained. Is this correct?
 
Status
You're currently viewing only Mingster's posts. Click here to go back to viewing the entire thread.
Not open for further replies.