CodingStyle 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. * Coding Style for the Mono C# source code.
  2. * Class Libraries and Assembly Layout
  3. The class libraries are grouped together in the assemblies
  4. they belong.
  5. Each directory here represents an assembly, and inside each
  6. directory we divide the code based on the namespace they
  7. implement.
  8. In addition, each assembly directory contains a Test directory
  9. that holds the NUnit tests for that assembly.
  10. We use a new build system which is described by various README
  11. files in mcs/build
  12. The build process typically builds an assembly, but in some
  13. cases it also builds special versions of the assemblies
  14. intended to be used for testing.
  15. * Missing implementation bits
  16. If you implement a class and you are missing implementation bits,
  17. please use the attribute [MonoTODO]. This attribute can be used
  18. to programatically generate our status web pages:
  19. [MonoTODO("My Function is not available on Mono")]
  20. int MyFunction ()
  21. {
  22. throw new NotImplementedException ();
  23. }
  24. Ideally, write a human description of the reason why there is
  25. a MonoTODO, this will be useful in the future for our
  26. automated tools that can assist in developers porting their code.
  27. * Tagging buggy code
  28. If there is a bug in your implementation tag the problem by using
  29. the word "FIXME" in the code, together with a description of the
  30. problem.
  31. Do not use XXX or obscure descriptions, because otherwise people
  32. will not be able to understand what you mean.
  33. * Tagging Problematic specs.
  34. If the documentation and the Microsoft implementation do
  35. differ (you wrote a test case to prove this), I suggest that you edit
  36. the file `mcs/class/doc/API-notes' so we can keep track of these problems
  37. and submit our comments to ECMA or Microsoft and seek clarification.
  38. Sometimes the documentation might be buggy, and sometimes the implementation
  39. might be buggy. Lets try to identify and pinpoint which one
  40. is the correct one.
  41. Sometimes the specification will be lame (consider Version.ToString (fieldCount)
  42. where there is no way of knowing how many fields are available, making the API
  43. not only stupid, but leading to unreliable code).
  44. In those cases, use the keyword "LAMESPEC".
  45. * Coding considerations and style.
  46. In order to keep the code consistent, please use the following
  47. conventions. From here on `good' and `bad' are used to attribute
  48. things that would make the coding style match, or not match. It is not
  49. a judgement call on your coding abilities, but more of a style and
  50. look call. Please try to follow these guidelines to ensure prettiness.
  51. Use 8 space tabs for writing your code (hopefully we can keep
  52. this consistent). If you are modifying someone else's code, try
  53. to keep the coding style similar.
  54. Since we are using 8-space tabs, you might want to consider the Linus
  55. Torvals trick to reduce code nesting. Many times in a loop, you will
  56. find yourself doing a test, and if the test is true, you will nest.
  57. Many times this can be changed. Example:
  58. for (i = 0; i < 10; i++) {
  59. if (something (i)) {
  60. do_more ();
  61. }
  62. }
  63. This take precious space, instead write it like this:
  64. for (i = 0; i < 10; i++) {
  65. if (!something (i))
  66. continue;
  67. do_more ();
  68. }
  69. * Performance and readability
  70. It is more important to be correct than to be fast.
  71. It is more important to be maintainable than to be fast.
  72. Fast code that is difficult to maintain is likely going to
  73. be looked down upon.
  74. * Style Guidelines
  75. * Use a space before an opening parenthesis when calling
  76. functions, or indexing, like this:
  77. method (a);
  78. b [10];
  79. * Do not put a space after the opening parenthesis and the
  80. closing one, ie:
  81. good: method (a); array [10];
  82. bad: method ( a ); array[ 10 ];
  83. * Inside a code block, put the opening brace on the same line
  84. as the statement:
  85. good:
  86. if (a) {
  87. code ();
  88. code ();
  89. }
  90. bad:
  91. if (a)
  92. {
  93. code ();
  94. code ();
  95. }
  96. * Avoid using unecessary open/close braces, vertical space
  97. is usually limited:
  98. good:
  99. if (a)
  100. code ();
  101. bad:
  102. if (a) {
  103. code ();
  104. }
  105. * When defining a method, use the C style for brace placement,
  106. that means, use a new line for the brace, like this:
  107. good:
  108. void Method ()
  109. {
  110. }
  111. bad:
  112. void Method () {
  113. }
  114. * Properties and indexers are an exception, keep the
  115. brace on the same line as the property declaration.
  116. Rationale: this makes it visually
  117. simple to distinguish them.
  118. good:
  119. int Property {
  120. get {
  121. return value;
  122. }
  123. }
  124. bad:
  125. int Property
  126. {
  127. get {
  128. return value;
  129. }
  130. }
  131. Notice how the accessor "get" also keeps its brace on the same
  132. line.
  133. For very small properties, you can compress things:
  134. ok:
  135. int Property {
  136. get { return value; }
  137. set { x = value; }
  138. }
  139. * Use white space in expressions liberally, except in the presence
  140. of parenthesis:
  141. good:
  142. if (a + 5 > method (blah () + 4))
  143. bad:
  144. if (a+5>method(blah()+4))
  145. * For any new files, please use a descriptive introduction, like
  146. this:
  147. //
  148. // System.Comment.cs: Handles comments in System files.
  149. //
  150. // Author:
  151. // Juan Perez ([email protected])
  152. //
  153. // (C) 2002 Address, Inc (http://www.address.com)
  154. //
  155. * If you are modyfing someone else's code, and your contribution
  156. is significant, please add yourself to the Authors list.
  157. * Switch statements have the case at the same indentation as the
  158. switch:
  159. switch (x) {
  160. case 'a':
  161. ...
  162. case 'b':
  163. ...
  164. }
  165. * Argument names should use the camel casing for
  166. identifiers, like this:
  167. good:
  168. void Method (string myArgument)
  169. bad:
  170. void Method (string lpstrArgument)
  171. void Method (string my_string)
  172. * Empty methods: They should have the body of code using two
  173. lines, in consistency with the rest:
  174. good:
  175. void EmptyMethod ()
  176. {
  177. }
  178. bad:
  179. void EmptyMethod () {}
  180. void EmptyMethod ()
  181. {}
  182. * Line length: The line length for C# source code is 134 columns.
  183. If your function declaration arguments go beyond
  184. this point, please align your arguments to match the
  185. opening brace, like this:
  186. void Function (int arg, string argb,
  187. int argc)
  188. {
  189. }
  190. When invoking functions, the rule is different, the
  191. arguments are not aligned with the previous
  192. argument, instead they begin at the tabbed position,
  193. like this:
  194. void M ()
  195. {
  196. MethodCall ("Very long string that will force",
  197. "Next argument on the 8-tab pos",
  198. "Just like this one")
  199. }
  200. * Variable declaration indentation.
  201. Sometimes it is convenient to indent the variables to make the code
  202. look pretier, but do not add gratuitous space, try to use the minimally
  203. necessary space, for example:
  204. Good:
  205. void Method ()
  206. {
  207. string b;
  208. int a;
  209. byte c;
  210. }
  211. Bad:
  212. void Method ()
  213. {
  214. string b;
  215. int a;
  216. byte c;
  217. }
  218. * Braces and the `else' clause
  219. If there are braces closing or opening next to the else clause,
  220. they go on the same line as the word `else', for example:
  221. Good:
  222. if (..) {
  223. } else {
  224. }
  225. Bad:
  226. if (..) {
  227. }
  228. else {
  229. }
  230. Bad:
  231. if (..) {
  232. } else
  233. {
  234. }
  235. Bad:
  236. if (..) {
  237. }
  238. else
  239. {
  240. }
  241. * RCS and CVS tags
  242. Some users like to use the special RCS/CVS tags in their
  243. source code: $id$, $log$ and so on.
  244. The use of these is not permitted on the Mono source code
  245. repository. This metadata belongs on a ChangeLog or in the
  246. SVN metadata.
  247. * File formats
  248. Historically our repository has used a mix of line-endings,
  249. this is a mistake that we are trying hard to fix.
  250. For existing files, please make sure that you do not convert
  251. the file, as that causes us to loose precious history (the
  252. full file is commited).
  253. For new files that you create, please make sure that you use
  254. Subversion's support for mapping the line endings
  255. automatically, after adding your file:
  256. $ svn add file.cs
  257. Execute this command:
  258. $ svn propset svn:eol-style native file.cs
  259. Which will make the file automatically receive the proper
  260. treatment from that point on.
  261. Please verify before commiting that your changes wont loose
  262. history, you can do this by running:
  263. $ svn diff
  264. And examining the output.
  265. * Warnings
  266. Avoid commiting code with warnings to the repository, the use
  267. of #pragmas to disable warnings is strongly discouraged, but
  268. can be used on unique cases. Please justify the use of the
  269. warning ignore clause on a comment.
  270. Do not commit changes to the Makefiles that removes warnings,
  271. if anything warnings should be eliminated one at a time, and
  272. if not possible, they must be flagged.
  273. * Examples:
  274. class X : Y {
  275. bool Method (int argument_1, int argument_2)
  276. {
  277. if (argument_1 == argument_2)
  278. throw new Exception (Locale.GetText ("They are equal!");
  279. if (argument_1 < argument_2) {
  280. if (argument_1 * 3 > 4)
  281. return true;
  282. else
  283. return false;
  284. }
  285. //
  286. // This sample helps keep your sanity while using 8-spaces for tabs
  287. //
  288. VeryLongIdentifierWhichTakesManyArguments (
  289. Argument1, Argument2, Argument3,
  290. NestedCallHere (
  291. MoreNested));
  292. }
  293. bool MyProperty {
  294. get {
  295. return x;
  296. }
  297. set {
  298. x = value;
  299. }
  300. }
  301. void AnotherMethod ()
  302. {
  303. if ((a + 5) != 4) {
  304. }
  305. while (blah) {
  306. if (a)
  307. continue;
  308. b++;
  309. }
  310. }
  311. }
  312. * Conditional compilation
  313. Ideally we would not need conditional compilation, and the use
  314. of #ifdef is strongly discouraged. But due to our support for
  315. old C# 1.0 compilers we have to use it in a few places.
  316. Try to avoid negative tests that have an else clause, for
  317. example:
  318. #if !NET_2_0
  319. CODE_FOR_1_0
  320. #else
  321. CODE_FOR_2_0
  322. #endif
  323. Instead use:
  324. #if NET_2_0
  325. CODE_FOR_2_0
  326. #else
  327. CODE_FOR_1_0
  328. #endif
  329. When a major feature differs across compilation targets, try
  330. to factor out the code into a separate class, a helper class
  331. or a separate file, and include that in your profile while
  332. surrounding that helper file/class with the ifdefs to reduce
  333. the amount of ifdefs in the code.
  334. For instance, this is used for some parts of Grasshopper where
  335. the code is ifdefed out, when large parts of a file would have
  336. been ifdefed out, we moved the code into a MyOtherFile.jvm.cs
  337. For 2.0 classes, this is even simpler as code can be trivially
  338. factored out into
  339. MyHelperClass.cli.cs
  340. MyHelperClass.jvm.cs
  341. By using partial classes.