I’m kind of having a hard time finding the correct style. I used to use getters and setters a lot, but I want to make a definite switch to properties. However, I now prefix my private vars with an underscore so that the names don’t cause any compile errors. I.e.: _width and width.
But doing this only with vars being used by properties seems ugly, especially when a property later becomes useless and I remove it, while perhaps leaving the underscore in the varname, which would be ugly.
I’m currently tempted to prefix every private with an underscore, but that looks like of weird to, almost bringing me back to the ActionScript 2 era
public ClassName
int _classVariable;
int ClassVariable { get; set; }
int _classVariableWithoutProperty;
public ClassName(){
int localVariable;
}
Basically, all my class-level variable are prefixed with an underscore - whether they have a property or not. Any variables scoped at the method level or more local don’t get the underscore (making it very easy to tell whether a variable is local to this method, or whether it’s a member variable).
I’ve seen a lot of C# programmers go with variables like “m_variableName” instead of just “_variableName” - which accomplishes the same thing and is equally valid in my opinion.
I would definitely use a naming convention where member variables, properties, and method variables all have a different style though - it just makes things easier to read when you go back six months from now (and lets you have names that all make sense without worrying about collisions).
Wouldn’t you want the property ClassVariable to be classVariable then? It feels odd for me to access it through className.ClassVariable (as I associate first character uppercase with classnames)
.Net naming convention dictates properties should be in Pascal case. When writing in C# I stick with the .Net naming conventions to keep consistent across all my C# projects (some in Unity, some standalone compiled against Mono, a couple compiled against .Net). When I work I flash I tend to use camel casing for properties though, to keep consistent with the environment.
If you only wrote C# when working with Unity, I could certainly see the merit in using camel casing for properties (since that’s how Unity Tech does it for all the Unity classes).
We C# folks don’t use _ to prefix variables (maybe with a very few exceptions).
The normal convention (which is also recommended from Microsoft) is to user lower camel case for private and local variables while using upper PascalCase for Class names, Methods, properties.
// Namespaces... Namespaces usually start with Company.ProductName or Company.TechnologyName or DeveloperName.ProductName/Technology
namespace Microsoft.XML { .. } // Microsoft = company, XML = Technology name
// MyClass, starting with upper case and each "word" in it starting with an upper case too. If we have a all Uppercase name, like XML and HTML, only the first letter will be Uppercase, rest will be lowercase: i.e. HttpRequest is correct, HTTPRequest is discuoraged. Same as XmlSerializer vs XMLSerializer.
public class MyClass : MonoBehaviour {
public const MaxPlayers = 8;
private bool hasNothing; // underscores in variables are not common in the recommended naming conventions. That's some hangover from C++/PHP/ruby/python-esque languages. Only exception on constants, see above
public int count;
public int Count {
get { return count; }
}
// you can also use the shortcut version (auto-property) if it's a simple variable which obtains/sets something but has to be public
// public variables in classes are generally discouraged in C#
// advantage of this attempt is, if you ever need to add validation, you just change the property and all the other code still works
public int Number { get; set; }
// You can also make have a Property where getter and setter have different accessors
// This property can be read by other classes, but only the class itself (and classes who derive from it can set it)
public int Ammo { get; protected set; }
// many bool properties are prefixed with "Is" or "Has"
public bool IsPlayer { get; set; } // Is the object player controlled
public bool HasWheels { get; set; } // Does this have wheels?
// methods always start uppercase
public void Fire() {
}
// events delegates
public delegate void HealthUpdatedEventHandler(Player player, float health); // adding the EventHandler
public event HealthUpdate; // not HealthUpdated, as it's usually used for methods who register to the event;
public MyClass() {
HealthUpdate += OnHealthUpdated;
}
void OnHealthUpdated(Player player, float health) {
// handle the event
}
}
// Enumarations. If you use a bitwise flag enumerations, which are used for configurations, you usually add "Options" at the end of the name
[flags]
public enum RenderOptions {
Opaque = 0x01,
ZBuffer = 0x02,
//etc.
}
// Interfaces always start with an I, followed by an Capital letter.
public interface IWeapon {
int HasAmmo { get; }
}
guess that’s due to their affinity for ActionScript-esque usage and UnityScript. In javascript it’s a typical convention to write the object properties as camelCase
Sorry for the somewhat late reply. Everything is clear to be and the uppercase convention seems to make more sense. However, since I’m switching a lot between languages which use somewhat the same convention, it will be hard (but not impossible of course) to make the mental switch when it comes to the conventions
I would like to thank everyone for their great replies. Clear as day to me now