Coding convention: Repeat code, or highly specilized conditions

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?
ideally you would write something reusable that can work on any of the tables.
even if it takes a specialized parameter, eg a vector of column names, formatting codes, and widths that is hard coded to your current work. Is something like that possible?

this will look like your specialized code, but instead of "if table is 3 do this" you have "if parameter says format this way, do this else...)

that said, sometimes you need to get stuff done. Copy the code and adjust it for each table if you are in a hurry for the result. It will be easier to get that working and debugged.
Last edited on
Another possibility is to create a table class structure. Have a base Table class that does all of the common work for displaying the tables and calls virtual functions at the pinch points. Each derived class (Table1, Table2, etc., although better names would be appreciated) inherit from the base Table class and do the specific work related to the table at each pinch point.

This way, the class hierarchy takes care of the table differences, and there is no if/else or switch needed for formatting.

p.s. I don't know your system, so I don't know if this is overkill for what you are trying to do or not. To that end, this is only a suggestion, not necessarily a recommendation.
Topic archived. No new replies allowed.