G
Greg Schmidt
I'm wondering what the "best" is way to handle blocks of code that are
shared between two or more cases in a switch statement. Consider the
following code (assuming valid variable and function definitions,
obviously):
switch (a)
{
case 1:
if (x) return false;
y = 6;
foo();
break;
case 2:
if (x) return false;
y = 6;
bar();
break;
case 3:
foo();
break;
}
Of course, this isn't too bad, but when the first two lines turn into a
dozen, or as there are more cases that share common code, the
possibility of a change to one not being reflected to all others
increases. Generally, the solution to common code is to pull it out,
but the ways that I see of doing that here are:
1) Sequential switches
switch (a)
{
case 1:
case 2:
// shared code here
}
switch (a)
{
// unique code here
}
This might also be written like
if (a == 1 || a == 2)
{
// shared code here
}
depending on whether there were multiple different blocks of shared
code.
This method looks potentially confusing, and perhaps causes maintenance
problems.
2) Put the common code in a function that is called from each applicable
case. This would work well in some cases, but not, I think, if there
are multiple possible early return values that might be generated,
multiple variables that may be tested or affected (which now all need to
be parameters to the function), etc.
Exceptions could be a way to solve the "multiple return value" part of
the problem with this solution.
So, are there other methods I've missed that people use in this
situation? Is this perhaps an indication that I'm trying to use one
variable to control two orthogonal values (time to refactor)?
shared between two or more cases in a switch statement. Consider the
following code (assuming valid variable and function definitions,
obviously):
switch (a)
{
case 1:
if (x) return false;
y = 6;
foo();
break;
case 2:
if (x) return false;
y = 6;
bar();
break;
case 3:
foo();
break;
}
Of course, this isn't too bad, but when the first two lines turn into a
dozen, or as there are more cases that share common code, the
possibility of a change to one not being reflected to all others
increases. Generally, the solution to common code is to pull it out,
but the ways that I see of doing that here are:
1) Sequential switches
switch (a)
{
case 1:
case 2:
// shared code here
}
switch (a)
{
// unique code here
}
This might also be written like
if (a == 1 || a == 2)
{
// shared code here
}
depending on whether there were multiple different blocks of shared
code.
This method looks potentially confusing, and perhaps causes maintenance
problems.
2) Put the common code in a function that is called from each applicable
case. This would work well in some cases, but not, I think, if there
are multiple possible early return values that might be generated,
multiple variables that may be tested or affected (which now all need to
be parameters to the function), etc.
Exceptions could be a way to solve the "multiple return value" part of
the problem with this solution.
So, are there other methods I've missed that people use in this
situation? Is this perhaps an indication that I'm trying to use one
variable to control two orthogonal values (time to refactor)?