I’m having some questions regarding improvement of code structure and maintainability, especially regarding SOLID. These questions are specifically for this project, so I would be happy if this doesn’t end up as a duplicate topic post (I am aware there are a lot of posts regarding this topic).
The main question is, if it is useful or even necessary to use SOLID to properly refactor the code.
A short introduction: I’m working as a coding monkey on a C# .NET project for a windows service. This service has to read text files, which contain data logging information, identify the reading (e.g. voltage) perform some calculations on it and output the data into a text file. This is a single man job I am doing for my employer. It is currently around 3000 LOC As you are well aware project like this start small with a nice small set of requirements. Then suddenly new stuff needs to be implemented within the shortest time possible. As a result the structure of the program is not optimal. I have to admit that I am not very experienced as well (currently studying CS at college).
It started with four classes, one the windows service implementation itself with basic function on the data, and three for the big data structure containing all configuration and identification values.
Now there are two problems, the first is we started with just one type of data logger, now there are multiple and the function which performs the operations on read data needs to be split in parts.
Second and main concern, I still just have four classes. Everything is still in the ConverterService class.
I am aware that according to SOLID and OOD things need be separated in different classes, if they are for different concerns. What I can’t really wrap my head around it, if it really needs to be done here.
For instance I have a function for reading XML and function for writing XML. This could be put in a separate class, but I would just have one instance of it anyway.
Now regarding the data calculation function, this if how the structure currently looks like:
private static void editFile(input the file) check the header validate that values are really from configured data logger check timestamps special cases where values registers have certain names do… if conditions q if data logger x and also a do lots of operations else if data logger y and also a do else if data logger z and b do if condition w if data logger x and also a do else if data logger y and also a do some other operations else if data logger z and b do …
As you can imagine pretty unpleasant to look at.
Now what I would definitely do is separate the tasks you can see in different functions (like checking the header etc.)
But when it comes to separate the condition operation mix things get a little complicated.
I see two ways doing it. First the non OOD/SOLID approach:
Keep it in the ConverterService class but create separate functions for the operations.
The function editFile will still contains the condition checks and calls these functions.
Second approach, create a class for each data logger with their own operations. It would also be possible to create an interface that each class of data loggers implement to have it more unified. Problem is though, that the operations are very different so probably not the best idea. The function editFile will still contains the condition checks and calls these functions. I don’t see a big improvement because the abstraction doesn’t really simplify anything.
Summarized: 3000 LOC, 37 Functions and one developer
- What should I do?
- Should I use SOLID?
- Do I need to strongly separate my functions with classes?
- Have I completely misunderstood OOD? (Have mercy)
- Is using a new class the right way, even though I’ll just use it to call a function (and have on instance)?
- How would you refactor this?
- How to deal with large parameter lists that you need when separating it in several functions?
I really hope this large chunk of questions is not out of place.