Go Down

Topic: Define after use? (Read 1 time) previous topic - next topic

bit-twiddler

May 12, 2012, 07:58 pm Last Edit: May 12, 2012, 08:17 pm by bit-twiddler Reason: 1
Why is "define after use" coding so prevalent in the Arduino community?  

dxw00d

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

bit-twiddler

#2
May 12, 2012, 08:15 pm Last Edit: May 12, 2012, 08:20 pm by bit-twiddler Reason: 1
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.  

robtillaart


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
Rob Tillaart

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

tgm1175

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: [Select]


function doStuff();

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

void loop()
{
 doStuff();
}

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


Arduino Way:

Code: [Select]


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

void loop()
{
 doStuff();
}

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



dxw00d


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.

bit-twiddler

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.



AWOL

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

jwatte

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.

The Clever Monkey

#9
May 13, 2012, 04:31 am Last Edit: May 13, 2012, 02:44 pm by The Clever Monkey Reason: 1

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.
I yield() for co-routines.

kf2qd

#10
May 13, 2012, 04:39 am Last Edit: May 13, 2012, 04:41 am by kf2qd Reason: 1
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...

MarkT

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.
[ I won't respond to messages, use the forum please ]

robtillaart

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 .

Rob Tillaart

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

Grumpy_Mike

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.

Go Up