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 (imports) of other namespaces will be wrapped with region "Using" 10 #region Using11 using System;12 using MyNameSpace.Delegates;13 using MyNameSpace.Interfaces;14 #endregion15 16 // types are defined within the scope of namespace17 // space name starts with Upper case letter18 // each following word starts with upper Case letter19 // sub-spaces are divided by '.' (dot)20 // here "MyNameSpace" is main space and "Classes" is sub-space21 namespace MyNameSpace.Classes22 { // <- opening parenthesis, new line after namespace name23 /// <summary>24 /// type name starts with Upper case letter25 /// each following word starts with Upper case letter26 /// <remarks>27 /// type = class, enum, struct, delegate. interface, etc28 /// </remarks>29 /// </summary>30 public class MyCodeStyle : IMyInterface31 { // <- opening parenthesis, new line after type name32 /// <summary>33 /// private variable name starts with "_" (underscore) character34 /// and with lower case letter afterwards35 /// each following word starts with Upper case letter36 /// </summary>37 private int _myInt;38 39 /// <summary>40 /// protected variable name starts with Upper case letter41 /// each following word starts with Upper case letter42 /// </summary>43 protected float MyFloat;44 45 /// <summary>46 /// type scope const name starts with Upper case letter47 /// each following word starts with Upper case letter48 /// </summary>49 public const string MyConstString = "Hello My Style!";50 51 /// <summary>52 /// property name starts with Upper case letter53 /// each following word starts with Upper case letter54 /// </summary>55 public int MyIntProperty56 { // <- opening parenthesis, new line after property name57 // if simple return, can save line space58 // alternative syntax: get => _myInt;59 get { return _myInt; }60 // if public setter is not necessary, private or protected will be used here61 private set62 { // <- opening parenthesis, new line after set/get instruction63 64 // conditional operator '?' usage 65 _myInt = value >= 0 ? value : 0;66 67 // similar code for easier debugging and future extension68 // with parenthesis69 if (value >= 0)70 {71 _myInt = value;72 }73 else74 {75 _myInt = 0;76 }77 78 // similar code for easier debugging and future extension79 // without parenthesis80 if (value >= 0)81 _myInt = value;82 else83 _myInt = 0;84 } // <- closing parenthesis, new line after last set/get code line85 } // <- closing parenthesis, new line after last property code line86 87 /// <summary>88 /// public event name starts with Upper case letter89 /// each following word starts with Upper case letter90 /// <remarks>91 /// this is simplified style of declaration,92 /// in case no logic in adder/remover is required93 /// </remarks> 94 /// </summary>95 public event MyDelegate MyEvent;96 97 /// <summary>98 /// private event/delegate name starts with "_" (underscore) character99 /// and with lower case letter afterwards100 /// each following word starts with Upper case letter101 /// </summary>102 private event MyDelegate _myEvent;103 104 // alternative syntax to prevent null ref. exc.105 // private event MyDelegate _myEvent = delegate(int value) { };106 107 /// <summary>108 /// public event name starts with Upper case letter109 /// each following word starts with Upper case letter110 /// <remarks>111 /// this style uses expression body style with '=>' operator112 /// code block 'wraps' access for '_myEvent'113 /// </remarks> 114 /// </summary>115 public event MyDelegate MyEventWrapper1116 {117 add => _myEvent += value;118 remove => _myEvent -= value;119 }120 121 /// <summary>122 /// public event name starts with Upper case letter123 /// each following word starts with Upper case letter124 /// <remarks>125 /// this style uses parenthesis126 /// code block 'wraps' access for '_myEvent' and add logic code127 /// </remarks> 128 /// </summary>129 public event MyDelegate MyEventWrapper2130 {131 add132 {133 // exception throwing code blocked wrapped with parenthesis to allow better debugging134 if (value == null)135 {136 throw new NullReferenceException("Null value cannot be accepted");137 }138 _myEvent += value;139 }140 remove141 {142 // short code block, but not debugging friendly143 _myEvent -= 144 value ?? 145 // throwing exception here is not a good practice146 // see next block with better example147 throw new NullReferenceException("Null value cannot be accepted");148 149 // alternative: exception throwing code blocked wrapped with parenthesis to allow better debugging150 if (value == null)151 {152 throw new NullReferenceException("Null value cannot be accepted");153 }154 _myEvent -= value;155 }156 }157 158 /// <summary>159 /// method name starts with Upper case letter160 /// each following word starts with Upper case letter161 /// </summary>162 /// <param name="numericValue">163 /// parameter name starts with lower case letter164 /// each following word starts with Upper case letter165 /// </param>166 public void MyMethod(int numericValue)167 { // <- opening parenthesis, new line after method name168 // TODO comment169 // BUG comment170 // NOTE comment171 } // <- closing parenthesis, new line after last method code line172 173 /// <summary>174 /// function name starts with Upper case letter175 /// each following word starts with Upper case letter176 /// </summary>177 /// <param name="worldName">178 /// parameter name starts with lower case letter179 /// each following word starts with Upper case letter180 /// </param>181 /// <returns></returns>182 protected string MyFunction(string worldName)183 {184 // new way: string interpolation with '$' operator (predicate)185 var str1 = $"Hello {worldName}!";186 187 // old way: use string.Format(...) function to interpolate strings188 var str2 = string.Format("Hello {0}!", worldName);189 190 return str1 + str2;191 }192 } // <- closing parenthesis, new line after last type code line193 } // <- closing parenthesis, new line after last space code line