Guest User

Untitled

a guest
Jul 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. # matlab/octave and C++
  2.  
  3. (1) use parentheses around “if” conditions
  4.  
  5. ```if (x = y && (x + y > 3))``` - good
  6. ```if x = y && (x + y > 3)``` - not good
  7.  
  8. (2) initialize variables and don’t assume zeros
  9.  
  10. good:
  11. ```
  12. x = 0;
  13. if (a > 0)
  14. x = 1;
  15. end
  16. % at this point x is either 0 or 1
  17. ```
  18. not good:
  19. ```
  20. if (a > 0)
  21. x = 1;
  22. end
  23. % at this point x is either 0 or 1
  24. ```
  25. (3) matlab variables don’t have scope, create artificial scope for them and don’t reuse them
  26.  
  27. good:
  28. ```
  29. % this is not needed, but indicate the “scope” of “some_variable”
  30. some_variable = 0
  31. for i = 1 : 100
  32. if (x > 0)
  33. some_variable = 10;
  34. else
  35. some_variable = 0;
  36. end %if
  37. end %for sample
  38. for i = 1 : 100
  39. if (some_variable == 10)
  40. end %if
  41. end %for i
  42. ```
  43. not good:
  44. ```
  45. some_variable = 0
  46. for i = 1 : 100
  47. if (x > 0)
  48. some_variable = 10;
  49. else
  50. some_variable = 0;
  51. end %if
  52. end %for sample
  53. for i = 1 : 100
  54. if (some_variable == 10)
  55. end %if
  56. end %for i
  57. ```
  58.  
  59. (4) don’t create arrays just to perform matrix operations on them (although this is the preferred way of doing that in matlab):
  60.  
  61. good:
  62. ```
  63. for sample = 1 : 100
  64. sum_x = sum_x + rand();
  65. end
  66. avg_x = sum_x/100;
  67. ```
  68. not good:
  69. ```
  70. for sample = 1 : 100
  71. x(sample) = rand();
  72. end
  73. avg_x = mean(x);
  74. ```
  75.  
  76. (5) use assert when things must not happen (this finds lots of programming bugs and help understand the software). Also, try to make sure there is always an “else” section to an if..elseif statement:
  77.  
  78. ```
  79. % f() should not return negatives
  80. x = f();
  81.  
  82. if (x == 0)
  83. do_something
  84. elseif (x > 0)
  85. do_something_else
  86. else
  87. assert(0);
  88. end
  89. ```
  90.  
  91. (6) don’t optimize using copy&paste. use functions instead
  92.  
  93. good:
  94. ```
  95. function f(is_x_positive)
  96. % 100 lines of code + some logic on is_x_zero
  97. end
  98.  
  99. if (x == 0)
  100. f(1);
  101. else
  102. f(0);
  103. end % if
  104. ```
  105. not good:
  106. ```if (x == 0)
  107. % 100 lines of code
  108. else
  109. % very similar 100 lines of code
  110. end % if
  111. ```
  112.  
  113. (7) don’t use very long lines, use “...” instead (works for both matlab and octave). Putting comments is different lines than code also help with that
  114.  
  115. (8) make sure indentation is correct, not too much, not too little
  116.  
  117. good:
  118. ```
  119. if (x == 0)
  120. do_something
  121. ```
  122. not good:
  123. ```
  124. if (x == 0)
  125. Do_something
  126. ```
  127.  
  128. (9) (you already do this) indicate which statement the “end” ends:
  129.  
  130. ```
  131. for sample = 1 : 100
  132. if (x > 0)
  133. end %if
  134. end %for sample
  135. ```
  136.  
  137. (10) extra parentheses around “^” (power) operator
  138.  
  139. good:
  140. ```
  141. x = x + (y^(z+1));
  142. ```
  143. not good:
  144. ```
  145. x = x + y^(z+1);
  146. ```
  147.  
  148. (10) named indexes. use “constants” instead of numbers:
  149.  
  150. good:
  151. ```
  152. MARRIED = 3;
  153. ...
  154. prob_of_emp = women(idx, MARRIED);
  155. ```
  156.  
  157. not good:
  158. ```
  159. prob_of_emp = women(idx, 3);
  160. ```
  161.  
  162. (11) C/matlab style arrays: matlab start from one, C start from zero. try to be consistent with these. when not possible, use another variables and comments. for example:
  163.  
  164. good:
  165. ```
  166. for interval = 1 : 100
  167. % the first interval start at t=0
  168. t = interval - 1;
  169. x = f(t);
  170. y(interval) = x;
  171. end
  172. ```
  173.  
  174. not good:
  175. ```
  176. for t = 0 : 100
  177. x = f(t);
  178. y(t+1) = x;
  179. end
  180. ```
  181.  
  182. (12) variable names and “constants”: matlab does not have “constants”, so try to use: UPPER_CASE for constants and lower_case for non-constants
Add Comment
Please, Sign In to add comment