A question about style I think

Below is a snip of code. You'll notice the programmer often puts a ; after a }.

looking like };

Is this a style thing or does it serve a function?

--snip--

void loop()
{
int incomingByte=0;//create and initialize a local variable

//The "then" part of the following will only execute
// after you have sent something to the Arduino
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.print(incomingByte, DEC);
if (incomingByte==102) {boFast=true;
Serial.println(", code for f. LED will wink fast.");};
if (incomingByte==115) {boFast=false;
Serial.println(", code for s. LED will wink slowly.");};
if ((incomingByte!=102)&&(incomingByte!=115)) {
Serial.println(". Only lower case s and f do anything.");};
}

//The following happen every time you pass through "loop"
if (boFast) {fastwink();};
if (!boFast) {slowwink();};
};

That's hideous! No, you don't need those semi-colons, they just add an empty statement.

Andrew

No, that's really not "style".

  if (incomingByte==102)

Not many of us carry around an ASCII-to-decimal table with us, so if (incomingByte== 'f') is much easier to read.

if (boFast)  {fastwink();};
if (!boFast) {slowwink();};

"else" is so much simpler.

That's quite the most horrible bit of C I've seen in a while.

Thanks for the feedback guys.

I'm not the most accomplished programmer around (actually I'm a hardware engineer so I like to think I have an excuse), but that code just didn't read well.

I learned what I needed to learn from it but it took some effort.

Thanks again...

I'm not the most accomplished programmer around (actually I'm a hardware engineer so I like to think I have an excuse),

That sure works for me. Don't let the softheads push you around :wink:

Lefty

     if (incomingByte==102) {boFast=true;
               Serial.println(", code for f. LED will wink fast.");};
       if (incomingByte==115) {boFast=false;
               Serial.println(", code for s. LED will wink slowly.");};
       if ((incomingByte!=102)&&(incomingByte!=115)) {
            Serial.println(". Only lower case s and f do anything.");};

This would be better as a "switch" statement:

switch (incomingByte) {
     case 'f':
     // no { } needed after case
               boFast=true;
               Serial.println(", code for f. LED will wink fast.");
               break;  // Important to have break at end of every case
 
    case 's':
               boFast=false;
               Serial.println(", code for s. LED will wink slowly.");
               break; 

      default:
           // This is run if none of the cases matched.  It must be the 
           // last case in the list.
            Serial.println(". Only lower case s and f do anything.");
           break;  // This break technically isn't needed.
}

The only thing that I could think of is that they might be using a different compiler... They might be not using optimisation and inserting a "}" charactor to see what the compiler does line by line...

If there was no reason for it, you should stop getting code off that person.

There is one case where a semicolon is REQUIRED following the }, and that is in the definition of a class:

class MuClass
{
  // Members and fields
}[glow];[/glow]

Other than that one case, often missed by new and experienced coders, the ; is not required following the }.

Don't forget 'struct's.

Don't forget 'struct's.

I usually do.

Other than that one those two cases, often missed by new and experienced coders

Also, unions, and at the end of a multi-element initializer.

-Mike

; is not required following the }.

It's more than "not required." It does something specific (creates an empty "statement") that is sometimes quite wrong. For example, the following will produce compile errors

if (a == b) { serial.print(a); };
  else { serial.print(b); };

I'm not the most accomplished programmer around (actually I'm a hardware engineer so I like to think I have an excuse)

I've often wondered how much this kind of attitude, which is fairly prevalent in both professions (hardware engineering vs. software engineering), has actually hampered progress in both fields...?

This isn't a unique situation; in many human endeavors this kind of competition, rivalry, and specialization - and the attitudes it engenders - has led to a lack of progress that was only broken when those involved worked together, and tried to understand and learn from each other the skills and knowledge that they individually lacked.

Its an ever greater tragedy when it comes to anything related to computers and computer science; the inter-relatedness of hardware and software is such that to me, it makes no sense to specialize in one or the other. IMHO, I believe you honestly can't understand and know one of the sides without understanding and knowing the other; they are the inseparable sides of a coin, so to speak.

Finally, I believe that the understanding of the history of computation, as well as the possible (likely?) future of it, is something that none of us should ignore.

:slight_smile:

I'm with you, however, I found out quite early in my engineering education, that it was very hard to be masterful in all aspects of the realm.

that it was very hard to be masterful in all aspects of the realm.

IMHO too many software only guys seem to only think of the world as only true and false, high and low, on or off. While the hardware guys understand that natural variations and tolerances means you can only hope to be within nominal limits. ;D