CodingStyle 12 KB

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