The names of functions should document their use.
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.
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.
This is common practice in MathWorks code.
mean(...); standarderror(...)
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(...)
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(...)
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(...)
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 American initialize should be preferred over the British initialise. Abbreviation init should be avoided.
initializeproblemstate(...)
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(...);
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.
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
.