Go Down

Topic: Do these people get paid by the LOC? (Read 1 time) previous topic - next topic

Groove

This is a slightly edited example of source (not Arduino) of someone else's code
Code: [Select]
void initDisplayDesc (displayDesc* pDesc)
{
  void* addr;
  unsigned long size;

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

Why on Earth not simply
Code: [Select]
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)?
Per Arduino ad Astra

Riva

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.
http://forum.arduino.cc/index.php?action=unread;boards=5,67,10,11,66,12,15,17,21,22,23,24,25,29;ALL

Groove

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

stimmer

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.
Due VGA library - http://arduino.cc/forum/index.php/topic,150517.0.html

DuaneB

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
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

Groove

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

billroy

Some mistakes were built to last:

http://stackoverflow.com/questions/218123/what-was-the-strangest-coding-standard-rule-that-you-were-forced-to-follow

-br

stimmer


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 :)
Due VGA library - http://arduino.cc/forum/index.php/topic,150517.0.html

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.
Per Arduino ad Astra

dxw00d

#9
Dec 05, 2012, 04:00 pm Last Edit: Dec 05, 2012, 04:03 pm by dxw00d Reason: 1
Quote
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.

liudr


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.

codlink


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..
//LiNK

Nick Gammon


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":

Code: [Select]

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


The above calls the copy constructor for A.




Code: [Select]

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:

Code: [Select]

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


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

Code: [Select]

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:

Code: [Select]

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


Now one loop has the declaration and one doesn't which looks asymmetrical.
http://www.gammon.com.au/electronics

westfw

Quote
Why on Earth not simply
Code: [Select]
  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.


kf2qd

#14
Dec 07, 2012, 07:39 pm Last Edit: Dec 07, 2012, 07:41 pm by kf2qd Reason: 1
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.

Go Up