Coding Standard
Coding Standard
Coding Standard
One of the most important features of any technical communication is that it is accessible clear
and easy for others to read and understand. MATLAB codes are a special form of technical
communication and to help make it clear for others, we will implement a coding standard this
semester. There are many important reasons to follow this coding standard. If your codes are
consistent and easy to follow, then:
Debugging your own code should be easier,
Getting help from TAs and/or faculty should be easier,
Publishing your code in MATLAB produces a professional-looking document, and
Grading your codes should be easier, meaning quicker feedback on your work!
But if thats not enough incentive for you, you will not earn full credit on MATLAB
assignments if you do not follow the coding standard. There are five important features all
submitted code should have.
1) Codes should include a properly formatted header that includes your name, the name of
the script or function, and a brief description of what the code does (and in the case of a
function, this needs to be long enough to clearly describe all inputs and outputs for the
function).
2) Codes should be formatted into major blocks using cell structure. (Use a double
percentage sign and a space to start a new cell.) For complicated codes, example blocks
include defining constants, initializing arrays, computing results, or displaying results.
3) Variables should have meaningful names.
4) Codes should be indented properly (and make use of ellipses as necessary) so that the
structure of the code is easy to see. Quick tip: Before publishing/submitting any MATLAB
code, hit Ctrl+A to select all, then Ctrl+I to auto-indent your code.
5) Codes should include comments as appropriate to make the program clear. Some
examples:
a. Describe what each variable represents when its first used (unless its obvious),
b. Note the units on any physical properties,
c. Explain a complicated computational procedure, function call, or conditional/loop
structure.
MATLAB codes that do not adhere to features 1 and 2 will only earn up to 50% of the possible
points for the submitted code. Codes that do not adhere to features 3, 4, and 5 will only earn up
to 90% of the possible points. These percent multipliers are cumulative for any given assignment
(so code that completely violates the standard is theoretically worth 50% 50% 90% 90%
90% = 18.2% of the possible points, rounded to the nearest integer).
Sample code on next page.
area=NaN;
areaExists=true; %if this is false, code will skip computation
sides=zeros(3,1); %sides will hold s1, s2, and s3 after they're sorted
s=0; %semiperimeter of triangle, computed below
%% Error-proof Inputs
% Check to make sure s1, s2, and s3 are all scalar values
if ~(isscalar(s1) && isscalar(s2) && isscalar(s3))
areaExists=false;
end
% Check to make sure the two shorter sides sum to be longer than the
% longest side - otherwise there's no such triangle!
sides=sort([s1,s2,s3]); %sorts low to high
if sides(1) + sides(2) <= sides(3)
areaExists=false;
end
%% Perform Computation, if triangle exists
if areaExists
s=(s1+s2+s3)/2;
area=sqrt(s*(s-s1)*(s-s2)*(s-s3)); % Heron's formula
end
%% Display Error Message, as necessary
% This error message could be done differently so there is a unique message
% for each error, but that's not the case here.
if ~areaExists
disp('invalid entry - area could not be computed')
end
end