A good habit of name conventions is important for the code readability. It is especially important when you share the code among a group of people. In this blog, I will conclude some naming conventions for MATLAB, which are also applicable for other programming languages like Python.
Naming conventions are not that strict but there are some common rules which you should follow. And those are the things listed here.
On of the most important thing is to set a unit suffix for your dimensional variables, like attackAngleDEG, or shipSpeedMS. Normally, I would use all capital characters for the units.
- Variable names should be in the form of the mixed case and start with a lower case, like shipSpeed, channelWidth, etc.
- The prefix n can be used for variables representing the number of objects, like nFiles.
- The suffix No can be used as a single entity number, like shipNo, studentNo.
- Iterator variables should be more meaningful than i, j, k. The iterated object can be prefixed with i, j, k, like iFolder, jFile, kList.
- Acronyms should be consistently mixed even if they are normally uppercase, like hTML.
- Constants should be kept in uppercase and separated with underscores. They can be prefixed by a common type name, like SHIP_LENGTH.
- Structures should follow the naming conventions of variables except that the first letter should be in upper case, like Ship.rudderArea.
- Function names should be meaningful and written in lower case. I do prefer to separate the words with under scores. Other people may suggest just use pure lower cases. It depends on you. What I use is like compute_resistance().
- Prefixes get/set/compute/find/initialize/is can be applied for various purposes, like get/set the object properties, compute results, or find a property.
- Keep a maximum line width of 80 columns.
- Break the line after a comma, a whitespace, an operator.
- Surround logical operators with spaces.* Conventional operators can/cannot surrounded by spaces. I do prefer without spaces.
- Commas can be followed by a whitespace. It is required in the Python PEP8 format.
That is it.