Do these people get paid by the LOC?

This is a slightly edited example of source (not Arduino) of someone else's code

void initDisplayDesc (displayDesc* pDesc)
{
  void* addr;
  unsigned long size;

  size = FB_SIZE;
  addr = alloc_aligned (size, 4096);
  ...
}

Why on Earth not simply

void initDisplayDesc (displayDesc* pDesc)
{
  unsigned long size = FB_SIZE;
  void* addr = alloc_aligned (size, 4096);
  ...

}

?
Is there any justification for this style (or lack of it)?

It's a programming convention followed by a lot of people/companies that any local variables used in the function are declared at beginning of function and not declared as needed. Aids debugging and reading other peoples code.

I don't get the bug argument, and surely it is better to have variables initialised as soon as they are declared?

That's what C used to be like when I first used it. There were no initializers back then, and all variable declarations had to be at the beginning of a function. You couldn't just put int x=42; in the middle of a function like you can now.

Given the title, it looks as if the OPs complaint is with initialisation on a separate line rather than anything to do with where the variables are declared.

Duane B

Cars used to have crash boxes, but we don't teach people to double-declutch.
C has allowed initialisers for years now, surely?

Some mistakes were built to last:

-br

Groove:
Cars used to have crash boxes, but we don't teach people to double-declutch.
C has allowed initialisers for years now, surely?

The automatic gearbox has been around for about 100 years, yet people still drive manual :slight_smile:

I don't see the point you're trying to make stimmer.
Automatic boxes are for people with more money than sense, or who can't be bothered to learn to drive properly.

Automatic boxes are for people with more money than sense, or who can't be bothered to learn to drive properly.

Or have a physical disability that prevents them from driving a manual, like my partner. She did learn to drive 'properly' but later had the choice of restriction to automatics, or giving up her driving licence competely.

Groove:
I don't see the point you're trying to make stimmer.
Automatic boxes are for people with more money than sense, or who can't be bothered to learn to drive properly.

Maybe I've been in a hole for too long but I just have not heard how to manually drive my continuously variable transmission to get good gas mileage yet.

Groove:
I don't see the point you're trying to make stimmer.
Automatic boxes are for people with more money than sense, or who can't be bothered to learn to drive properly.

And I have found my new best friend! 90% of my cars have been manual. Automatics are just, boring..

Of course, my other hobby is racing stock cars..

Groove:
Is there any justification for this style (or lack of it)?

There are subtle differences to the two methods you presented. For example, give a class "myClass":

myClass B;
myClass A = B;   // make an instance of A based on B

The above calls the copy constructor for A.


myClass B;
myClass A;
A = B;   // copy B to A

The above calls operator= for A.


So they do different things. Admittedly for simple types there won't be much, if any, difference.

I think there is an argument for separating out the declaration from the initialization. Example:

int i = 10;
while (i--)
  {
  // do something
  }

Later on you need to do that again (in the same function) so you copy and paste:

int i = 10;
while (i--)
  {
  // do something
  }

Oops! Duplicate declaration of "i". Now if the declaration had been on its own line you just copy the initialization downwards and not the declaration. Or you fudge it like this for the second loop:

i = 10;
while (i--)
  {
  // do something
  }

Now one loop has the declaration and one doesn't which looks asymmetrical.

Why on Earth not simply

  unsigned long size = FB_SIZE;

void* addr = alloc_aligned (size, 4096);

I've worked in groups where the prevailing "style guide" recommended against combining variable initialization with variable declarations. I forget the exact justifications; there are people who like it better simply from a "compiler purity" point of view ("Declarations should not be mixed with code.") Like many "elements of programming style", it's something not worth fighting for (in either direction), IMO.

It's not like the code produced is going to be different.

Depends on when and where you learned.

Some aspects of style are for ease of reading. Some are because they help reduce mistakes. Some are because someone in charge (and signs off on the paycheck) decides that's how it will be done.

Is one better than the other? Apparently you feel it shold be done one way and others feel it should be done another.

Defined and Initialized variables - how will they do when they aren't used because the code changed. Won't get the little message, declared but unused... So there is some memory that is plugged up for nothing. Compiling for a PC, Who Cares??? for an Arduino - might make a difference. What's the difference at execution time? That's what should really matter.

Personally, I like to keep the assignment of values away from the declaration unless its a CONSTANT... As well as the fact that C isn't my only programming language.