Coding Style.rtf 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. {\rtf1\ansi\ansicpg1252\cocoartf1187\cocoasubrtf390
  2. \cocoascreenfonts1{\fonttbl\f0\fswiss\fcharset0 Helvetica;\f1\fnil\fcharset0 Monaco;}
  3. {\colortbl;\red255\green255\blue255;\red0\green134\blue2;\red195\green0\blue0;}
  4. \paperw12240\paperh15840\vieww18380\viewh12620\viewkind0
  5. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\qc
  6. \f0\fs36 \cf0 Coding Rules\
  7. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
  8. \fs24 \cf0 \
  9. \fs28 Cocoa coding style for the naming.\
  10. \
  11. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\sl480\slmult1\pardirnatural
  12. \f1\fs20 \cf0 variableNaming
  13. \f0\fs28 variable should use capitalization on all word expect the first and never '_' ie\
  14. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\fi540\pardirnatural
  15. \b\fs24 \cf2 RIGHT\
  16. \f1\b0\fs18 \cf0 BOOL isFirstTimeReading;\
  17. int age;\
  18. \f0\fs24 \
  19. \b \cf3 WRONG\
  20. \f1\b0\fs18 \cf0 BOOL is_first_time_reading;\
  21. int Age;\
  22. int isFirstTimeReading_ever;\
  23. \
  24. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\sl480\slmult1\pardirnatural
  25. \f0\fs28 \cf0 Objective-C Instance variable must be prefixed with _\
  26. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\fi540\pardirnatural
  27. \b\fs24 \cf2 RIGHT\
  28. \f1\b0\fs18 \cf0 @interface Object \{\
  29. BOOL _isFirstTimeReading;\
  30. int _age;\
  31. \}\
  32. \f0\fs24 \
  33. \b \cf3 WRONG\
  34. \f1\b0\fs18 \cf0 @interface Object \{\
  35. BOOL isFirstTimeReading;\
  36. int age;\
  37. \}
  38. \f0\fs28 \
  39. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
  40. \fs24 \cf0 \
  41. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\sl480\slmult1\pardirnatural
  42. \f1\fs20 \cf0 Pointer *
  43. \f0\fs28 must be preceded with a space and with no space after ie\
  44. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\fi540\pardirnatural
  45. \b\fs24 \cf2 RIGHT\
  46. \f1\b0\fs18 \cf0 void *pointer;\
  47. \f0\fs24 \
  48. \b \cf3 WRONG\
  49. \f1\b0\fs18 \cf0 void * pointer;\
  50. void* pointer;\
  51. void*pointer;\
  52. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
  53. \f0\fs24 \cf0 \
  54. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\sl480\slmult1\pardirnatural
  55. \f1\fs20 \cf0 if
  56. \f0\fs28 ,
  57. \f1\fs18 switch
  58. \f0\fs28 , and other keyword that are not function but takes parameter should have a space before ()\
  59. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\fi540\pardirnatural
  60. \b\fs24 \cf2 RIGHT\
  61. \f1\b0\fs18 \cf0 if (a) \{\
  62. ...\
  63. \}\
  64. function();\
  65. \f0\fs24 \
  66. \b \cf3 WRONG\
  67. \f1\b0\fs18 \cf0 if( a )\
  68. if ( a )\
  69. if(a)\
  70. function ();\
  71. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
  72. \f0\fs24 \cf0 \
  73. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\sl480\slmult1\pardirnatural
  74. \f1\fs18 \cf0 \{\}
  75. \f0\fs28 usage\
  76. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\li540\pardirnatural
  77. \b\fs24 \cf2 RIGHT\
  78. \f1\b0\fs18 \cf0 if (a) \{\
  79. ...\
  80. \}\
  81. while (a) \{\
  82. ...\
  83. \}\
  84. void function()\
  85. \{\
  86. ...\
  87. \}\
  88. - (void)functionWithParameter:(BOOL)parameter\
  89. \{\
  90. ...\
  91. \}\
  92. \f0\b\fs24 \cf3 WRONG
  93. \b0 \cf0 \
  94. \f1\fs18 - (void)function \{\
  95. ...\
  96. \}\
  97. if (a)\
  98. \{\
  99. ...\
  100. \}\
  101. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
  102. \f0\fs24 \cf0 \
  103. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\sl480\slmult1\pardirnatural
  104. \fs28 \cf0 Prefer early
  105. \f1\fs18 return
  106. \f0\fs28 ie:\
  107. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\fi540\pardirnatural
  108. \b\fs24 \cf2 PREFER\
  109. \f1\b0\fs18 \cf0 if (!a)\
  110. return;\
  111. \f0\fs24 \
  112. \b \cf3 OVER\
  113. \f1\b0\fs18 \cf0 if (a) \{\
  114. ...\
  115. ...\
  116. \}\
  117. \f0\fs24 \
  118. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\sl480\slmult1\pardirnatural
  119. \fs28 \cf0 Objective C code - Don't call multiply the same method\
  120. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\li540\pardirnatural
  121. \b\fs24 \cf2 RIGHT\
  122. \f1\b0\fs18 \cf0 NSWindow *window = [self window];\
  123. NSRect frame = [window frame];\
  124. frame.origin.x = 0;\
  125. [window setFrame:frame display:NO];\
  126. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\fi540\pardirnatural
  127. \f0\b\fs24 \cf3 WRONG\
  128. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\li540\pardirnatural
  129. \f1\b0\fs18 \cf0 NSRect frame = [[self window] frame];\
  130. frame.origin.x = 0;\
  131. [[self window] setFrame:frame display:NO];\
  132. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\sl480\slmult1\pardirnatural
  133. \f0\fs28 \cf0 objectAtIndex is gone - keep it like this\
  134. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\li540\pardirnatural
  135. \b\fs24 \cf2 RIGHT\
  136. \f1\b0\fs18 \cf0 NSArray *array;\
  137. \'85filled with lots of stuff\
  138. id object = array[index];\
  139. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\fi540\pardirnatural
  140. \f0\b\fs24 \cf3 WRONG\
  141. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\li540\pardirnatural
  142. \f1\b0\fs18 \cf0 NSArray *array;\
  143. \'85filled with lots of stuff\
  144. id object = [array objectAtIndex:index];\
  145. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\sl480\slmult1\pardirnatural
  146. \f0\fs28 \cf0 NSArray literals improve readability - use them\
  147. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\li540\pardirnatural
  148. \b\fs24 \cf2 RIGHT\
  149. \f1\b0\fs18 \cf0 NSArray *array = @[obj1, obj2, obj3];\
  150. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\fi540\pardirnatural
  151. \f0\b\fs24 \cf3 WRONG\
  152. \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\li540\pardirnatural
  153. \f1\b0\fs18 \cf0 NSArray *array = [NSArray arrayWithObjects: obj1, obj2, obj3, nil];\
  154. }