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.
- "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).
- 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 moreWe can help you with PowerFx Formatting If Statement Idiosyncrasies
If you want help implementing, troubleshooting, or improving this product, contact us and we’ll point you in the right direction.
Related posts
Separating Plugin Logic: A Guide to Testing Dataverse Plugins with IOC
I’m not a pure TDD developer. I frequently take my best guess at a Dataverse plugin, then apply TDD until everything works. This c...
How to Filter Dates in Canvas Apps Using Greater Than/Less Than Operators
Defining the ProblemRecently I was attempting to filter an on-premise SQL table by a DateTime field using a “greater than” operator, and displ...
Enabling or Disabling All Plugin Steps In Dataverse
If you are facing an issue with a recent bug in PowerPlatform.BuildTools version 0.0.81 that caused your plugin steps to become disabled, fret...
Using AutoFixture To Create Early Bound Entities
AutoFixture is an open source library that is used in testing to create objects without having to explicit set all the values. I recentl...
Long Functions Are Always A Code Smell
This article is in response to fellow MVP Alex Shelga’s recent article Long functions in dataverse plugins – is it still ” code smell”?. ...
Handle All Your Plugin Exceptions In One Place, And Then Hide It!
In coding, one of the recommended best practices is to avoid creating duplicate code as it leads to maintenance issues. Therefore, it is advis...