I'm currently using Qt's QTextStream to read many different types (read: different extension) of text files. Each "FileReader" class I create starts to have a similar pattern where it needs to readLine() like this:
// Get the line's first word as float where each word is delimited by a comma
fileData.readLine().split(",")[0].toFloat();
You can imagine I have tens of these lines in my program.
Further, it's possible that toFloat() could fail (e.g. the value read is not convertible to float), so I'm planning to modify the above line like this:
// Get the line's first word as float where each word is delimited by a comma
bool convertible;
fileData.readLine().split(",")[0].toFloat(&convertible);
if(!convertible) throw std::runtime_error("Error!");
Obviously, IMO, the least maintainable code would be to simply repeat the above code to every line that I use readLine(). This is definitely not the path I plan to choose. (I would welcome someone who could prove otherwise the benefits of doing so)
I could think of a few ways to refactor this code.
1) Instead of directly using Qt's QTextStream class, create my own class that owns QTextStream, and then create a method called readFirstTokenAsFloat(). Inside that method I would have error checking as shown above. Then every "FileReader" class would now switch to use this new class. The pros of this approach, IMO, is it accomplishes what I want to do, but the cons, IMO, is that if I were to need to do other things, or if I wanted to use other QTextStream's methods, I would violate the DRY principle (?) by duplicating the same methods, and internally just have a one-liner calling QTextStream.
2) OR I could just inherit from QTextStream. This way I would simply extend its functionality, and would also get all of QTextStream's functionality. But is inheritance a good idea in this case?
3) Any other thoughts? I'm sure someone has come across something like this. Is there a specific name for this pattern?