Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The most common argument:

Pass by reference hides the implication of a side effect on the parameter. If you expect the function to manipulate the parameter, passing by pointer is clearer.

Pass by reference was also only added to efficiently support copy constructors and more importantly operator overloading, both of which are syntactic sugar that a C with Classes style would normally avoid.

Template programming also requires pass by reference but templates are sugar on sugar so not the core reason why pass by reference is needed.

Ps. Disclaimer: I haven't checked if the Doom3 code uses these features.



Fair points, although all references should ideally be const unless there are side effects. It's not perfect though, since you can't tell from just viewing the calling code, you have to check the function prototype.

References also help avoid problems with pointer aliasing, and make it clear that the called function definitely cannot delete the passed objects. So it's more than just syntactic sugar.

(n.b. I'm not arguing that pointers are bad and should never be used, I still just don't see a clear reason never to use references. The id Software coders are smart, they must have had good reason to pick their choice)


Also, references can't be nullptr, which eliminates needs for null checks and makes clear that nullptr isn't a valid value. RVO also makes "returning" values through non-const references not as necessary.

Though, the most important thing is consistency and readability over using fancy new features, so they should stick to the style they were using for the project.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: