Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pins as variables?

Status
Not open for further replies.
dead horse and if you really are paranoid you can as I said run lint or have a parser look for missing breaks in the make file.

If you forget most anything in a program you are in trouble. This is far more readable then if else. To each his own.

You can get a whole lot more mileage out of doing comparisons with the constant first then not using switch/case. (mountain out of a mole hill)

Funny how you love pointers that trip up so many people but do not want to deal with switch case. Again to each his own on this one.
 
Last edited:
Funny how you love pointers that trip up so many people but do not want to deal with switch case. Again to each his own on this one.
Well, I would do the state machine using function pointers instead of switch-case or if-else thingies :)
 
  • Like
Reactions: 3v0
:eek: The 'Case' statement is pure C magic if used with a little imagination.

https://groups.google.com/forum/#!msg/net.lang.c/3KFq-67DzdQ/TKl64DiBAGYJ

Isn't it an example of spaghetti code condemned by general programming public?

I don't think the case statement is a good candidate when you need something very fast. If you want fast, you just create an array of addresses (labels) and then select the "goto" address from the array based on the "switch" variable. I guess that's spaghetti too :)
 
I don't think the case statement is a good candidate when you need something very fast.
Usually fast/efficient and good style do not go together.
 
  • Like
Reactions: 3v0
Isn't it an example of spaghetti code condemned by general programming public?

Yes, but it shows something that 'if else' can't do efficiently because the switch statement 'fallthrough' can emulate (computed) goto into the middle of a loop without saying the 'dirty' word within the switch input/output node. I would (almost) never use tricks like that with a good gnu type compiler but the 8 bit uC versions are a different animal.
The guys who created C liked spaghetti neat.

http://doc.cat-v.org/bell_labs/duffs_device
 
Last edited:
I don't like switch/case as it messes with my (OCD) need for consistent indenting. :p

Mike.
 
i have found switch to be very handy for event loop handlers, sub menus and multi-function buttons,
I have also found not using break is very helpful aswell, when used properly..

x=3;
switch (x){

case 1: y=2;break;
case 2: y=4;break;
case 3: y=6;
case 4: y=y++;break;
case 5: y=100; break;

}

system will guarantee to output y as 7!!
 
i have found switch to be very handy for event loop handlers, sub menus and multi-function buttons,
I have also found not using break is very helpful aswell, when used properly..

x=3;
switch (x){
case 1: y=2;break;
case 2: y=4;break;
case 3: y=6;
case 4: y=y++;break;
case 5: y=100; break;
}
system will guarantee to output y as 7!!

I take good style and safety quite seriously, so I would suggest that when you leave out the break; at least comment it very well:
C:
x=3;
switch (x){
    case 1: y=2;break;
    case 2: y=4;break;
    case 3: y=6; /* NO BREAK */
    case 4: y=y++;break;
    case 5: y=100; break;
}

I believe C is only language that requires the breaks in its switch-cases. If some Pascal programmer would edit your code, he might not understand what is going on and that is a dangerous trap.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top