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