Variables#
Follow Our Naming Conventions#
See Naming Conventions > Naming Convention Usage.
Follow Clean Code’s Name Rules#
- Choose descriptive and unambiguous names.
- Make meaningful distinctions.
- Use pronounceable names.
- Use searchable names.
- Replace magic numbers with named constants.
- Avoid encodings. Don’t append prefixes or type information.
Use Lowest Scope Possible#
Review the following sections:
Don’t Add Unneeded Context To The Structures#
Incorrect
<mvt:assign name="l.settings:customer:customer_name" value="'John Doe'" />
<mvt:assign name="l.settings:customer:customer_address" value="'16870 W Bernardo Dr Ste 100, San Diego, CA 92127'" />
<mvt:assign name="l.settings:customer:customer_email" value="'mivamerchant@miva.com'" />
Correct
<mvt:assign name="l.settings:customer:name" value="'John Doe'" />
<mvt:assign name="l.settings:customer:address" value="'16870 W Bernardo Dr Ste 100, San Diego, CA 92127'" />
<mvt:assign name="l.settings:customer:email" value="'mivamerchant@miva.com'" />
Use Proper Default Value Data Types#
When creating variables, your default value should be the same data type that you will be using in your logic.
Allows the developer to understand what type this variable is and prevents bugs.
Incorrect
<mvt:foreach iterator="group" array="basket:groups">
<mvt:assign name="l.total_quantity" value="l.total_quantity + l.settings:group:quantity" />
</mvt:foreach>
Correct
<mvt:assign name="l.total_quantity" value="0" />
<mvt:foreach iterator="group" array="basket:groups">
<mvt:assign name="l.total_quantity" value="l.total_quantity + l.settings:group:quantity" />
</mvt:foreach>
Assign Complex Logic Expressions To A Descriptive Variable#
Create more readable code by assigning complex logical expressions to a descriptive variable name and then use the variable in the conditionals.
Incorrect
<mvt:if expr="l.settings:page:code EQ 'LOGN' OR l.settings:page:code EQ 'ORDL' OR l.settings:page:code EQ 'GFTL'">
<!-- Do something for these pages -->
</mvt:if>
Correct
<mvt:assign name="l.is_login_page" value="l.settings:page:code EQ 'LOGN' OR l.settings:page:code EQ 'ORDL' OR l.settings:page:code EQ 'GFTL'">
<mvt:if expr="l.is_login_page">
<!-- Do something for the login page -->
</mvt:if>
Constants#
- See Naming Conventions > Constant Case
- Reserve for values that are static (i.e. that will never change), “Magic Numbers”, and high-level configurations. For example:
MINUTES_IN_A_DAY = 60 * 24
.- Good examples can be date, time, file path, file name, etc.
Incorrect
<mvt:assign name="l.export_file_path" value="'/path/to/export/file.txt'">
<!-- lots of other code here... -->
<mvt:assign name="s.null" value="file_overwrite( l.export_file_path, 'data', l.file_data )">
Correct
<mvt:assign name="l.EXPORT_FILE_PATH" value="'/path/to/export/file.txt'">
<!-- lots of other code here... -->
<mvt:assign name="s.null" value="file_overwrite( l.EXPORT_FILE_PATH, 'data', l.file_data )">