View Single Post
  #17   Spotlight this post!  
Unread 06-01-2007, 09:00
Bongle's Avatar
Bongle Bongle is offline
Registered User
FRC #2702 (REBotics)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Waterloo
Posts: 1,069
Bongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond repute
Send a message via MSN to Bongle
Re: Why it works with an underscore?

I'll also point out that #defines, although pretty useful in some cases, can make code very hard to read, which is important if you ever have someone helping you with it.

For example, your Setting_1 thing affects a LOT of variables, but someone coming along and reading the code would have no idea. They'd see "Setting_1;". It wouldn't look like a function and it wouldn't have any apparent assignments, but because of the #define it actually affects 6 different variables. At the very least, you should have the #define named such that it is apparent it changes something.

One of my coworkers at Sony really likes making complicated #defines. They make code REALLY hard to read until he explains what each one does.

Code:
((solenoid1 = 1) && (Solenoid2 = 0) && (solenoid3 = 1) && (solenoid4 = 0) && (solenoid5 = 0) && (solenoid6 = 0));
Although this actually may* do what you want it to, it does it in a pretty nonsensical way. As posted above, you want each of those assignment statements to have a semicolon after them, rather than a logical and. That way, each of them is a seperate statement that is guaranteed to execute, rather than a wierd condition statement that may stop halfway through (See below).
What follows below is completely tangential and is only for added knowledge
*Depending on the compiler, it may short-circuit the condition statement if one of the arguments in an && block is false. Since ALL conditions must be true for your condition to work out (because they are all anded together), then as soon as it encounters one that returns zero (Solenoid2 = 0) it may exit the condition and return zero.

Basically, when your program is run, it will do this:
Assigns solenoid1 = 1 (result: 1)
Assigns solenoid2 = 0 (result: 0)
**ANDS the first two expressions together (result: 0 && 1 is 0)
Assigns solenoid3 = 1 (result: 1)
ANDS the third expression with result from first 2 (result: 0 && 1 is 0)
Assigns solenoid4 = 0 (result: 0)
ANDS the fourth expression with result from first 3 (result: 0 && 0 is 0)
Assigns solenoid5 = 0 (result: 0)
ANDS the fifth expression with result from first 4 (result: 0 && 0 is 0)
Assigns solenoid6 = 0 (result: 0)
ANDS the sixth expression with result from first 5 (result: 0 && 0 is 0)

At **, the program KNOWS that the final result is going to be zero because one condition in a series of logical ands resulted in zero, so depending on the compiler, it might just save time and skip doing the rest.

Last edited by Bongle : 06-01-2007 at 09:05.