-2

In my company's code base there are examples of methods which receive some protobuf messages and use them in this fashion:

void doSomething(const A& a) {
  // Only data from a.info() is used in this function. Roughly 4,5 times.
}

Where A is a message type with several (>20) fields and Info also is a message type with several fields.

Shouldn't this syntax be more correct?

void doSomething(const Info& info) {
  // Use info in this function
}

I have two concerns:

  1. From the code practice point of view: I see that the first syntax allows doSomething to be expanded in the future and eventually access some extra information about the message type A. Shouldn't in that case be more correct to create a different function whenever it will be needed?
  2. From the performance point of view: I would expect the performance of the two functions to be comparable, as I am always accessing the info field. The overhead would be for the compiler to load in memory the line containing a.info(). Is this correct?
2
  • Do you require access to any of the fields in A? Commented Feb 10, 2021 at 18:42
  • 2
    As to performance considerations, the difference in performance between these two functions is almost certainly dwarfed by other concerns like network propagation time. Commented Feb 10, 2021 at 18:43

2 Answers 2

1

How would you describe the function? “It takes an A object and extracts all the bits that it needs”, or “it takes an info object and extracts all the bits it needs”? What does the caller expect? That’s the code that you write.

0

I would only pass the info - minimal Interface.

If someone wants more data one day, it’s his job to extend the interface (or make another method). Note that passing only info also allows the method to be used with info from other sources - which might or might not make sense, depending on your application.

Main argument for minimal is that otherwise you could argue to pass everything and anything, “in case it’s someday needed“ - certainly not a good plan.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.