trying to avoid using if statements - is this good idea ?

Started by
10 comments, last by L. Spiro 9 years, 4 months ago
Reading the original question, I think it shouldn't be a goal itself to get rid of if statements. I would say the goal is that your program delivers x, with y performance (and maybe keeping your code readable). If removing the if statement(s) visibly contributes to this goal, go for it. If it doesn't, it sounds like a waste of time (and possibly premature optimization).

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement

You are doing the (mostly) correct thing for the wrong reason.

#1: Multiple tested and failed if’s, paired with no fewer than 1 jump, will virtually never be faster than most table look-ups. Then again, there is the time it takes to check if the table has been made and to make it if not (not applicable to all platforms, including Windows®).
#2: Tables are easier to read and manage than multiple if’s. Tables are mainly useful because they offer a convenient way to convert any 0-based number into another number, and they are particularly easy to maintain. It is much easier to add to tables etc. and to update the conversion routine accordingly.

#1, however, is virtually a moot point, as any difference between them will be negligible. That isn’t why you use tables. #2 is why you use tables.

But your implementation leaves 2 main things to be desired.

#1: vertexToDrawModes should be static const, not just const. Your code will likely be much slower since it has to build the table every time the method/function is called.

#2: Never hard-code numbers. “3-1” should be some kind of named enumerated value.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement