1

I'm probably overthinking this, and have been coding on and off a few months but am looking for best practice advice on something very simple. Question: Is it frowned upon on do something like this?

uint8_t dotPos = GetDotPos(&buffer[0], len);

or is it better practice (as is often seen in examples) to do this:

uint8_t dotPos = 0;
dotPos = GetDotPos(&buffer[0], len);

Or is there no preference?

5 Answers5

3
const uint8_t dotPos = GetDotPos(&buffer[0], len);

is the better practice. Variable declaration and initialization should be close together for code readability. Additionally, if the variable is not changed, set it to const. Following this convention greatly simplifies flow analysis of the code in our heads.

From https://google.github.io/styleguide/cppguide.html#Local_Variables :

Place a function's variables in the narrowest scope possible, and initialize variables in the declaration.

int i;
i = f();      // Bad -- initialization separate from declaration.

int i = f();  // Good -- declaration has initialization.

Also https://wiki.c2.com/?DeclareVariablesAtFirstUse .

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
2

"Is it frowned upon on do something like..."
"or is it better practice..."
"Or is there no preference?"

The phrasing of your question suggests you may already have a natural preference. Try it out.
Although being open to learning new information regarding syntax and semantics is good, be careful from where, or whom you will accept that information. Often programmers, albeit with good intentions will suggest idiomatic constructs when teaching (or persuading) another to form habits in a manner suitable to a particular point of view. However be careful when accepting ideas, in particular when they are narrowly scoped, and use language with too many absolutes. (always do it this way, never use ... ) Smoothly presented but overly pedantic mandates should be scrutinized before settling on them.

Your question addresses a very specific scenario, but consider other scenarios you are likely to encounter to fully address how/when to create and initialize variables...

  • will variable also be used as an argument to the function?
  • Is the variable required to have global scope?
  • or, is it intended to have local (automatic) scope?
  • will variable be used in a threaded environment?

Each of these will factor into how you create, initialize, and perhaps maintain the integrity of your variable, thus will likely cause you to once again consider new information.

ryyker
  • 22,849
  • 3
  • 43
  • 87
0

Both are extensively used. Some people may prefer one or the other and it's totally a matter of taste. Some people may argue that the later allow to declare all function variable on the top of the function which can be considered cleaner.

Puck
  • 2,080
  • 4
  • 19
  • 30
  • 1
    Code style is not totally a matter of taste. Humans make more or fewer errors with some code styles than with others. Studies can reveal these issues and provide some objective information about which code styles are better. – Eric Postpischil Jul 03 '23 at 12:34
  • 2
    If you wrote C code in about 1973, you **had** to declare all variables first. This was not a preference, or chosen for clarity, but a limitation of the compilers at the time. – BoP Jul 03 '23 at 12:34
0

Usually the compiler does code optimization like Constant Folding and Constant Propagation so the assignment should not affect performance.

It is up to you which method you prefer or if your organization specifies a certain method.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Snehil Shah
  • 333
  • 1
  • 10
0

It depends on the scope of "i" that you want. If you are setting "i" in a loop or a condition and plan to use it outside then you need to have the declaration prior to it and outside of the loop/scope {}, else the compiler will complain as soon as you reference the same variable without a declaration.

Otherwise the single statement for declaration and assignment is correct and optimal.

Also if the variable is a pointer to a memory location then you definitely need to call a memset/calloc to clear the location prior to assignment as a best practice because you dont know what is lurking in the pointed location.