Programmers Debate: _underscore private class properties?
April 2nd, 2008 : Nik RowellI tend to be a very thorough and detail-oriented person… at least I hope so (there are other words for it, but we’ll leave it at that). So naturally, when I’m writing code - whether it’s xhtml, CSS, ActionScript etc - I try and keep things neat, clean and easy to read. I indent like it’s going out of style, I strive for consistent naming conventions, and I use descriptive variable and function names.
I subscribe to the belief that if a few extra characters makes things more clear for me or the next person that will see the code, I’ll do just that. We’re all fast typers here, no need to shorten variable or function names to the point where their role is unclear.
For example, I’ve been considering writting a simple slideshow class (or set of classes) in actionscript 3 and, chances are, I’ll use names like SimpleSlidShow.transitionTime
and SimpleSlideShowEvent.TRANSITION_COMPLETE.
As I’m beginning to write more of my own classes, I’m starting to faced with a decision on naming my private member variables: Should private properties be preceded with an “_underscore“? - (or all class variables if you practice encapsulation, and use get and set methods instead. (Remember that word for detail-oriented, perfectionism etc…
I know this really comes down to a personal preference but, as a hybrid-in-training, I’d like to know what other programmers think…












April 4th, 2008 at 9:38 pm
Personally, I’m not for it. While it does add something that will immediately set the variable apart, it’s not really doing anything else for you except (like you said) making a bit more work of typing your variable names.
I’m a proponent of making pretty much everything private and manipulating your instance variables via methods — not necessarily get/set, though those are useful, but through higher-level methods. A class tends to represent a concept, and its methods represent relevant actions, not just operations on its variables.
Public member variables are actually a bit of an oddity, though a common one: Your instance retains no control over a public variable.