Home |
ConditionsAndLoops.cs
|
1 /******************************************************************************
2 Style Guidelines:
3 * Readability - code is easy to read and to understand
4 * Standardisation - all team members are aligned with same code style
5 * Self-descriptive code - code is easy to understand without comments
6 * Debuggable - code is easy to debug (IDE friendly)
7 ******************************************************************************/
8
9 // using (import) of other namespaces will be wrapped with region "Using"
10 #region Using
11 using System;
12 using System.Collections.Generic;
13 using MyNameSpace.Enums;
14 using UnityEngine;
15
16 #endregion
17
18 // types are defined within the scope of namespace
19 // space name starts with Upper case letter
20 // each following word starts with upper Case letter
21 // sub-spaces are divided by '.' (dot)
22 // here "MyNameSpace" is main space and "Conditions" is sub-space
23 namespace MyNameSpace.Conditions
24 {
25 /// <summary>
26 /// type name starts with Upper case letter
27 /// each following word starts with Upper case letter
28 /// <remarks>
29 /// type = class, enum, struct, delegate. interface, etc
30 /// </remarks>
31 /// </summary>
32 public static class Extensions
33 {
34 /// <summary>
35 /// function name starts with Upper case letter
36 /// each following word starts with Upper case letter
37 /// </summary>
38 /// <remarks>
39 /// boolean functions/properties usually start with "Is" word
40 /// </remarks>
41 /// <param name="myEnum">
42 /// parameter name starts with lower case letter
43 /// each following word starts with Upper case letter
44 /// </param>
45 /// <param name="enumMember">
46 /// parameter name starts with lower case letter
47 /// each following word starts with Upper case letter
48 /// </param>
49 /// <returns>
50 /// specification of what will be returned is very useful in function comment header
51 /// </returns>
52 /// <exception cref="ArgumentOutOfRangeException">
53 /// exception(s) thrown in function are specified in it's comment header
54 /// </exception>
55 public static bool IsSame(this MyEnum myEnum, MyEnum enumMember)
56 {
57 // since 'result' is initialized with 'false' value, 'var' can be used here
58 var result = false;
59 switch (myEnum)
60 {
61 case MyEnum.EnumMember1:
62 if (enumMember == MyEnum.EnumMember1)
63 {
64 result = true;
65 }
66 break;
67
68 case MyEnum.EnumMember2:
69 if (enumMember == MyEnum.EnumMember2)
70 {
71 result = true;
72 }
73 break;
74
75 default:
76 throw new ArgumentOutOfRangeException(nameof(myEnum), myEnum, null);
77 }
78 return result;
79 }
80
81 /// <summary>
82 /// function name starts with Upper case letter
83 /// each following word starts with Upper case letter
84 /// </summary>
85 /// <param name="string">
86 /// parameter name starts with lower case letter
87 /// each following word starts with Upper case letter
88 /// if parameter name collides with type name(s) '@' character predicate can be used
89 /// </param>
90 /// <returns>
91 /// specification of what will be returned is very useful in function comment header
92 /// </returns>
93 public static bool IsNullOrEmpty(this string @string)
94 {
95 // 'var' can be used here instead of 'bool' since it is clear
96 // what value type is returned from 'string.IsNullOrEmpty(@string);'
97 var result = string.IsNullOrEmpty(@string);
98 // result variable is used here to capture the value for better debugging experience
99 return result;
100 }
101
102 /// <summary>
103 /// function name starts with Upper case letter
104 /// each following word starts with Upper case letter
105 /// </summary>
106 /// <param name="array">
107 /// parameter name starts with lower case letter
108 /// each following word starts with Upper case letter
109 /// </param>
110 /// <typeparam name="T">
111 /// generic type parameter name starts with Upper case letter
112 /// each following word starts with Upper case letter
113 /// in case of single parameter, single letter 'T' can be used
114 /// </typeparam>
115 public static void Reset<T>(this T[] array)
116 { // <- opening parenthesis, new line after method name
117 for (var i = 0; i < array.Length; i++)
118 { // <- opening parenthesis, new line after 'for' loop definition line
119 array[i] = default(T);
120 // alternative: array[i] = default; // no need to specify (T)
121 } // <- closing parenthesis, new line after last loop code line
122 } // <- closing parenthesis, new line after last method code line
123
124 /// <summary>
125 /// function name starts with Upper case letter
126 /// each following word starts with Upper case letter
127 /// </summary>
128 /// <param name="list">
129 /// parameter name starts with lower case letter
130 /// each following word starts with Upper case letter
131 /// </param>
132 /// <typeparam name="T">
133 /// generic type parameter name starts with Upper case letter
134 /// each following word starts with Upper case letter
135 /// in case of single parameter, single letter 'T' can be used
136 /// </typeparam>
137 public static void Print<T>(this IEnumerable<T> list)
138 {
139 // disposable resources will be defined within scope of 'using' statement
140 using (var e = list.GetEnumerator())
141 { // <- opening parenthesis, new line after 'using' definition line
142 while (e.MoveNext())
143 { // <- opening parenthesis, new line after 'while' loop definition line
144 Debug.Log(e.Current);
145 } // <- closing parenthesis, new line after last loop code line
146 } // <- closing parenthesis, new line after last 'using' code line
147 }
148 }
149 }