|
| 1 | +\documentclass[11pt,a4paper]{book} |
| 2 | +\usepackage[utf8]{inputenc} |
| 3 | +\usepackage[english]{babel} |
| 4 | +\usepackage{paralist} |
| 5 | + |
| 6 | +\usepackage{doavoidnot} |
| 7 | + |
| 8 | +\author{Gergő Balogh} |
| 9 | +\title{Code Convention} |
| 10 | +\begin{document} |
| 11 | +\maketitle |
| 12 | + |
| 13 | +\chapter{C\#} |
| 14 | + |
| 15 | +\section{Capitalization styles} |
| 16 | +\paragraph{Pascal case} The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized. \verb|PascalCase| |
| 17 | +\paragraph{Camel case} The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized. \verb|camelCase| |
| 18 | +\paragraph{Uppercase} All letters in the identifier are capitalized. \verb|UPPERCASE| |
| 19 | + |
| 20 | +\section{Naming} |
| 21 | + |
| 22 | +\domark Use pascal case if the visibility of entity is public. |
| 23 | +\begin{verbatim} |
| 24 | +public |
| 25 | +void Something() |
| 26 | +\end{verbatim} |
| 27 | + |
| 28 | +\domark Use camel case if the visibility of method, attribute, property or event is non-public. |
| 29 | +\begin{verbatim} |
| 30 | +private |
| 31 | +void something() |
| 32 | +
|
| 33 | +internal |
| 34 | +void something() |
| 35 | +\end{verbatim} |
| 36 | + |
| 37 | +\domark Use camel case and start the identifier with an underscore (\_) if the visibility of variable is local. |
| 38 | +\begin{verbatim} |
| 39 | +internal |
| 40 | +void something() |
| 41 | +{ |
| 42 | + int _count; |
| 43 | +} |
| 44 | +\end{verbatim} |
| 45 | + |
| 46 | +\whymark You can use code completion to quickly list all local variables, just type the underscore sing (\_). |
| 47 | + |
| 48 | +\domark Use camel case and start the name of parameters of lambda expressions with an underscore (\_). |
| 49 | +\begin{verbatim} |
| 50 | +var foo = ( _a, _b ) => _a + _b; |
| 51 | +\end{verbatim} |
| 52 | + |
| 53 | +\domark Use the variable name \_ (a single underscore) to denote variables in lambda expressions, which is irrelevant in the current context. |
| 54 | +\begin{verbatim} |
| 55 | +(_, _b) => _b + 1 |
| 56 | +\end{verbatim} |
| 57 | + |
| 58 | +\domark Use pascal case in the name of namespaces, classes, interfaces, structures, enumerations and members of enumerations. |
| 59 | + |
| 60 | +\domark Start name of interfaces with a capital I. |
| 61 | + |
| 62 | +\domark Start name of generic type parameters with a capital T. |
| 63 | +\begin{verbatim} |
| 64 | +class ExampleClass< TValue > |
| 65 | +
|
| 66 | +interface IExampleInterface |
| 67 | +
|
| 68 | +enum ExampleEnum |
| 69 | +
|
| 70 | +struct ExampleStruct |
| 71 | +\end{verbatim} |
| 72 | + |
| 73 | +\domark Use camel case in the name of parameters of methods. |
| 74 | +\begin{verbatim} |
| 75 | +void Somthing( int somethingToRead ) |
| 76 | +\end{verbatim} |
| 77 | + |
| 78 | +\notmark Do not use Hungarian notion. |
| 79 | +\begin{verbatim} |
| 80 | +int i_foo; |
| 81 | +float f_foo; |
| 82 | +\end{verbatim} |
| 83 | + |
| 84 | +\whymark It usually results cryptic abbreviation and types are noted and shown by Visual Studio or any other IDE. |
| 85 | + |
| 86 | +\avoidmark Avoid using single letter variable names. |
| 87 | +\begin{verbatim} |
| 88 | +//avoid |
| 89 | +int a; |
| 90 | +int b; |
| 91 | +
|
| 92 | +//do |
| 93 | +int componentA; |
| 94 | +int componentB; |
| 95 | +\end{verbatim} |
| 96 | + |
| 97 | +\notmark Do not mark member with m\_ prefix. |
| 98 | +\begin{verbatim} |
| 99 | +int m_length //wrong |
| 100 | +
|
| 101 | +int length //right |
| 102 | +\end{verbatim} |
| 103 | + |
| 104 | +\notmark Do not use white-spaces to construct columns in the source code. |
| 105 | +\begin{verbatim} |
| 106 | +//do |
| 107 | +int length = 10; |
| 108 | +string name = "Thangorodrim; |
| 109 | +
|
| 110 | +//wrong |
| 111 | +int length = 10; |
| 112 | +string name = "Thangorodrim; |
| 113 | +\end{verbatim} |
| 114 | + |
| 115 | +\whymark It suggest structures which do not exist. |
| 116 | + |
| 117 | +\avoidmark Avoid uncommon abbreviation. Use abbreviation, when you have no other choice. |
| 118 | +\begin{verbatim} |
| 119 | +string Idkwit = "true"; //I Don't Know What Is This |
| 120 | +\end{verbatim} |
| 121 | + |
| 122 | +\domark Use the same case as in their first letter of abbreviation less then three character long. |
| 123 | +\begin{verbatim} |
| 124 | +private |
| 125 | +void uiReader() |
| 126 | +
|
| 127 | +private |
| 128 | +void readerUI() |
| 129 | +
|
| 130 | +public |
| 131 | +void UIReader() |
| 132 | +\end{verbatim} |
| 133 | + |
| 134 | +\domark Use camel case in abbreviation more then two letters long. |
| 135 | +\begin{verbatim} |
| 136 | +private |
| 137 | +void umlReader() |
| 138 | +
|
| 139 | +public |
| 140 | +void UmlReader() |
| 141 | +\end{verbatim} |
| 142 | + |
| 143 | +\avoidmark Avoid the using the following words. |
| 144 | +\begin{compactitem} |
| 145 | +\item data |
| 146 | +\item information |
| 147 | +\item some |
| 148 | +\item do |
| 149 | +\item make |
| 150 | +\end{compactitem} |
| 151 | + |
| 152 | +\whymark Because they are too general to provide useful information. |
| 153 | + |
| 154 | +\notmark Do not use plural form in the name of namespaces, classes, interfaces, structures, enumerations and members of enumerations. |
| 155 | +\begin{verbatim} |
| 156 | +//wrong |
| 157 | +items |
| 158 | +men |
| 159 | +
|
| 160 | +//right |
| 161 | +itemCollection |
| 162 | +manCollection |
| 163 | +\end{verbatim} |
| 164 | + |
| 165 | +\whymark It is easy to misread the plural and singular forms. Can not find irregular forms by searching singulars. |
| 166 | + |
| 167 | +\section{Layout} |
| 168 | + |
| 169 | +\avoidmark Avoid lines longer then 80 characters. Prefer shorter lines as much as possible. |
| 170 | + |
| 171 | +\subsection{Horizontal spacing} |
| 172 | + |
| 173 | +\notmark Do not put spaces inside empty parentheses. |
| 174 | + |
| 175 | +\domark Use spaces at the inner side of all parentheses, except curly braces. |
| 176 | +\begin{verbatim} |
| 177 | +( Something< int >( _foo[ bla ] ) ) * 10 |
| 178 | +\end{verbatim} |
| 179 | + |
| 180 | +\domark Use spaces around operators, except parentheses, point (.), increment (\verb|++|) and decrement (\verb|--|) operators. |
| 181 | +\begin{verbatim} |
| 182 | +int foo = ( ( x == 0 ) ? ( z + 3 ) : bar.some[ 42 ] ) |
| 183 | +foo++; |
| 184 | +\end{verbatim} |
| 185 | + |
| 186 | +\domark Use spaces instead of tabs at the beginning of the line. |
| 187 | + |
| 188 | +\domark Use four spaces per each indentation level. 4 spaces = 1 tab |
| 189 | + |
| 190 | +\notmark Do not mix tabs and spaces at the beginning of the line. |
| 191 | + |
| 192 | +\domark Only increment the indentation level one-by-one. |
| 193 | + |
| 194 | +\subsection{Line-breaks} |
| 195 | + |
| 196 | +\domark Put curly braces in their own empty lines. |
| 197 | +\begin{verbatim} |
| 198 | +void foo() |
| 199 | +{ |
| 200 | + if( true ) |
| 201 | + { |
| 202 | + bla++; |
| 203 | + } |
| 204 | +} |
| 205 | +\end{verbatim} |
| 206 | + |
| 207 | +\domark Put member modifiers in a separate line. |
| 208 | +\begin{verbatim} |
| 209 | +public static |
| 210 | +void Something() |
| 211 | +
|
| 212 | +private |
| 213 | +int count; |
| 214 | +\end{verbatim} |
| 215 | + |
| 216 | +\domark Write getter and setter with one or no modifier in the same line with the name of auto property. |
| 217 | +\begin{verbatim} |
| 218 | +public |
| 219 | +int Count{ get; private set; } |
| 220 | +\end{verbatim} |
| 221 | + |
| 222 | +\domark If necessary break line after equal sign (=). |
| 223 | +\begin{verbatim} |
| 224 | +int _count = |
| 225 | + x + 567567563 / 2121231 + foobar; |
| 226 | +\end{verbatim} |
| 227 | + |
| 228 | +\domark If necessary break line before operators, except parentheses and assignment (=). |
| 229 | +\begin{verbatim} |
| 230 | +string _text = |
| 231 | + "this is a long string need to be" |
| 232 | + + "broken into seperate lines"; |
| 233 | +
|
| 234 | +"to write something" |
| 235 | + .Let().TaggedAsInformation() |
| 236 | + .Write(); |
| 237 | +\end{verbatim} |
| 238 | + |
| 239 | +\domark Put the closing parentheses in the same line with the last parameter, except curly braces. |
| 240 | +\begin{verbatim} |
| 241 | +int grantPermissions( |
| 242 | + string user, |
| 243 | + string password, |
| 244 | + int id, |
| 245 | + PersmissionSet permission ) |
| 246 | +\end{verbatim} |
| 247 | + |
| 248 | +\domark If necessary prefer to start method calls in a new line. |
| 249 | + |
| 250 | +\domark Put all or none of the parameters into separate lines. |
| 251 | +\begin{verbatim} |
| 252 | +int count = |
| 253 | + AvarageOf( |
| 254 | + foo, |
| 255 | + bar, |
| 256 | + asd ); |
| 257 | +\end{verbatim} |
| 258 | + |
| 259 | +\subsection{Vertical spaces} |
| 260 | + |
| 261 | +\domark Separate members in classes with an empty lines. |
| 262 | +\begin{verbatim} |
| 263 | +private |
| 264 | +int foo |
| 265 | +
|
| 266 | +public |
| 267 | +void Something() |
| 268 | +\end{verbatim} |
| 269 | + |
| 270 | +\section{Commenting} |
| 271 | + |
| 272 | +\domark Prefer documenting comments (///) over general one (//). |
| 273 | + |
| 274 | +\notmark Do not use block comments (/* */). |
| 275 | + |
| 276 | +\appendix |
| 277 | + |
| 278 | +\chapter{C\#} |
| 279 | + |
| 280 | +\section{EditorConfig for Visual Studio} |
| 281 | + |
| 282 | +For further details and \emph{some} automatic style and convention settings please use the \texttt{.editorcongif} file next to this document. It only contains Visual Studio and C\#{} specific settings. This file do not cover every details present in this documents and provided \emph{as is}. |
| 283 | + |
| 284 | +\end{document} |
0 commit comments