Skip to content

Commit 1beaed5

Browse files
author
David Kline
authored
Merge pull request #2779 from StephenHodgson/vNEXT-CodingGuidelines
vNEXT: Updated Coding Guidelines
2 parents a74f197 + 48e7942 commit 1beaed5

File tree

1 file changed

+86
-43
lines changed

1 file changed

+86
-43
lines changed

CodingGuidelines.md

Lines changed: 86 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This document outlines the recommended coding guidelines for the Mixed Reality T
88

99
All scripts posted to the MRTK should have the standard License header attached, exactly as shown below:
1010

11-
```
11+
```c#
1212
// Copyright (c) Microsoft Corporation. All rights reserved.
1313
// Licensed under the MIT License. See LICENSE in the project root for license information.
1414
```
@@ -19,7 +19,7 @@ Any script files submitted without the license header will be rejected
1919

2020
All public classes, structs, enums, functions, properties, fields posted to the MRTK should be described as to it's purpose and use, exactly as shown below:
2121

22-
```
22+
```c#
2323
/// <summary>
2424
/// The Controller definition defines the Controller as defined by the SDK / Unity.
2525
/// </summary>
@@ -38,16 +38,17 @@ This ensures documentation is properly generated and disseminated for all all cl
3838
3939
## MRTK namespace rules
4040

41-
The vNext structure adheres to a strict namespace culture of mapping the namespace 1-1 with the folder structure of the project. This ensures that classes are easy to discover and maintain. It also ensures the dependencies of any class are laid out in the beginning usings of the file.
41+
The vNext structure adheres to a strict namespace culture of mapping the namespace 1-1 with the folder structure of the project. This ensures that classes are easy to discover and maintain. It also ensures the dependencies of any class are laid out in the beginning using definitions of the file.
4242

4343
![](/External/ReadMeImages/MRTK-NameSpaceExample.png)
4444

4545
### Do:
46-
```
46+
47+
```c#
4748
// Copyright (c) Microsoft Corporation. All rights reserved.
4849
// Licensed under the MIT License. See LICENSE in the project root for license information.
4950
50-
namespace Microsoft.MixedReality.Toolkit.Internal.Definitons
51+
namespace Microsoft.MixedReality.Toolkit.Core.Definitions
5152
{
5253
/// <summary>
5354
/// The ButtonAction defines the set of actions exposed by a controller.
@@ -68,8 +69,8 @@ Additionally, ensure that spaces are added for conditional / loop functions like
6869

6970
### Don't:
7071

71-
```
72-
private Foo()
72+
```c#
73+
private Foo () // < - space between Foo and ()
7374
{
7475
if(Bar==null) // <- no space between if and ()
7576
{
@@ -85,7 +86,7 @@ private Foo()
8586

8687
### Do:
8788

88-
```
89+
```c#
8990
private Foo()
9091
{
9192
if (Bar==null)
@@ -100,21 +101,46 @@ private Foo()
100101
}
101102
```
102103

104+
## Spacing
105+
106+
Do not to add additional spaces between square brackets and parenthesis:
107+
108+
### Don't:
109+
110+
```c#
111+
private Foo()
112+
{
113+
int[ ] var = new int [ 9 ];
114+
Vector2 vector = new Vector2 ( 0f, 10f );
115+
}
116+
117+
```
118+
119+
### Do:
120+
121+
```c#
122+
private Foo()
123+
{
124+
int[] var = new int[9];
125+
Vector2 vector = new Vector2(0f, 10f);
126+
}
127+
```
128+
103129
## Naming Conventions
104130

105131
Always use `PascalCase` for public / protected / virtual properties, and `camelCase` for private properties and fields.
106132
>The only exception to this is for data structures that require the fields to be serialized by the `JsonUtility`.
107133
108134
### Don't:
109135

110-
```
136+
```c#
111137
public string myProperty; // <- Starts with a lower case letter
112138
private string MyProperty; // <- Starts with an uppercase case letter
113139
```
114140

115141
### Do:
116142

117-
```
143+
```c#
118144
public string MyProperty;
119145
protected string MyProperty;
120146
private string myProperty;
@@ -126,17 +152,29 @@ Always declare an access modifier for all fields, properties and methods.
126152

127153
>All Unity API Methods should be `private` by default, unless you need to override them in a derived class. In this case `protected` should be used.
128154
155+
>Fields should always be `private`, with `public` or `protected` property accessors.
156+
157+
>Use [expression-bodied members](https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6#expression-bodied-function-members) and [auto properties](https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6#auto-property-enhancements) where possible
158+
129159
### Don't:
130160

131-
```
161+
```c#
162+
// protected field should be private
163+
protected int myVariable = 0;
164+
165+
// property should have protected setter
166+
public int MyVariable { get { return myVariable; } }
167+
132168
// No public / private access modifiers
133169
void Foo() { }
134170
void Bar() { }
135171
```
136172

137173
### Do:
138174

139-
```
175+
```c#
176+
public int MyVariable { get; protected set; } = 0;
177+
140178
private void Foo() { }
141179
public void Bar() { }
142180
protected virtual void FooBar() { }
@@ -148,7 +186,7 @@ Always use braces after each statement block, and place them on the next line.
148186

149187
### Don't:
150188

151-
```
189+
```c#
152190
private Foo()
153191
{
154192
if (Bar==null) // <- missing braces surrounding if action
@@ -160,7 +198,7 @@ private Foo()
160198

161199
### Don't:
162200

163-
```
201+
```c#
164202
private Foo() { // <- Open bracket on same line
165203
if (Bar==null) DoThing(); <- if action on same line with no surrounding brackets
166204
else DoTheOtherThing();
@@ -169,7 +207,7 @@ private Foo() { // <- Open bracket on same line
169207

170208
### Do:
171209

172-
```
210+
```c#
173211
private Foo()
174212
{
175213
if (Bar==true)
@@ -189,7 +227,7 @@ If the class, struct, or enum can be made private then it's okay to be included
189227

190228
### Don't:
191229

192-
```
230+
```c#
193231
public class MyClass
194232
{
195233
public struct MyStruct() { }
@@ -200,7 +238,7 @@ public class MyClass
200238

201239
### Do:
202240

203-
```
241+
```c#
204242
// Private references for use inside the class only
205243
public class MyClass
206244
{
@@ -212,43 +250,43 @@ public class MyClass
212250

213251
### Do:
214252

215-
```
253+
MyStruct.cs
254+
```c#
216255
// Public Struct / Enum definitions for use in your class. Try to make them generic for reuse.
217-
MyStruct.cs
218256
public struct MyStruct
219257
{
220258
public string Var1;
221259
public string Var2;
222260
}
223261
```
224262

225-
```
226263
MyEnumType.cs
264+
```c#
227265
public enum MuEnumType
228266
{
229267
Value1,
230268
Value2 // <- note, no "," on last value to denote end of list.
231269
}
232270
```
233271

234-
```
235272
MyClass.cs
273+
```c#
236274
public class MyClass
237275
{
238276
private MyStruct myStructreference;
239277
private MyEnumType myEnumReference;
240278
}
241279
```
242280

243-
## Initilize Enums.
281+
## Initialize Enums.
244282

245-
To ensure all Enum's are initialized correctly starting at 0, .NET gives you a tidy shortcut to automatically initilize the enum by just adding the first (starter) value.
283+
To ensure all Enum's are initialized correctly starting at 0, .NET gives you a tidy shortcut to automatically initialize the enum by just adding the first (starter) value.
246284

247285
> E.G. Value 1 = 0 (Remaining values are not required)
248286
249287
### Don't:
250288

251-
```
289+
```c#
252290
public enum Value
253291
{
254292
Value1, <- no initializer
@@ -259,7 +297,7 @@ public enum Value
259297

260298
### Do:
261299

262-
```
300+
```c#
263301
public enum ValueType
264302
{
265303
Value1 = 0,
@@ -274,7 +312,7 @@ It is critical that if an Enum is likely to be extended in the future, to order
274312

275313
### Don't:
276314

277-
```
315+
```c#
278316
public enum SDKType
279317
{
280318
WindowsMR,
@@ -287,7 +325,7 @@ public enum SDKType
287325

288326
### Do:
289327

290-
```
328+
```c#
291329
/// <summary>
292330
/// The SDKType lists the VR SDK's that are supported by the MRTK
293331
/// Initially, this lists proposed SDK's, not all may be implemented at this time (please see ReleaseNotes for more details)
@@ -303,7 +341,7 @@ public enum SDKType
303341
/// </summary>
304342
Other,
305343
/// <summary>
306-
/// The Windows 10 Mixed reality SDK provided by the Universal Windows Platform (UWP), for Immersive MR headsets and Hololens.
344+
/// The Windows 10 Mixed reality SDK provided by the Universal Windows Platform (UWP), for Immersive MR headsets and HoloLens.
307345
/// </summary>
308346
WindowsMR,
309347
/// <summary>
@@ -355,7 +393,7 @@ If there is a possibility for an enum to require multiple states as a value, e.g
355393
356394
### Don't:
357395

358-
```
396+
```c#
359397
public enum Handedness
360398
{
361399
None,
@@ -366,7 +404,7 @@ public enum Handedness
366404

367405
### Do:
368406

369-
```
407+
```c#
370408
[flags]
371409
public enum HandednessType
372410
{
@@ -392,20 +430,20 @@ If you need to have the ability to edit your field in the inspector, it's best p
392430
393431
### Don't:
394432

395-
```
433+
```c#
396434
public float MyValue;
397435
```
398436

399437
### Do:
400438

401-
```
439+
```c#
402440
// private field, only accessible within script (field is not serialized in Unity)
403441
private float myValue;
404442
```
405443

406444
### Do:
407445

408-
```
446+
```c#
409447
// Enable private field to be configurable only in editor (field is correctly serialized in Unity)
410448
[SerializeField]
411449
private float myValue;
@@ -415,7 +453,7 @@ public float MyValue;
415453

416454
### Don't:
417455

418-
```
456+
```c#
419457
private float myValue1;
420458
private float myValue2;
421459

@@ -434,10 +472,15 @@ public float MyValue;
434472

435473
### Do:
436474

437-
```
475+
```c#
438476
// Enable field to be configurable in the editor and available externally to other scripts (field is correctly serialized in Unity)
439-
[SerializeField]
440-
private float myValue; // <- Notice we co-located the backing field above our corrisponding property.
477+
[SerializeField]
478+
[ToolTip("If using a tooltip, the text should match the public property's summary documentation, if appropriate.")]
479+
private float myValue; // <- Notice we co-located the backing field above our corresponding property.
480+
481+
/// <summary>
482+
/// If using a tooltip, the text should match the public property's summary documentation, if appropriate.
483+
/// </summary>
441484
public float MyValue
442485
{
443486
get{ return myValue; }
@@ -451,13 +494,13 @@ In some cases a foreach is required, e.g. when looping over an IEnumerable. But
451494

452495
### Don't:
453496

454-
```
497+
```c#
455498
foreach(var item in items)
456499
```
457500

458501
### Do:
459502

460-
```
503+
```c#
461504
int length = items.length; // cache reference to list/array length
462505
for(int i=0; i < length; i++)
463506
```
@@ -468,7 +511,7 @@ With the HoloLens in mind, it's best to optimize for performance and cache refer
468511

469512
### Don't:
470513

471-
```
514+
```c#
472515
void Update()
473516
{
474517
gameObject.GetComponent<Renderer>().Foo(Bar);
@@ -477,7 +520,7 @@ void Update()
477520

478521
### Do:
479522

480-
```
523+
```c#
481524
[SerializeField] // To enable setting the reference in the inspector.
482525
private Renderer myRenderer;
483526

@@ -502,7 +545,7 @@ Unity will create a new material each time you use ".material", which will cause
502545

503546
### Don't:
504547

505-
```
548+
```c#
506549
public class MyClass
507550
{
508551
void Update()
@@ -515,7 +558,7 @@ public class MyClass
515558

516559
### Do:
517560

518-
```
561+
```c#
519562
// Private references for use inside the class only
520563
public class MyClass
521564
{

0 commit comments

Comments
 (0)