1

I have quite a lot of experience programming, but over the years of constantly writing functions, I just wanted to know the communities opinion on this matter is.

Under a function is the best place to declare all the variables be at the very beginning or declaring them as you go?

So for example:

void fake_function1() {
    int i;
    //do something here with variable i

    int counter;
    //do something here with variable counter

 }

 or

void fake_function2() {
    int i;
    int counter;

    //do something here with variable i
    //do something here with variable counter

 }

As of now I tend to usually do things like in fake_function2() since that seems more correct, but some other times I would do things like in fake_function1() since it looks more readable and readable code is always better code especially when code can run over 100k lines easily in my opinion. I think consistency is very important, but I am having a hard time deciding which is better.

StevenTsooo
  • 498
  • 4
  • 13
  • possible duplicate of [Declare local variables as late as possible or at the nearest curly brace they belong?](http://stackoverflow.com/q/10204828/1503018) – sectus Jul 02 '13 at 03:31

3 Answers3

2

A rule of thumb that has really worked for me, is:

Declaration is initialization

This means that even statements like int i; are to be avoided in favor of int i = 0;.

Bringing the code that declares together with the code that handles the variable really only merits maintainability.

Also note that declaring variables at the top is a practice that might stem from the time that C did not allow mixed code and declarations.

Community
  • 1
  • 1
Grimace of Despair
  • 3,436
  • 26
  • 38
1

The general rule for most good coding practices guides that I've seen (I've only read a few), is to declare them as close as possible to the location of first use, and to initialize them if they aren't default-constructed. This greatly reduces the chance of them being accidentally used without first being initialized, reducing the liklihood of bugs, without any increase in code or any hit to performance.

That said, I like to put local constants at the beginning of the function.

Jamin Grey
  • 10,151
  • 6
  • 39
  • 52
0

I understand and agree with your points and with your feeling that the second style seems more correct.

Although it may help the author of the code keep their thoughts together at first to use the style of the first function, it helps both others, and the author later on (when they return to maintain their code), if variables are declared as soon as their scope begins, which for JavaScript is at the top of the function, and for other languages is at the beginning of a block. This can seem cumbersome, but if the number of variables gets so cumbersome, then perhaps the function or blocks themselves ought to be shortened, just like good writers tend to write long sentences, but even better writers usually master the art of shortening sentences back down again.

Note well: regardless of what style is used, it is good to write code in which each variable exists only as long as necessary.

Joseph Myers
  • 6,434
  • 27
  • 36