Code duplication - best practices
I was wondering what the best practices is for duplication of code between nodes.
For example:
I grouped together two packages with a metapackage called perception, namely; object detection and localization.
object detection package has a node -> ballDetection
localization package has a node -> linesDetection
As you can already assume, there is a lot of code duplication in these nodes. For example, if I want to display images in both nodes to the screen, I have the exact same code in both nodes.
Should I create a library that both nodes can use to display images to the screen? Or should I use nodes for everything? What is the best practice for situations like this?
Not an answer, but:
this is a little strange: metapackages don't contain anything, especially not other packages.
If yours does, then it's not a metapackage.
You are right. I edited my question.
do you really have nested packages?
I am confused. Maybe I did mean metapackage:
http://wiki.ros.org/ROS/Concepts
Meta package is meant to group together packages. That's what I did. I edited my post, it is correct now.
Please check REP-140: metapackage: metapackages are not pkgs that contain other pkgs. They only refer to them (in their
package.xml
).See #q256493 for an explanation of the concept and its utility.
For actions such as displaying an image on the screen, then I would put them in a library if you want to share that code. Nodes are generally used to encompass atomic building blocks of a robotic system.
Agree with PeteBlackerThe3rd, you can use libraries to keep the common functionality. Depending on project complexity you can use git submodules for your libraries and assuming each package is a standalone repo you could have a nice reusability and version control for the common library code.