PMD suggested in my GWT project that I should change my switch
to multiple if
s if it has fewer than 3 branches. I know that this is compiled to Javascript, but I wondered if it made any difference in GWT. Do you think the generated Javascript is more efficient with if's compared to switch
? Does the current GWT compiler (2.4) do any optimisation on this?
Asked
Active
Viewed 249 times
0

jabal
- 11,987
- 12
- 51
- 99
-
Do you have any performance problems here? If not, don't even think about such performance optimizing. About readability: I think `switch` is easier to read and expand - and less error prone when using `enum`s (plus you get tool support). So I would change the PMD settings, but it is a matter of taste. – Hauke Ingmar Schmidt Mar 09 '12 at 15:43
-
Unless you are a complete freak of performance and the switch is invoked millions of times, I think that you can discard without more investigation. – Guillaume Polet Mar 09 '12 at 15:44
-
I agree with you, however the PMD trend graphs are strict guys. – jabal Mar 09 '12 at 16:05
-
And that is a good thing, in general. But you don't have to agree with every assumptions they do: TooFewBranchesForASwitchStatement "Swith are designed complex branches, and allow branches to share treatement. Using a switch for only a few branches is ill advised, as switches are not as easy to understand as if. In this case, it's most likely is a good idea to use a if statement instead, at least to increase code readability." I, as stated, completely disagree, and would change that setting. – Hauke Ingmar Schmidt Mar 09 '12 at 16:27
1 Answers
2
According to this site: http://oreilly.com/server-administration/excerpts/even-faster-websites/writing-efficient-javascript.html#the_switch_statement found via this answer: https://stackoverflow.com/a/2923007/66416 It does matter:
In JavaScript, if statements are generally faster than switch statements when there are just one or two conditions to be evaluated.
A quick scan of a compiled GWT 2.4 file found no replacements of 'small' switch statements into if statements. So it actually might be a useful suggestion of PMD.

Community
- 1
- 1

Hilbrand Bouwkamp
- 13,509
- 1
- 45
- 52