Skip to content
Alex Feer edited this page Aug 10, 2015 · 1 revision

Code-style UI.Windows

Common

namespace MW2.Gameplay.Scene {

	public class HeroView : DatabaseMonoItem<HeroView> {

		public float param1;
		public int param2;

		private AnyType paramPrivate1;
		private AnyType paramPrivate2;
		
		private bool anyProperty {
			get;
			private set;
		}

		public void Method(int param1, int param2, bool param3 = false) {

			this.param1 = param1;
			this.param2 = param2;

			if (param3 == true) this.DoSomething();

		}

		public void DoSomething() {

			Debug.LogFormatted("{0}, {1}", this.param1, this.param2);

		}

	}

}

Braces

I write braces {} at the end of the line. I can not explain the reason for this, it's a habit.

if (...) {

	// something

}

Blank lines at the beginning and at the end only needs to quickly read the code.

Comparisons to null and bool

Each method that returns a bool or a variable that is bool, should be described completely. Not allowed reduction without indicating a comparison to true or false. The same applies to checks on null.

if (a == true) xxx
if (b == null) xxx

One-line if

if (...) xxx

One-line recording is possible only when one and only one action. This is not to make the transfer of the body to a new line without brackets.

This entry is necessary for quick one-line comments, and avoid unnecessary strings:

if (this.image1 != null) this.image1.Recycle();
if (this.image2 != null) this.image2.Recycle();

The code required such tests often.

Properties

All variable names are declared with a small letter. Properties is not an exception. Example for understanding:

this.Setup(this.Something);

In this example, I quickly realize that Something - a reference to the method, not the value of the property. Doubtful disadvantage of this approach is the fact that I can not distinguish the property from the field. But I believe that if property can not be taken when handling the parameters, it is the same field. Moreover, I try to avoid all property advertisements. It is better to call a method;)

this

this word is used everywhere in the class to refer to class variables and call methods. This is useful when you do not need to spend time thinking up names for variables.

public float value;

public float Set(float value) {

	this.value = value;

}

The advantage of this approach is the rapid reading and understanding of the code where the variable is declared - is a method or within a method.

Static

All static variables must be called with the name of the class where they are declared:

public class A {

	public static int param = 12;

	public void Method() {

		Debug.Log(A.param);

	}

}

Constants

Constants are declared in upper case. Callings should be the same as for static. Inside methods it must declared in camelCase.

public class A {
	
	public const int PARAM_SOMETHING_LONG = 123;
	
	public void Method() {
		
		const float insideMethodValue = 100f;

		var const1 = A.PARAM_SOMTHING_LONG;

	}
	
}

return word

Ideally, the word return should be used only once in the method. But it is not a must.

private int Method() {

	var result = 123;

	if (...) {

		result = 234;

	}

	return result;

}

Declaring Variables

We float suffix must be f. In double - d. It is necessary.

If possible, always use the var key - recording will take up less space. But sometimes, for an understanding of (or otherwise) need to use a specific type of data.

Declaring variables list (float a, b;) is undesirable.

var v = new Vector3(1f, 2f, 3f);

i++ or ++i

Almost always, we just need to add one of the original expression and the result is not important. Therefore, use ++i. An exception can only be treated to an array like this:

array[i++] = 1;
array[i++] = 2;
array[i++] = 3;

Enum

Always declare enum indicating the type of data:

public enum Type : byte {

	A,
	B,
	C,

}

After the last element of a comma to quickly add new items.

Inheritance on the type of data necessary in view of certain problems associated with custom enum in using string as a key enum type in Dictionary. It allocates memory for the string for each search.

Boxing/Unboxing

It is advisable to use a template classes and template method calls. Exceptions are very rare object and rarely used.

TODO

When there are events already in the GUI, you must implement each method. But a lot of methods and implement them can be long - each write // TODO:. It is advisable to comment. Also TODO write wherever algorithm can be implemented better. Then it cuts down the work.

Declaring Methods

When declaring a method written first scope, and then static, virtual, override. The very name of the method begins with a capital letter.

public static void Method() {}

It is important to understand that the most important scope. If the method is not available (e.g., private), the static or not it is not important.

Also, regardless of the scope required to specify it:

private void Method() {
}

It is necessary to faster reading of the code.

Calling Methods

No bias there. I want to still sometimes to understand the context, and sometimes it is even necessary. Therefore desired call context variables:

this.gameObject.SetActive(active: true);
this.image.SetStates(mainState: true, otherState: false, value: 5f);

Editor-functional

All functionality that want to do exclusively Editor'e must be in ifdef'e UNITY_EDITOR. Thus this code never gets in the production.

#if UNITY_EDITOR
public override void OnValidateEditor() {

	base.OnValidateEditor();

	if (this.gameObject.activeSelf == false) return;

	if (this.image == null) this.image = this.GetComponent<Image>();
	if (this.rawImage == null) this.rawImage = this.GetComponent<RawImage>();

}
#endif

Conclusion

If you follow the above rules, you can learn to read a printed piece of code, divorced from context.