Pages: [1]   Go Down
Author Topic: Define after use?  (Read 1137 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why is "define after use" coding so prevalent in the Arduino community?  
« Last Edit: May 12, 2012, 01:17:04 pm by bit-twiddler » Logged

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

What do you mean? What is "define after use"?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In software engineering, "define after use" is the practice of using a function or variable before it has been defined.    I am amazed at the number of sketches that I see that use a function in "loop" or "setup" before it has been defined.  
« Last Edit: May 12, 2012, 01:20:21 pm by bit-twiddler » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13917
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The Arduino IDE takes care of the prototyping. If you look in the preprocessed C++ file it should all be fine.

If you hold the shift key when uploading you see more info where the intemediate files are located
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pennsylvania, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Because most of the people on here aren't experienced C++ programmers.  

Processing (arduino's language) creates the function headers for you, which is why define after use works.

I was taught C++ in college, so  even though its not necessary,  I declare my functions before defining them...

C++ way:

Code:

function doStuff();

void setup()
{
Serial.begin(9600);
}

void loop()
{
  doStuff();
}

function doStuff
{
  Serial.print("Hello World!");
}

Arduino Way:

Code:

void setup()
{
 Serial.begin(9600);
}

void loop()
{
  doStuff();
}

function doStuff
{
  Serial.print("Hello World!");
}

Logged

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

In software engineering, "define after use" is the practice of using a function or variable before it has been defined.    I am amazed at the number of sketches that I see that use a function in "loop" or "setup" before it has been defined.  

Ah. get it. What the others said.

I was only ever a programmer, not a software engineer. I didn't know it had a name.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am "old school."  I prefer to design and implement software "bottom up."   It makes unit and integration testing much easier.  Bottom-up coding also eliminates the need to flip back and forth between pages on a long source code listing because one already knows what a function or class method does before one sees a reference to it in the code.  Define-before-use is akin to defining all of terms in a document before they are used.


Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26525
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I prefer to design and implement software "bottom up."
Have you found anything about the Arduino environment that prevents this?
(Apart from the well documented bug related to references, but if you're old-school C, you won't care about references)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
God Member
*****
Karma: 4
Posts: 813
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The main problem I have with the IDE is that it makes breaking your longer projects apart into separate files for separate modules harder than it should be. I find I end up writing a bunch of one-file libraries, for what might in another system be separate class files that are still private to the particular project being built.
Logged

Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 246
Code Monkey
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am "old school."  I prefer to design and implement software "bottom up."   It makes unit and integration testing much easier.  Bottom-up coding also eliminates the need to flip back and forth between pages on a long source code listing because one already knows what a function or class method does before one sees a reference to it in the code.  Define-before-use is akin to defining all of terms in a document before they are used.

Arduino/Processing is primarily about RAD and providing pluggable components for a wide audience.

You can still do most of what you want to do in the Arduino IDE. But, honestly, if it matters a lot you are going to switch to using some other environment -- either one from Atmel or something free built on avr-lib etc.
« Last Edit: May 13, 2012, 07:44:11 am by The Clever Monkey » Logged

I yield() for co-routines.

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

The Arduino environment is configured so someone with little experience can get something done without having to learn a bunch of "rules" that make programming harder to learn.

While some think that certain techniques should be rigidly enforced, it would probably cause a lot of frustration that would demotivate beginners from using the device. While some in the programming community find this disconcerting, the mentallity of the Arduino group is that being able get results is more important than following certain rules.

I have a degree in Computer Science and my personal feelings is that some choose to make the rules more important than getting anything done. Arduino programs tend to be small. As such some of the techniques and rules are not necesary.

Relax - if you feel those techniques and rules are necesary, by all means use them. Peersonally I don't need another rule bound programming environment to frustrate me.

I also use Assembly Language - and JMP is just another name for GOTO...
« Last Edit: May 12, 2012, 09:41:38 pm by kf2qd » Logged

0
Online Online
Shannon Member
****
Karma: 216
Posts: 12553
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Surely "declare before use" is a hack in the language to allow a one-pass compiler?  Its certainly not there to make the programmer's life easier.  This is an area where the Java toolset gets things right, and makes C and C++ linkage look like the dinosaur it is.
Logged

[ I won't respond to messages, use the forum please ]

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13917
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Surely "declare before use" is a hack in the language to allow a one-pass compiler?

It is indeed a relic from the days that a multipass compiler would take "years". It is not only C, also good old Turbo Pascal used it for type checking .

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 637
Posts: 34581
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
my personal feelings is that some choose to make the rules more important than getting anything done.
I agree 100% with this, I find the arduino refreshing in this respect.
Logged

Pages: [1]   Go Up
Jump to: