5 ms·
Not being a C/C++ programmer, I wonder why they postfix their variables with "underscore"? https://chromium-review.googlesource.com/c/chromium/src/+/1510080/13
by Insanity 8y ago
Not being a C/C++ programmer, I wonder why they postfix their variables with "underscore"?
https://chromium-review.googlesource.com/c/chromium/src/+/1510080/13/device/gamepad/gamepad_device_linux.cc https://chromium-review.googlesource.com/c/chromium/src/+/15...
e.g: switch_pro_4, dualshock_ps4_, ...
It does not happen for all variables, what is this convention? :o
- olig15 8y agoI think it's because they're member variables. It makes them clear they're not local to the method.
- Insanity 8y agoAt a quick glance, that does seem to be a difference with the postfixed variables! Have not yet encountered something like that in the languages I've worked with. ^^
- cmrdporcupine 8y agoWait til you discover Hungarian notation?
- Insanity 8y agoThat one I actually did encounter somewhere in high school, when a lecturer used it in Visual Basic, for some reason. xD
- cmrdporcupine 8y agoGoogle C++ style guide. It's for instance variables on classes, to distinguish from local variables. https://google.github.io/styleguide/cppguide.html#Variable_Names https://google.github.io/styleguide/cppguide.html#Variable_N...
- joshvm 8y agoIs there a reason for not using 'this'? I assume the risk is that you might have a scope where both this->foo and foo are valid and it's unclear which is which. You have no ambiguitiy with this->foo_, and foo (and if you forget 'this', foo_ is still clear).
- jcelerier 8y agosix characters to type vs 1 ?
- joshvm 8y agoYou can argue that either way. Seeing 'this' unambiguously implies instance variable. Otherwise you have to spot the underscore and be aware of the conventions in the codebase. A good IDE should highlight class variables vs locals, though. On the other hand code can get really messy with boilerplate like this everywhere, and I personally agree with you. Although I believe Google also promotes human-readable variables over less typing.
- tokyodude 8y agoI actually think implicit "this" is a language wart. I think C++, C#, Java, Swift, etc.. would all be better languages if they required `this`. As it is, nearly every one of them ends up needing a per team naming convention to help make the code readable. int area(int width) { return width * height; } What is "height". Is it a global variable? variable in some outer scope? A member/property of the current object? Absolutely no idea.
- amalcon 8y agoRules like this are usually less about risk, and more about conveying information. If I see a variable ending in "_", I immediately know to look for its definition in the class rather than the local scope.
- nolok 8y agoI assume it's because if a dev forgets a this somewhere as a caller/accessor, you now have two problems; whereas if they forget a _, it doesn't compile.