CodingStyle 10 KB

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