Variables... uppercase... merrrrrr

Started by
9 comments, last by jbadams 12 years, 7 months ago
So I was just running through an XNA tutorial quickly just to get caught up with it again. I don't think I've messed with it since 2.x and it's currently at 4.0, so I just wanted to catch up quickly and refresh my C# jazz.

ANYWAY, I was reading through one of the tutorials and this was one of the code snippets:

[source lang="csharp"]
// Animation representing the player
public Texture2D PlayerTexture;

// Position of the Player relative to the upper left side of the screen
public Vector2 Position;

// State of the player
public bool Active;

// Amount of hit points that player has
public int Health;
[/source]

and this:

[source lang="csharp"]
public void Initialize(Texture2D texture, Vector2 position)
{
PlayerTexture = texture;

// Set the starting position of the player around the middle of the screen and to the back
Position = position;

// Set the player to be active<br class="cdc2"> Active = true;

// Set the player health
Health = 100;
}
[/source]

Does anybody get super bugged by seeing member variables capitalized? D: It feels dirty and wrong. Most of the time all my variables lowercase start camalcase after, and methods are all uppercase start camalcase after. It feels really weird looking at this code for that reason.

/totally pointless mini-rant
Advertisement
That's actually part of the .NET naming guidelines. You get used to it after a while.

That's actually part of the .NET naming guidelines. You get used to it after a while.


D:

edit: clearly I had no idea there was a word for pascal case... I learned 2 new things from your linking :D
I'm more upset about the utter uselessness of the comments. [color=#008800]/* comment about the comments */
true.. I still have an hard time adopting it.. I developed my liking in naming conventions through Java even if I mostly work with C++... I think MS had had capital letters in public methods forever so I thought it was just that heritage.

But then I started to dig deeper andit really make sense in C# because you have properties...it is much nicer imo to have a property named "Whatever" wrapping a protected member "whatever"... Java forces you to have the "getWhatever / setWhatever" thing which isn't nice either.. in C++ you would maybe do "whatever / whatever(value) ) which is better.. but you'll have to have a protected member "m_whatever" "whatever_" which again.. isn't nice.. so afterall, I think .NET guidelines are keepers.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

It's best to follow the convention of the language you are coding in, no matter how horrid it is. The worst thing you couldve ever done is to use one convention in a language that has completely different convention (such as m_strVar in Java)
This is part of working with other people's code.


I prefer the Correct Bracing Style in curly-braces-oriented languages:

if(foo)
{
bar();
baz();
}



However, it is also popular to use the Incorrect Bracing Style:

if (foo) {
bar();
baz();
}


Indeed, I've now worked for several companies whose internal conventions mandate using the Incorrect style. This is just the price you pay for interacting with other programmers. It's worth getting over it sooner rather than later, because you will never work with anyone else who formats code exactly the way you like it.

It's somewhat like food. You can cook your own meals, and get them precisely as you want them; but if you want to eat at a restaurant, or at someone else's home, you'd better be willing to tolerate variety.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]


This is part of working with other people's code.
...


I can't agree with this enough.

Although it isn't the precise point of the thread, I can't stress enough that no matter how the code is formatted, if you're maintaining or adding to existing code, just do whatever the previous programmer did.

I learned this the hard way with my first real programming job. There was only one other developer, who was my manager. He had a lot of code already written to do various things, and his coding format was, for lack of knowledge of a proper term, old C style. That is, in particular using underscores to separate words in variables and using inline braces (or The One True Brace Style, if I'm remember correctly).

I'd learned to use camel case and braces on their own lines, which had suited me just fine for personal projects and programming assignments for school where it didn't matter.

Whenever I had to modify his code, I blindly used my own style instead of his, and it made the code horrid to look at and confusing at times. I'd have to stop and think, was that variable called object_timer or objectTimer? It was a waste of time. I was once even boneheaded enough to write code to translate between the two styles (this is in PHP, where you can use reflection to do this sort of thing) instead of just adopting his style.

He never called me out on it, which surprises me to this day, but within a few weeks I learned to use his style and things fell into place much better. I'm working on a project right now that I started over 2 years ago (because the client can't figure out what the heck they really want it to do), and I have to continuously fight with the fact that the code has mixed styles in it. And I can't fix the variable names in one place, else I have to fix them in five.

Of course, that's indicative of just me being a stupid programmer more than anything, so I've amended my ways.


On topic though, I agree that style is a bit... unusual to me. I haven't done any .NET development, and understand that they have style guidelines, but even using member names like MemberFunction instead of memberFunction bugs me. Not sure why really. I've also never really been a big fan of using different name conventions for member variables versus other variables either. I've seen recommendations to use underscore separation for stack variables and camel case for member variables for instance, and while I can see some marginal benefits, it would bug me actually writing code like that.
Success requires no explanation. Failure allows none.

This is part of working with other people's code.


I prefer the Correct Bracing Style in curly-braces-oriented languages:

if(foo)
{
bar();
baz();
}



However, it is also popular to use the Incorrect Bracing Style:

if (foo) {
bar();
baz();
}


Indeed, I've now worked for several companies whose internal conventions mandate using the Incorrect style. This is just the price you pay for interacting with other programmers. It's worth getting over it sooner rather than later, because you will never work with anyone else who formats code exactly the way you like it.

It's somewhat like food. You can cook your own meals, and get them precisely as you want them; but if you want to eat at a restaurant, or at someone else's home, you'd better be willing to tolerate variety.


Can you explain why option 1 is the "correct bracing style" ? I use something like:

if( condition){
statement
}
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github

Can you explain why option 1 is the "correct bracing style" ? I use something like:

if( condition){
statement
}



It's not "incorrect". There is no correct or incorrect. Personally I prefer:
if()
{
}

Just because its what I'm used to and I like how the brackets line up (rather than if/while/for/whatever lining up to a }. I'd imagine people just stick to whatever they started with (once settled).

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

This topic is closed to new replies.

Advertisement