Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Should I prefer functions that transform data instead of creating data?

I noticed in many functions from OpenCV library (C++ API) that there's a pattern regarding to the parameters passed to a function. I see that almost always there's a &src Mat and a &dst Mat that is the result (the data modified) being the return type of that function a void type. When I came from java to start in C++ I was writing all of my functions with a return type instead of the &dst Mat idea. Something like this:

cv::Mat doStuff(cv::Mat &src) {

  cv::Mat modifiedStuff;
  ... // do stuff
  return modifiedStuff;

I think that way sometimes make things more clear, I understand that I'm generating an extra matrix inside the method everytime, and it can be bad. However, I'm curious how much bad it could be. Sometimes for example, I'm using the return concept instead of the &dst concept coz I need that the matrix have a specific proportion for example, so I have to create it inside the method to be sure that method will always work with that proportion.

cv::Mat doStuffWith1x132Matrix(cv::Mat &src) {

  cv::Mat modifiedStuff(1,132,CV_64F); 
  ... // do stuff 
  return modifiedStuff;

There's some consense in the community regarding to where should I use the &dst matrix idea instead of returning a new matrix (created inside the method) or should I take it as a question of preferences?