C++ Coding style observation / relaxation


#1

We use the google c++ style for C++ portions of TVM.

Any reasons why folks prefer a pass-by-pointer semantics for in-out parameters rather than a pass-by-reference model ? There are the obvious benefits that a reference can never be null and one doesn’t have to worry about null pointer checks in code if you want pass-by-reference.

Has this been discussed elsewhere or am I missing something simple ?

regards
Ramana


#2

Thanks for bringing this up, I would definitely be in favor of the pass by reference. @tqchen thoughts on this?


#3

The short answer is that Google c++ style is a widely adopted style so we just use it so our contributor don’t have to learn yet another project specific code-style.

In terms of my understanding about why pass-by-pointer is preferred, usually it makes the code more explicit. When we call the function it can be MyFunction(input_arg0, &output); If it is pass by reference , it is harder to tell from the code itself it will simply be MyFunction(input_arg0, output);. So personally I actually find the pass-by-pointer style better due to the explicit-ness.

Most developers gets used to the interface such that they won’t pass nullptr into such functions.


#4

I see your point about readability at the call site in those cases and it’s an interesting tradeoff. My personal opinion is that projects can evolve their coding style over time despite the starting point.

regards
Ramana


#5

I agree we could if the community believes that there is a strong reason for a certain case. Up until now we have found that Google C style was a quite reasonable choice