Docsity

Matlab Style Guidelines for ME 2016: Writing Clear and Readable Code, Assignments of Computer Science

Toggle sidebar
bg1
Matlab Style Guidelines for ME 2016
The list below is just a guide-line. In general, following these rules will result in more
readable code.
1. Headers:
• Use the templates provided on the web:
http://www.srl.gatech.edu/education/ME2016/Homework/function_template.m
http://www.srl.gatech.edu/education/ME2016/Homework/script_template.m
2. Comments:
• Include enough comments so that somebody unfamiliar with your program can
understand it. If you use descriptive variable names, you may not need a lot of
comments.
• Include the comments BEFORE the code that is being explained. The only
exception is very short explanation, such as listing the units of a variable, that can
be included at the end of the line to which they apply.
3. Variable names:
• Use descriptive variable names so that it is immediately clear which quantity the
variable represents.
• Make sure the meaning of a variable remains the same inside the scope of a
function or script. For instance, use torque_Nm and torque_ftlbf to indicate the
same torque in different units rather than just one variable torque for both.
• Use a consistent naming scheme. For instance, either of the following are good:
weekly_pay = hours_worked * pay_rate;
weeklyPay = hoursWorked * payRate;
• We typically do not start variable names with a capital unless it really improves
the readability of the program. For instance, to distinguish between the absolute
and relative error in an equation, on may use Et and et.
4. Indentation
• Make your code more readable by indenting to the same number for spaces
commands that are at the same logical level – for instance, all the commands
inside a for loop should be indented relative to the “for” line and the “end” line of
the for loop. Indentation should be used for all iteration (loops) and conditional
(if-then-else) statements.
• The Matlab editor automatically indents commands – take advantage of this
feature
5. Error checking
• At the beginning of a function you should check for errors in the inputs provided
by the user.
• Use the error( ) function if your function is going to provide a completely
meaningless result or may not complete because of an invalid operation (e.g.
pf2

Partial preview of the text

Download Matlab Style Guidelines for ME 2016: Writing Clear and Readable Code and more Assignments Computer Science in PDF only on Docsity!

Matlab Style Guidelines for ME 2016

The list below is just a guide-line. In general, following these rules will result in more readable code.

  1. Headers:
    • Use the templates provided on the web: http://www.srl.gatech.edu/education/ME2016/Homework/function_template.m http://www.srl.gatech.edu/education/ME2016/Homework/script_template.m
  2. Comments:
    • Include enough comments so that somebody unfamiliar with your program can understand it. If you use descriptive variable names, you may not need a lot of comments.
    • Include the comments BEFORE the code that is being explained. The only exception is very short explanation, such as listing the units of a variable, that can be included at the end of the line to which they apply.
  3. Variable names:
    • Use descriptive variable names so that it is immediately clear which quantity the variable represents.
    • Make sure the meaning of a variable remains the same inside the scope of a function or script. For instance, use torque_Nm and torque_ftlbf to indicate the same torque in different units rather than just one variable torque for both.
    • Use a consistent naming scheme. For instance, either of the following are good: weekly_pay = hours_worked * pay_rate; weeklyPay = hoursWorked * payRate;
    • We typically do not start variable names with a capital unless it really improves the readability of the program. For instance, to distinguish between the absolute and relative error in an equation, on may use Et and et.
  4. Indentation
    • Make your code more readable by indenting to the same number for spaces commands that are at the same logical level – for instance, all the commands inside a for loop should be indented relative to the “for” line and the “end” line of the for loop. Indentation should be used for all iteration (loops) and conditional (if-then-else) statements.
    • The Matlab editor automatically indents commands – take advantage of this feature
  5. Error checking
    • At the beginning of a function you should check for errors in the inputs provided by the user.
    • Use the error( ) function if your function is going to provide a completely meaningless result or may not complete because of an invalid operation (e.g.

division by zero). The function should stop execution of the program in this case

  • otherwise the user may interpret the result as meaningful when it is clearly not.
  • Use the warning( ) function if your function is going to provide a suspicious result (may or may not be meaningful). The warning message should be sufficiently descriptive so that
  1. For and while loops
  • Use a for loop when you know in advance how many times you need to iterate.
  1. Avoid copying and pasting
  • Copying the same line of code multiple times and then changing it slightly is typically a bad idea. It very difficult to see what has been changed and what has remained the same. Often one can use a for-loop to achieve the same functionality without repeating the line.