Your colleague's point is from old school programming. Not that it is bad, but this comes from assembly language.
At the beginning of an assembly code, you had to declare the DS
register (Data Segment) which made some allocation on the stack in order to reserve some memory. Here is a code sample:
.MODEL SMALL
.DATA
myVariable1 DW "This is my first variable"
myVariable2 DB "Second var"
.CODE 'code segment
:ProgramStart
MOV AX,01h
...
:ProgramEnd
As the others have already said, in C programs, the variable declarations had to be in the beginning of a function. Just like you needed to write your functions definitions in the beginning of your C program, before the main()
function, then writing their body after your main function, otherwise your function (or variable) was not recognized. So yes, this was faster because the variable address was already known by the generated inline code.
However, with the coming of OOP and event programming, we tend to stay as close to the event as possible. What I mean resumes itself being your point of view. Today's compilers parse the code for such variables throughout the code and make it a manageable CLR code. So this no longer make a difference, speaking of performance. In today's programming, we tend to make the code as easy to read and understand as possible. That said, this includes variable declarations.
Declaring a variable in the beginning of a function, and using it only 20 lines further is no use, plus it tends to make the program harder to understand. For example, you're analyzing a piece of code, then hop! a variable name appears, but no declaration around. Where this variable comes from, shall you ask? You will need to scroll up (or go to the definition) to find out what is this variable. Meanwhile, you may have lost your idea about the purpose of the routine you were actually reading and trying to understand. Make it three or more times, and your lost trying to understand the code.
It's better to have a lean and swift point of view of the purpose of the code. That is, don't declare any unecessary variable before it really is indispensable. Writing this way will avoid one always scrolling up and down while reading or writing the code of a program.
This is my two cents. I hope to be clear enough in what I have been trying to explain.
I hope this helps anyway!