Click here to Skip to main content
15,923,689 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Do it only once please Pin
Xagyg29-Nov-07 4:03
Xagyg29-Nov-07 4:03 
Generalswitch statement Pin
Tony Wesley16-Nov-07 6:33
Tony Wesley16-Nov-07 6:33 
GeneralRe: switch statement Pin
Oshtri Deka16-Nov-07 11:33
professionalOshtri Deka16-Nov-07 11:33 
GeneralRe: switch statement Pin
Tony Wesley16-Nov-07 11:38
Tony Wesley16-Nov-07 11:38 
GeneralRe: switch statement Pin
Oshtri Deka16-Nov-07 15:02
professionalOshtri Deka16-Nov-07 15:02 
AnswerRe: switch statement Pin
Tony Wesley17-Nov-07 4:26
Tony Wesley17-Nov-07 4:26 
GeneralRe: switch statement Pin
xenonysf21-Nov-07 2:05
xenonysf21-Nov-07 2:05 
GeneralRe: switch statement Pin
Tony Wesley21-Nov-07 6:50
Tony Wesley21-Nov-07 6:50 
xenonysf wrote:
You should construct a data structure and keep the possible values in case statements in an ordered way in your data structure; then your incoming value should be compared with the values inside your data structure in binary search fashion...

this will be faster and save you from time consuming sequential searchin


I agree with what you say but not for the reasons you give.

A switch/case statement has better performance than a sequence of if/else statements. The compiler may generate a jump table. For large tables, some use binary searches. See http://www.eventhelix.com/RealtimeMantra/Basics/CToAssemblyTranslation3.htm[^].

So performance isn't the driving factor here.

But I do agree that the cases -- where possible -- should be encapsulated in a data structure. We would like to have actions for the various cases in a data structure to simply the logic.

Complexity leads to increased probability of error. We have unneccessary complexity. However, it takes a while to untangle code that has grown for decades.
GeneralRe: switch statement Pin
DavidNohejl16-Nov-07 13:39
DavidNohejl16-Nov-07 13:39 
GeneralRe: switch statement Pin
bsaksida17-Nov-07 4:12
bsaksida17-Nov-07 4:12 
GeneralRe: switch statement Pin
Tony Wesley17-Nov-07 4:31
Tony Wesley17-Nov-07 4:31 
GeneralRe: switch statement Pin
bsaksida17-Nov-07 4:16
bsaksida17-Nov-07 4:16 
GeneralRe: switch statement Pin
Tony Wesley17-Nov-07 4:45
Tony Wesley17-Nov-07 4:45 
Generaltry-catch Oo [modified] Pin
Oshtri Deka16-Nov-07 0:19
professionalOshtri Deka16-Nov-07 0:19 
JokeRe: try-catch Oo Pin
Nemanja Trifunovic16-Nov-07 1:35
Nemanja Trifunovic16-Nov-07 1:35 
GeneralRe: try-catch Oo Pin
Nathan Holt at EMOM16-Nov-07 7:43
Nathan Holt at EMOM16-Nov-07 7:43 
GeneralRe: try-catch Oo Pin
Oshtri Deka16-Nov-07 11:14
professionalOshtri Deka16-Nov-07 11:14 
GeneralRe: try-catch Oo Pin
Ware@Work30-Nov-07 3:55
Ware@Work30-Nov-07 3:55 
GeneralRe: try-catch Oo Pin
Robert Royall16-Nov-07 9:45
Robert Royall16-Nov-07 9:45 
QuestionRe: try-catch Oo Pin
Chris Meech16-Nov-07 10:23
Chris Meech16-Nov-07 10:23 
AnswerRe: try-catch Oo Pin
Robert Royall16-Nov-07 12:38
Robert Royall16-Nov-07 12:38 
GeneralRe: try-catch Oo Pin
Oshtri Deka16-Nov-07 11:27
professionalOshtri Deka16-Nov-07 11:27 
GeneralRe: try-catch Oo Pin
Brady Kelly16-Nov-07 22:05
Brady Kelly16-Nov-07 22:05 
GeneralRe: try-catch Oo Pin
Chris Losinger17-Nov-07 12:40
professionalChris Losinger17-Nov-07 12:40 
GeneralRe: try-catch Oo Pin
StM0n21-Nov-07 0:26
StM0n21-Nov-07 0:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.