7

C# peeps... Which one?

private int number;

||

private int _number;

Comments
  • 2
    Private int number;
  • 2
    Definitely the first
  • 1
    private int mNumber;
  • 0
  • 2
    First one.
  • 0
  • 1
  • 0
    @GinjaNinja tacking on an m or s onto a variable is a common Android convention, used to denote member and static variables. Even though this is a C# related preference, such a convention prevents accidental shadowing of your variables, which can often happen if you're not careful. Plus it's easier to identify where what a variable's scope is just by looking at it. My $.02 anyway...
  • 1
    @codeclod it's generally not done in most languages and is seen as bad practice. It harks back to Hungarian notation, just like adding cbo to a combobox or str to a string, it's just means that you variables are poorly named for there purpose. Adding a single m to the start to denote module level vars seems kinda antiquated to me, your class should be single purpose so module scope vars should also have relevant names !

    Just my 2 cents
  • 1
    @bluescreen I guess I'm jaded by the fact I can never tell what someone else means with their names and design half the time (due to a number of reasons), such that it seems easier to use a convention that can actually be automatically audited (such as with checkstyle), rather that rely on everyone on your project to not do stupid things. I guess this why we have devRant 😉
  • 0
    @codeclod it take team discipline. Once everyone agrees it's quite easy to police and having a style check process like stylecop or similar really helps. I personally think it makes your code that little bit more readable.
  • 1
    @bluescreen yep. I am definitely a proponent of your class design inherently describing only the intended purpose, nothing more, nothing less. That's a harder thing to coach, and while you do so, such conventions (which I've gotten used to) helps keep the poor design in check. I can understand the ugliness, and propensity to deem it redundant. Although, I'm more willing to entertain the label of the specific UI element rather than the raw data type in the name, simply because it ties me to the user interaction point, rather than the underlying data type (cbox or comboBox, as opposed to str for a String). I'm just inconsistent in my preference 😧
  • 1
    @codeclod design patterns like mvvm help as your model and view model describe the data and the controls simply bind to there iterators and command sources allowing you to separate ui and interaction logic
  • 1
    @bluescreen yep. We use a lot of JavaFX now, which does a good job of building in those abstractions.
  • 0
    Guess you guys go crazy now... private int m_iNumber;
  • 0
    I always use _variable. Apparently that's also the official coding style of the .NET Core team as well.
Add Comment