• Create Account

#Actualslicer4ever

Posted 25 December 2012 - 01:23 PM

so, i was thinking of switch's the other night, and one of the annoyances i find with switchs is that in many cases, i simply write out code that boils down to being an if/else if/else statement's because all the switch wants to do is break out after each case, and I only potentially want to use w/e the compiler optimizations switchs with. so i was thinking of a potential design solution for this, that doesn't require adding a break after every statement, and came up with this:

int Foo(int value){
int r=0;
switch(Value; break){ //think of w/e is after the ; as a goto at the bottom of each case, so with this one statement, i append a break to every single case, this could be used for anything, but break is just a common example.
case 0: r=1;
case 1(): r=2;//appending a () before the : and after the value overrides what the switch statement's conditional does, with what is specefied in the (), in this case, nothing is specefied, so we have default switch behavior here.
case 2: r=r+2;
default: r=100;
}
return r; //I know i can just return in the switch to do what i want, but this is suppose to be an example.
}

int main(int argc, char **argv){
Foo(0) //R = 1
Foo(1) //R = 4
Foo(2) //R = 2
Foo(3) //R = 100
return 0;
}

any thoughts on this, is their a more elegant design that i'm missing, or are my thoughts on this just crazy/stupid?

and yes, their is plenty of room for discussion on why one would design code that essentially boils down to an if/else if/else code block, and i suspect that this discussion might fall into that, but we'll see what people's thoughts on this is.

#4slicer4ever

Posted 25 December 2012 - 01:18 PM

so, i was thinking of switch's the other night, and one of the annoyances i find with switchs is that in many cases, i simply write out code that boils down to being an if/else if/else statement's because all the switch wants to do is break out after each case, and I only potentially want to use w/e the compiler optimizations switchs with. so i was thinking of a potential design solution for this, that doesn't require adding a break after every statement, and came up with this:

int Foo(int value){
int r=0;
switch(Value; break){ //think of w/e is after the ; as a goto at the bottom of each case, so with this one statement, i append a break to every single case, this could be used for anything, but break is just a common example.
case 0: r=1;
case 1(): r=2;//appending a () before the : and after the value overrides what the switch statement's conditional does, allowing you to maintain fallthrough, with little overhead.
case 2: r=r+2;
default: r=100;
}
return r; //I know i can just return in the switch to do what i want, but this is suppose to be an example.
}

int main(int argc, char **argv){
Foo(0) //R = 1
Foo(1) //R = 4
Foo(2) //R = 2
Foo(3) //R = 100
return 0;
}

any thoughts on this, is their a more elegant design that i'm missing, or are my thoughts on this just crazy/stupid?

and yes, their is plenty of room for discussion on why one would design code that essentially boils down to an if/else if/else code block, and i suspect that this discussion might fall into that, but we'll see what people's thoughts on this is.

#3slicer4ever

Posted 25 December 2012 - 01:18 PM

so, i was thinking of switch's the other night, and one of the annoyances i find with switchs is that in many cases, i simply write out code that boils down to being an if/else if/else statement's because all the switch wants to do is break out after each case, and I only potentially want to use w/e the compiler optimizations switchs with. so i was thinking of a potential design solution for this, that doesn't require adding a break after every statement, and came up with this:

int Foo(int value){
int r=0; //I know i can just return in the switch to do what i want, but this is suppose to be an example.
switch(Value; break){ //think of w/e is after the ; as a goto at the bottom of each case, so with this one statement, i append a break to every single case, this could be used for anything, but break is just a common example.
case 0: r=1;
case 1(): r=2;//appending a () before the : and after the value overrides what the switch statement's conditional does, allowing you to maintain fallthrough, with little overhead.
case 2: r=r+2;
default: r=100;
}
return r;
}

int main(int argc, char **argv){
Foo(0) //R = 1
Foo(1) //R = 4
Foo(2) //R = 2
Foo(3) //R = 100
return 0;
}

any thoughts on this, is their a more elegant design that i'm missing, or are my thoughts on this just crazy/stupid?

and yes, their is plenty of room for discussion on why one would design code that essentially boils down to an if/else if/else code block, and i suspect that this discussion might fall into that, but we'll see what people's thoughts on this is.

#2slicer4ever

Posted 25 December 2012 - 01:16 PM

so, i was thinking of switch's the other night, and one of the annoyances i find with switchs is that in many cases, i simply use an if/else if/else statement's when all the switch wants to do is break out after each case, and only potentially use w/e the compiler optimizations switchs with. so i was thinking of a potential design solution for this, and came up with this:

int Foo(int value){
int r=0; //I know i can just return in the switch to do what i want, but this is suppose to be an example.
switch(Value; break){ //think of w/e is after the ; as a goto at the bottom of each case, so with this one statement, i append a break to every single case, this could be used for anything, but break is just a common example.
case 0: r=1;
case 1(): r=2;//appending a () before the : and after the value overrides what the switch statement's conditional does, allowing you to maintain fallthrough, with little overhead.
case 2: r=r+2;
default: r=100;
}
return r;
}

int main(int argc, char **argv){
Foo(0) //R = 1
Foo(1) //R = 4
Foo(2) //R = 2
Foo(3) //R = 100
return 0;
}

any thoughts on this, is their a more elegant design that i'm missing, or are my thoughts on this just crazy/stupid?

and yes, their is plenty of room for discussion on why one would design code that essentially boils down to an if/else if/else code block, and i suspect that this discussion might fall into that, but we'll see what people's thoughts on this is.

#1slicer4ever

Posted 25 December 2012 - 01:16 PM

so, i was thinking of switch's the other night, and one of the annoyances i find with switchs is that in many cases, i simply use an if/else if/else statement's when all the switch wants to do is break out after each case, and only potentially use w/e the compiler optimizations switchs with. so i was thinking of a potential design solution for this, and came up with this:

int Foo(int value){
int r=0; //I know i can just return in the switch to do what i want, but this is suppose to be an example.
switch(Value; break){ //think of w/e is after the ; as a goto at the bottom of each case, so with this one statement, i append a break to every single case, this could be used for anything, but break is just a common example.
case 0: r=1;
case 1(): r=2;//appending a () before the : and after the value overrides what the switch statement's conditional does, allowing you to maintain fallthrough, with little overhead.
case 2: r=r+2;
default: r=100;
}
return r;
}

int main(int argc, char **argv){
Foo(0) //R = 1
Foo(1) //R = 4
Foo(2) //R = 2
Foo(3) //R = 100
return 0;
}

any thoughts on this, is their a more elegant design that i'm missing, or are my thoughts on this just crazy/stupid?

yes, their is plenty of room for discussion on why one would design code that essentially boils down to an if/else if/else code block, and i suspect that this discussion might fall into that, but we'll see what people's thoughts on this is.

PARTNERS