By following the best practices for writing conditional statements, developers can create code that is less error-prone and more efficient, making it easier for other developers to work with the code and reducing the likelihood of introducing bugs.
Following are the best-practices on how to use conditions:default-for-mutually-exclusive
: Default Values for Mutually Exclusive Statements-- ftd.text: Hello
color if { flag }: red
color if { !flag }: green
-- ftd.text: Hello
color if { flag }: red
color: green
avoid-redundant-conditions
: Avoid redundancy with ConditionsAvoid Unnecessary Conditional Statements for always true
or always false
statements.
-- integer num: 1
-- ftd.integer: $num
if: { num == 1 }
-- ftd.text: World
if: { num == 2 }
-- integer num: 1
-- ftd.integer: $num
num
is immutable i.e. the value of num is
fixed to 1, therefore, if: { num == 1 }
is always true
and if: { num == 2 }
is always false
.different-conditions-for-element-children
: Avoiding same conditions on element and it's childrenIt is not recommended to create same conditions on element and it's children.
This approach adds an unnecessary line of code and can make the ftd
code more
difficult to read and maintain.
-- ftd.column:
if: { flag }
-- ftd.text: Hello
if: { flag }
-- ftd.text: World
color if { flag }: green
-- end: ftd.column
-- ftd.column:
if: { flag }
-- ftd.text: Hello
-- ftd.text: World
color: green
-- end: ftd.column
mutually-exclusive-conditions
: Avoiding mutually exclusive conditions on element and it's childrenTo simplify the code and reduce the risk of errors, it is unnecessary to add mutually exclusive conditions to the children and their attributes in relation to the element. These conditions will never be true and only add complexity to the code.
Instead, it is recommended to apply conditions only to the element itself, and omit applying conditions to its children. This approach makes the code easier to read and understand.-- ftd.column:
if: { flag }
-- ftd.text: Hello
if: { !flag }
-- ftd.text: World
color if { !flag }: green
-- end: ftd.column
-- ftd.column:
if: { flag }
-- ftd.text: World
-- end: ftd.column