Loading...

PowerFx Formatting If Statement Idiosyncrasies

PowerFx Formatting If Statement Idiosyncrasies

Disclaimer: This entire blog is an opinion, and opinions are like butts, we all have one.  This is not doctrinal truth that must be observed or else you “are dead to me”.  It is just my opinion which I offer with my reasoning in the hopes that it will convince others to make their low-code developer lives and the lives of those that come after them, better. (If you do disagree with, I would love to hear your counter arguments in the comments)

Auto Formatting If Statements Sucks Has Room For Improvement

This is how a simple if statement is auto-formatted within the Canvas Apps:



This is better than a single line, but still sucks has room for improvement.

  1. "If(" does not require it’s own line to distinguish itself from the surrounding code.  The indentation already servers that purpose and vertical spacing in large PowerFx functions is at a premium (but yet it’s premium-ness shouldn’t override readability, more on that later).
  2. The parts of the if are not clearly distinguished as it contains a single uninterrupted block of text .  This is an extremely simple if statement, but it takes a lot of attention to visual detail (aka brain power/cognitive load) to separate the condition portion, the true-statement, and the false/else-statement.  This leads to bugs and increases the time required to understand what the code is, what it is doing, and what changes may need to be made.

By including the condition on the same line as the if, and most importantly SEPERATING ELSE/ELSE-IF’S WITH AN UNINDENTED COMMA, each part of the if statement becomes clearly defined, and it takes up no less vertical space than before:



This becomes even more important on longer if statements, or if statements with an else/if:



Although vertical spacing is at a premium in PowerFx (all programing languages really), it shouldn’t override readability.  For longer more complicated if statements, readability should be win out, and the parts of the condition can be split among multiple lines, but double indented to clearly define where the true-statement starts:



The choosing of when to split up the if-condition among multiple lines vs keeping it on a single line is much more personal preference than anything else, and is one that may personally fluctuate from time to time (or at least it does for me).

One final thing to note, eliminating an if statement usually results in cleaner code.  In cases where there is just an if statement used to set a Boolean value, removing it is simple and should be done in almost all situations, just like this one:



Happy coding formatting!

Published on:

Learn more
.Net Dust
.Net Dust

NULL

Share post:

Related posts

Power Automate – Power Fx integration with Power Automate for desktop

Get ready to experience the magic of low-code language with Power Automate for desktop! The exciting news is the integration of Power Fx scrip...

9 days ago

Generating Power Fx Formulas with Multi-Language Comment Support in Power Apps Using Copilot

The Comment-Generated Formulas feature in Power Apps lets you create Power Fx formulas directly from code comments. By typing `//` or ‘/*’ fol...

15 days ago

Executing SQL Server stored procedures with Power FX in Power Apps

A stored procedure in SQL is a pre-defined collection of SQL commands stored within the database, optimized to enhance execution efficiency an...

21 days ago

Power Automate: Instant Low-Code Plugins To Run PowerFx Code

Power Automate can run PowerFx code by calling an instant low-code plugin. An instant low-code ... The post Power Automate: Instant Low-Code P...

1 month ago

Show/Hide Buttons Based on Entity Permissions Using Power FX

Recently, we received a client requirement to show a button to users who have the Create permission for a specific entity. In the traditional ...

1 month ago

[Beginner’s Guide] PowerFx | Lookup Function with ‘ReductionFormula’ Property

Did you know that when you use the Lookup function, it retrieves the matching record along with All Columns? In the screenshot below, I used t...

2 months ago
Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy