Pages: [1]   Go Down
Author Topic: Do these people get paid by the LOC?  (Read 1474 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

  size = FB_SIZE;
  addr = alloc_aligned (size, 4096);
  ...
}
Why on Earth not simply
Code:
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)?
Logged

Per Arduino ad Astra

Norfolk UK
Offline Offline
Edison Member
*
Karma: 66
Posts: 2475
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Per Arduino ad Astra

Offline Offline
God Member
*****
Karma: 32
Posts: 507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Per Arduino ad Astra

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged


UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Per Arduino ad Astra

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: December 05, 2012, 10:03:30 am by dxw00d » Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


USA, FL
Offline Offline
God Member
*****
Karma: 14
Posts: 688
A life? Where can I download one of those?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


* IMG_0373.JPG (2134.87 KB, 2592x1936 - viewed 32 times.)
Logged

//LiNK

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
myClass B;
myClass A = B;   // make an instance of A based on B

The above calls the copy constructor for A.



Code:
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:
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:
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:
i = 10;
while (i--)
  {
  // do something
  }

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

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6652
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1024
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: December 07, 2012, 01:41:52 pm by kf2qd » Logged

Pages: [1]   Go Up
Jump to: