0

I am currently coding on a project using OpenCV and C++ to detect something from a video stream. The project therefore consists almost solely of a big while-true-loop processing the frame (approx 4000 LoC).

I am looking for ways to structure the contents of this loop, which basically consists of a lot of functions being called one after another.

Until now, i separated functions and variables into files and namespaces based on their logical task, but i miss the ability to hide stuff internally. However, using classes as a wrapper also seems weird if they will only ever be created once and do not hold any data at all.

1 Answers1

1

I have a colleague who likes to create classes and put static methods in them, instead of having functions in a namespace. There area already topics about what is better, e.g. Namespace + functions versus static methods on a class.

However, if you have big loop calling one function after the other with 4k lines total, I think you are making some mistakes. Since you seem to be doing image processing, maybe I can help.

Taking a very high-level view of your task, you have a video stream as input, and some detections as output. Very likely you detect something in a frame.

If your detection has a state, i.e., detections depend on results from previous frames, this is already grounds for a class. Saving this in global (albeit namespaced) variables is simply not right in C++. If it doesn't, then your input is no longer a video stream, but an image, and you should design your interfaces in a way that they take an image. Running your code then for several images from a video stream, especially extracting images from the stream, is something that should be separated from the main logic.

Then you say your functions are being called one after another. Very likely, the order matters? If it does, inputs from some functions depend on output from others. This is also a state which could be saved, and again grounds for writing a class. The question how to structure it exactly depends on your exact task. I recommend you take a piece of paper and a pen, and draw a block for each component (function) of your algorithm, as well as your inputs (images, possibly other data) and outputs. Then draw lines what depends on what. This helps for getting an idea how to structure your code.

Finally, a little challenge: Try to make sure none of your functions is longer than 20 lines (you can do 10, but it gets harder). Trying to achieve this has several good effects. First of all, all your logical units (functions) are short and have defined inputs and outputs. Also, that makes them easy to read (a 5-line-function rarely needs much documentation and often speaks for itself) and easy to test and debug. Then, in 4000 lines of code, you should start thinking about unit tests anyway, and short functions are helpful there, I think.

You shouldn't take the 20-line thing as a rule, there are plenty good reason for having longer functions (although usually not many of them). Trying to keep your functions short and to the point is a good goal, however.

Coming back to image processing, in my experience this strategy works very well there. Also tackling your problem from a high-level view and refining it (divide and conquer) can help you producing well-structured and maintainable code.

Demosthenes
  • 1,515
  • 10
  • 22
  • Thanks for the answer, very good link, too. I follow the view that any class should have data strongly associated with it (and be a true object). But the only way i can think of creating a class here would be this: -feed some input data to the class -call a couple of class functions in a fixed order -retrieve the new/ derived data (as input for the next class) This does not seem like a good design to me (but maybe im wrong?); so i think i will try to go with anonymous namespaces. – Commodore Yournero Aug 31 '19 at 11:10