Search code examples
iphoneobjective-cios4uitableview

Is it best to use if statements or switch statements in cellForRowAtIndexPath? - UITableView


When setting up cells in UITableView, is it best to use if statements or switch statements as a general practice?

I'm talking specifically about cellForRowAtIndexPath here and other UITableView methods, not as a general programming question.


Solution

  • Because I am so old that I worry about performance, I would probably do it this way ............ but wait: modern compilers, quite simply, "take over" and optimize/compile how they want.

    So in my opinion, it truly and profoundly makes no difference, for that reason:

    You have no clue what modern compilers are doing, so it's pointless worrying about writing it one way or the other way.

    I tend to prefer "if" rather than complicated modern switches, purely as a stylistic matter and to wave the flag for old-fashionedness.

    You could possibly say: try to "return" from the routine as early as possible on each path so it doesn't pointlessly traverse stuff at the end. I lose sleep over things like this, it's a great question.

    One point .. in iOS programming there is tremendous value in doing things the same way others do them. I hate that fact, but, it's true! So in the specific question at hand, if you can gleam an Apple idiom -- do it that way, for sure.