
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.