Good coding habits
naming folders and files
Stanford file naming best practices
- Include information to distinguish file name e.g. project name, objective of function, name/initials, type of data, conditions, version of file,
- if using dates, use YYYYMMDD, so the computer organizes by year, then month, then day
- avoid special characters e.g. !, #, $, ...
- avoid using spaces if not necessary, some programs consider a space as a break in code use dashes
-
or underscores_
or CamelCase
Commenting your code
Its important to comment your code to mention what a variable's units are, what the function is supposed to do, etc.
function i=code(j)
% Example of bad variable names and bad function name
for w=1:j
i(w)=w;
end
end
help code
'code' is a command-line function
Example of bad variable names and bad function name
Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
'doc <topic>' to search the manual index.
Help and information about Octave is also available on the WWW
at http://www.octave.org and via the help@octave.org
mailing list.
Choose variable names that describe the variable
function count_vector=counting_function(max_value)
% Good variable names and better help documentation
%
% counting function creates a vector from 1 to max_value where each index, i, is
% stored in each vector spot
for i=1:max_value
count_vector(i)=i; % set each element in count_vector to i
end
end
help counting_function
'counting_function' is a command-line function
Good variable names and better help documentation
counting function creates a vector from 1 to max_value where each index, i, is
stored in each vector spot
Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
'doc <topic>' to search the manual index.
Help and information about Octave is also available on the WWW
at http://www.octave.org and via the help@octave.org
mailing list.
Putting it all together
- Clone your homework_1 to your computer
- open Matlab (cli, jupyter or gui)
- Change working directory to homework_1 e.g. Windows:
cd('C:\Users\rcc02007\Documents\Github\homework_1')
, Mac:cd('/Users/rcc02007/Documents/Github/homework_1')
- You have already created your first script
myscript.m
(if not see lecture_4) - Run
>> my_script.m
- Create a new m-file called nitrogen_pressure.m
- Create a function based upon the ideal gas law for nitrogen, Pv=RT
- R=0.2968 kJ/(kg-K)
- inputs to function are v (specific volume m^3/kg), and T, temperature (K)
- output is P, pressure (kPa)
- Once the function works, commit the change to the repository (add a message, like 'added file nitrogen_pressure.m'
- After file is 'committed', 'push' the changes to your github account
for the command-line git user, this is steps 8 and 9:
$ git add *
$ git commit -m 'added file nitrogen_pressure.m'
$ git push -u origin master Username for 'https://github.uconn.edu':rcc02007 <enter>