Re: big function vs many small functions



On Sep 25, 5:30 am, teke...@xxxxxxxxxxx (t.j.) wrote:
Let's say that one has a function with a large switch statement with
many case statements.

One way of writing the function is to just place all of the code for
each case inside of the function containing the switch statement.

Another would be to have an individual function for each case statement.

Which way is better in terms of maintainability and frequency of bugs?

I recall reading something where someone actually studied this situation
and determined there was no meaningful difference between the two.
Anyone else come across the same thing?

It depends on how much logic is going into each case block. In some
cases switch statements are just used to convert e.g. from an
enumeration to a string representation; when each block is doing a
variant of the same thing and the logic is trivial the switch
statement is clearer. OTOH if there's significant code to each case -
certainly if there are any nested blocks of conditional code -
factoring into method calls is clearer. And if the switch is
essentially a dispatch table it should certainly call different
functions for each case and not try to implement cases in-place.

Also note that there may be object-oriented patterns that allow an
entire problem to be solved elegantly with something other than a
switch. Say for example you have a "type" class data member and
several methods which switch on type to select a certain behavior.
This may be better solved by creating an interface or abstract class
with the methods declared and defining the alternative implementations
in subclasses rather than using a switch statement in one concrete
class (assuming a given instance's "type" doesn't change).

Or, if you have a function that is parameterized and may behave
different ways based on some parameter, but that parameter represents
a relatively stable system state or configuration, you might be able
to use a strategy pattern or decorator pattern (creating a helper
object which provides the needed implementation) instead of a switch
statement in the function body.

- Chris

.



Relevant Pages

  • Re: http site text-only read possible?
    ... >> If you switch off image display then the image files aren't fetched ... The -O switch sends the downloaded file to the file "foo.txt" ... The pattern in foo.awk matches all lines from a line containing the ... Finally a Fortran program reads and does something with the data ...
    (comp.lang.fortran)
  • Re: Why dont variables work in switch patterns?
    ... | substitutions on the pieces. ... switch -- $value { ... does NOT substitute in either the pattern or bodies, ... As soon as it finds a pattern that matches string it evaluates the following body argument by passing it recursively to the Tcl interpreter and returns the result of that evaluation. ...
    (comp.lang.tcl)
  • Re: Comparing memory with a constant
    ... switch (array) { ... case bitpattern1: ... ... matched pattern #0 ... ... for computing a number to switch on, ...
    (comp.lang.c)
  • Re: Advice on landing better
    ... and did two passes in the pattern. ... It is as if the hand just reaches out on its own and hits the switch. ... All you probably really need is practice, perhaps with some supervision so that your instructor can tell you exactly what you need to improve and so you don't practice bad habits. ... World Famous Flight Instructor ...
    (rec.aviation.student)
  • Re: Your juggling weaknesses.
    ... Mark Nicoll wrote: ... barrier that causes me to just switch off around 250 catches in any ... pattern with any number of balls. ... I start a stopwatch, then count five ...
    (rec.juggling)