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

Support `fastn`!

Enjoying `fastn`? Please consider giving us a star ⭐️ on [GitHub](https://github.com/fastn-stack/fastn) to show your support!
[⭐️](https://github.com/fastn-stack/fastn)

Getting Help

Have a question or need help? Visit our [GitHub Q&A discussion](https://github.com/fastn-stack/fastn/discussions/categories/q-a) to get answers and subscribe to it to stay tuned. Join our [Discord](https://discord.gg/a7eBUeutWD) channel and share your thoughts, suggestion, question etc. Connect with our [community](/community/)!
[💻️](/community/)

Found an issue?

If you find some issue, please visit our [GitHub issues](https://github.com/fastn-stack/fastn/issues) to tell us about it.

Quick links:

- [Install `fastn`](install/) - [Create `fastn` package](create-fastn-package/) - [Expander Crash Course](expander/) - [Syntax Highlighting in Sublime Text](/sublime/)

Join us

We welcome you to join our [Discord](https://discord.gg/a7eBUeutWD) community today. We are trying to create the language for human beings and we do not believe it would be possible without your support. We would love to hear from you.
Copyright © 2023 - [FifthTry.com](https://www.fifthtry.com/)