You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CodingGuidelines.md
+86-43Lines changed: 86 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ This document outlines the recommended coding guidelines for the Mixed Reality T
8
8
9
9
All scripts posted to the MRTK should have the standard License header attached, exactly as shown below:
10
10
11
-
```
11
+
```c#
12
12
// Copyright (c) Microsoft Corporation. All rights reserved.
13
13
// Licensed under the MIT License. See LICENSE in the project root for license information.
14
14
```
@@ -19,7 +19,7 @@ Any script files submitted without the license header will be rejected
19
19
20
20
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:
21
21
22
-
```
22
+
```c#
23
23
/// <summary>
24
24
/// The Controller definition defines the Controller as defined by the SDK / Unity.
25
25
/// </summary>
@@ -38,16 +38,17 @@ This ensures documentation is properly generated and disseminated for all all cl
38
38
39
39
## MRTK namespace rules
40
40
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.
/// 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
68
69
69
70
### Don't:
70
71
71
-
```
72
-
private Foo()
72
+
```c#
73
+
privateFoo () // < - space between Foo and ()
73
74
{
74
75
if(Bar==null) // <- no space between if and ()
75
76
{
@@ -85,7 +86,7 @@ private Foo()
85
86
86
87
### Do:
87
88
88
-
```
89
+
```c#
89
90
privateFoo()
90
91
{
91
92
if (Bar==null)
@@ -100,21 +101,46 @@ private Foo()
100
101
}
101
102
```
102
103
104
+
## Spacing
105
+
106
+
Do not to add additional spaces between square brackets and parenthesis:
107
+
108
+
### Don't:
109
+
110
+
```c#
111
+
privateFoo()
112
+
{
113
+
int[ ] var=newint [ 9 ];
114
+
Vector2vector=newVector2 ( 0f, 10f );
115
+
}
116
+
117
+
```
118
+
119
+
### Do:
120
+
121
+
```c#
122
+
privateFoo()
123
+
{
124
+
int[] var=newint[9];
125
+
Vector2vector=newVector2(0f, 10f);
126
+
}
127
+
```
128
+
103
129
## Naming Conventions
104
130
105
131
Always use `PascalCase` for public / protected / virtual properties, and `camelCase` for private properties and fields.
106
132
>The only exception to this is for data structures that require the fields to be serialized by the `JsonUtility`.
107
133
108
134
### Don't:
109
135
110
-
```
136
+
```c#
111
137
publicstringmyProperty; // <- Starts with a lower case letter
112
138
privatestringMyProperty; // <- Starts with an uppercase case letter
113
139
```
114
140
115
141
### Do:
116
142
117
-
```
143
+
```c#
118
144
publicstringMyProperty;
119
145
protectedstringMyProperty;
120
146
privatestringmyProperty;
@@ -126,17 +152,29 @@ Always declare an access modifier for all fields, properties and methods.
126
152
127
153
>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.
128
154
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
+
129
159
### Don't:
130
160
131
-
```
161
+
```c#
162
+
// protected field should be private
163
+
protectedintmyVariable=0;
164
+
165
+
// property should have protected setter
166
+
publicintMyVariable { get { returnmyVariable; } }
167
+
132
168
// No public / private access modifiers
133
169
voidFoo() { }
134
170
voidBar() { }
135
171
```
136
172
137
173
### Do:
138
174
139
-
```
175
+
```c#
176
+
publicintMyVariable { get; protectedset; } =0;
177
+
140
178
privatevoidFoo() { }
141
179
publicvoidBar() { }
142
180
protectedvirtualvoidFooBar() { }
@@ -148,7 +186,7 @@ Always use braces after each statement block, and place them on the next line.
148
186
149
187
### Don't:
150
188
151
-
```
189
+
```c#
152
190
privateFoo()
153
191
{
154
192
if (Bar==null) // <- missing braces surrounding if action
@@ -160,7 +198,7 @@ private Foo()
160
198
161
199
### Don't:
162
200
163
-
```
201
+
```c#
164
202
privateFoo() { // <- Open bracket on same line
165
203
if (Bar==null) DoThing(); <-ifactiononsamelinewithnosurroundingbrackets
166
204
elseDoTheOtherThing();
@@ -169,7 +207,7 @@ private Foo() { // <- Open bracket on same line
169
207
170
208
### Do:
171
209
172
-
```
210
+
```c#
173
211
privateFoo()
174
212
{
175
213
if (Bar==true)
@@ -189,7 +227,7 @@ If the class, struct, or enum can be made private then it's okay to be included
189
227
190
228
### Don't:
191
229
192
-
```
230
+
```c#
193
231
publicclassMyClass
194
232
{
195
233
publicstructMyStruct() { }
@@ -200,7 +238,7 @@ public class MyClass
200
238
201
239
### Do:
202
240
203
-
```
241
+
```c#
204
242
// Private references for use inside the class only
205
243
publicclassMyClass
206
244
{
@@ -212,43 +250,43 @@ public class MyClass
212
250
213
251
### Do:
214
252
215
-
```
253
+
MyStruct.cs
254
+
```c#
216
255
// Public Struct / Enum definitions for use in your class. Try to make them generic for reuse.
217
-
MyStruct.cs
218
256
publicstructMyStruct
219
257
{
220
258
publicstringVar1;
221
259
publicstringVar2;
222
260
}
223
261
```
224
262
225
-
```
226
263
MyEnumType.cs
264
+
```c#
227
265
publicenumMuEnumType
228
266
{
229
267
Value1,
230
268
Value2// <- note, no "," on last value to denote end of list.
231
269
}
232
270
```
233
271
234
-
```
235
272
MyClass.cs
273
+
```c#
236
274
publicclassMyClass
237
275
{
238
276
privateMyStructmyStructreference;
239
277
privateMyEnumTypemyEnumReference;
240
278
}
241
279
```
242
280
243
-
## Initilize Enums.
281
+
## Initialize Enums.
244
282
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.
246
284
247
285
> E.G. Value 1 = 0 (Remaining values are not required)
248
286
249
287
### Don't:
250
288
251
-
```
289
+
```c#
252
290
publicenumValue
253
291
{
254
292
Value1, <- no initializer
@@ -259,7 +297,7 @@ public enum Value
259
297
260
298
### Do:
261
299
262
-
```
300
+
```c#
263
301
publicenumValueType
264
302
{
265
303
Value1=0,
@@ -274,7 +312,7 @@ It is critical that if an Enum is likely to be extended in the future, to order
274
312
275
313
### Don't:
276
314
277
-
```
315
+
```c#
278
316
publicenumSDKType
279
317
{
280
318
WindowsMR,
@@ -287,7 +325,7 @@ public enum SDKType
287
325
288
326
### Do:
289
327
290
-
```
328
+
```c#
291
329
/// <summary>
292
330
/// The SDKType lists the VR SDK's that are supported by the MRTK
293
331
/// 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
303
341
/// </summary>
304
342
Other,
305
343
/// <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.
307
345
/// </summary>
308
346
WindowsMR,
309
347
/// <summary>
@@ -355,7 +393,7 @@ If there is a possibility for an enum to require multiple states as a value, e.g
355
393
356
394
### Don't:
357
395
358
-
```
396
+
```c#
359
397
publicenumHandedness
360
398
{
361
399
None,
@@ -366,7 +404,7 @@ public enum Handedness
366
404
367
405
### Do:
368
406
369
-
```
407
+
```c#
370
408
[flags]
371
409
publicenumHandednessType
372
410
{
@@ -392,20 +430,20 @@ If you need to have the ability to edit your field in the inspector, it's best p
392
430
393
431
### Don't:
394
432
395
-
```
433
+
```c#
396
434
publicfloatMyValue;
397
435
```
398
436
399
437
### Do:
400
438
401
-
```
439
+
```c#
402
440
// private field, only accessible within script (field is not serialized in Unity)
403
441
privatefloatmyValue;
404
442
```
405
443
406
444
### Do:
407
445
408
-
```
446
+
```c#
409
447
// Enable private field to be configurable only in editor (field is correctly serialized in Unity)
410
448
[SerializeField]
411
449
privatefloatmyValue;
@@ -415,7 +453,7 @@ public float MyValue;
415
453
416
454
### Don't:
417
455
418
-
```
456
+
```c#
419
457
privatefloatmyValue1;
420
458
privatefloatmyValue2;
421
459
@@ -434,10 +472,15 @@ public float MyValue;
434
472
435
473
### Do:
436
474
437
-
```
475
+
```c#
438
476
// 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
+
privatefloatmyValue; // <- 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>
441
484
publicfloatMyValue
442
485
{
443
486
get{ returnmyValue; }
@@ -451,13 +494,13 @@ In some cases a foreach is required, e.g. when looping over an IEnumerable. But
451
494
452
495
### Don't:
453
496
454
-
```
497
+
```c#
455
498
foreach(variteminitems)
456
499
```
457
500
458
501
### Do:
459
502
460
-
```
503
+
```c#
461
504
intlength=items.length; // cache reference to list/array length
462
505
for(inti=0; i<length; i++)
463
506
```
@@ -468,7 +511,7 @@ With the HoloLens in mind, it's best to optimize for performance and cache refer
468
511
469
512
### Don't:
470
513
471
-
```
514
+
```c#
472
515
voidUpdate()
473
516
{
474
517
gameObject.GetComponent<Renderer>().Foo(Bar);
@@ -477,7 +520,7 @@ void Update()
477
520
478
521
### Do:
479
522
480
-
```
523
+
```c#
481
524
[SerializeField] // To enable setting the reference in the inspector.
482
525
privateRenderermyRenderer;
483
526
@@ -502,7 +545,7 @@ Unity will create a new material each time you use ".material", which will cause
502
545
503
546
### Don't:
504
547
505
-
```
548
+
```c#
506
549
publicclassMyClass
507
550
{
508
551
voidUpdate()
@@ -515,7 +558,7 @@ public class MyClass
515
558
516
559
### Do:
517
560
518
-
```
561
+
```c#
519
562
// Private references for use inside the class only
0 commit comments