I'm working on outputting a dynamically generated table that would be capable of printing values with various degrees of precision as defined by the user at runtime. The tables will be generated in any of 4 different forms, as specified by the user at runtime. Tables 1 & 2 are almost the same except the variable used as its base. The output format is virtually identical. Tables 3 & 4 will have a slightly different format, with grouping of the output data in a slightly different order. (For those familiar with them, I'm creating dynamically generated Steam Tables.)
The problem that I'm running into is that I've got a great code base that works excellently for formatting and outputting Tables 1 & 2 in plain text, either to the console or to a text file, but not so well for Tables 3 & 4. This format has great column headers, perfectly evenly spaced columns, and column dividers for each of the groups of data. Since the format for Tables 1 & 2 are nearly identical, just the first two columns swapped, it made sense to make functions to calculate Tables 1 & 2, then use a separate common function to perform the actual output.
Where this generates problems is that in Tables 3 & 4, the variable containing the width of column 1 is specialized instead of just looking at the length of the strings in that column. Additionally, the column dividers that are wonderful for Tables 1 & 2 have no meaning in Tables 3 & 4.
Here's my question: which is better coding practice, to repeat the same code several places so that the implementation is apparent and individual chunks of code are cleaner, or use multiple, highly specialized, and repeated condition checks to prevent repeating the bulk code?
By highly specialized I mean:
1 2 3 4 5 6 7 8 9 10 11 12
|
if (nTable == 3 && stringElement == 0)
{
// Do formatting needed only for Table 3
}
if (nTable == 4 && stringElement == 0)
{
// Do formatting needed only for Table 4
}
else
{
// Do other elegant formatting
}
|
And use this code structure in 3+ places through the function.
Thoughts?