Functions
The names of functions should document their use.
Names of functions should be written in lower case
It is clearest to have the function and its m-file names the same. Using lower case avoids potential filename problems in mixed operating system environments.
getname(.), computetotalwidth(.)
There are two other function name conventions commonly used. Some people prefer to use underscores in function names to enhance readability. Others use the naming convention proposed here for variables.
Functions should have meaningful names
There is an unfortunate MATLAB tradition of using short and often somewhat cryptic function names, probably due to the DOS 8 character limit. This concern is no longer relevant and the
tradition should usually be avoided to improve readability.
Use computetotalwidth(...)
Avoid compwid(...)
An exception is the use of abbreviations or acronyms widely used in mathematics.
max(...); gcd(...)
Functions with such short names should always have the complete words in the first header comment line for clarity and to support lookfor
searches.
Functions with a single output can be named for the output
This is common practice in MathWorks code.
mean(...); standarderror(...)
Functions with no output argument or which only return a handle should be named after what they do
This practice increases readability, making it clear what the function should (and possibly should not) do. This makes it easier to keep the code clean of unintended side effects.
plot(...)
The prefixes get/set should generally be reserved for accessing an object or property
General practice of MathWorks and common practice in C++ and Java development. A plausible exception is the use of set for logical set operations.
getobj(...); setappdata(...)
The prefix compute can be used in methods where something is computed
Consistent use of the term enhances readability. Give the reader the immediate clue that this is a potentially complex or time consuming operation.
computweightedaverage(...); computespread(...)
The prefix find can be used in methods where something is looked up
Give the reader the immediate clue that this is a simple look up method with a minimum of computations involved. Consistent use of the term enhances readability and it is a good substitute for get.
findoldestrecord(...); findheaviestelement(...);
The prefix initialize can be used where an object or a concept is established
The American initialize should be preferred over the British initialise. Abbreviation init should be avoided.
initializeproblemstate(...)
The prefix is should be used for Boolean functions
Common practice in MathWorks code as well as C++ and Java.
isoverpriced(...); iscomplete(...)
There are a few alternatives to the is prefix that fit better in some situations. These include the has
, can
and should
prefixes:
hasLicense(...); canEvaluate(...); shouldSort(...);
Complement names should be used for complement operations
Reduce complexity by symmetry.
get/set, add/remove, create/destroy, start/stop, insert/delete, increment/decrement, old/new, begin/end, first/last, up/down, min/max, next/previous, old/new, open/close, show/hide, suspend/resume, etc.
Avoid unintentional shadowing
In general function names should be unique. Shadowing (having two or more functions with the same name) increases the possibility of unexpected behavior or error. Names can be checked for shadowing using which -all
or exist
.