Want to try fastn for your company's website?
Book a Demo

How to use Conditions

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
For the two mutually exclusive statements, only one condition is required. For the other statement, use default value.
Not recommended
-- ftd.text: Hello
color if { flag }: red
color if { !flag }: green
Lang:
ftd
Recommended
-- ftd.text: Hello
color if { flag }: red
color: green
Lang:
ftd
avoid-redundant-conditions: Avoid redundancy with Conditions

Avoid Unnecessary Conditional Statements for always true or always false statements.

A programming best practice where unnecessary conditional statements for expressions that are always true or always false are avoided as it only results in redundant code.
Not recommended
-- integer num: 1

-- ftd.integer: $num
if: { num == 1 }

-- ftd.text: World
if: { num == 2 }
Lang:
ftd
Recommended
-- integer num: 1

-- ftd.integer: $num
Lang:
ftd
In the above case, the variable 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.

Conditions with respect to element and it's children

different-conditions-for-element-children: Avoiding same conditions on element and it's children

It 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.

Instead, the recommended approach is to include the condition only on the element and then include any necessary child within that element.
Not recommended
-- ftd.column:
if: { flag }

-- ftd.text: Hello
if: { flag }

-- ftd.text: World
color if { flag }: green

-- end: ftd.column
Lang:
ftd
Recommended
-- ftd.column:
if: { flag }

-- ftd.text: Hello

-- ftd.text: World
color: green

-- end: ftd.column
Lang:
ftd
mutually-exclusive-conditions: Avoiding mutually exclusive conditions on element and it's children

To 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.
Not recommended
-- ftd.column:
if: { flag }

-- ftd.text: Hello
if: { !flag }

-- ftd.text: World
color if { !flag }: green

-- end: ftd.column
Lang:
ftd
Recommended
-- ftd.column:
if: { flag }

-- ftd.text: World

-- end: ftd.column
Lang:
ftd