10 Power Apps Code Examples To Improve Your Coding Style
You, my fellow human, are about to become better at writing Power Apps code.
By following a coding standard you can make your apps easier to maintain and have less bugs. High-quality code is simple to read. It follows a consistent pattern. Your goal is to write code that is simple for other developers and your “future-self “to understand.
It doesn’t matter if you are a professional or a complete newbie. It is important to write clean code. The best part is, anyone can learn to do it. Read these 10 Power Apps code examples. At the end of this, you’ll know what good code looks like and improve your own Power Apps development skills.
Table Of Contents: 1. Use The WITH Function 2. Apply Automatic Formatting 3. Write Helpful Comments 4. Flatten Nested IF Functions 5. Have A Consistent Naming Convention For Controls & Variables 6. Join Text Strings And Variables 7. Choose Consistent Logical Operators 8. Remove IF Statements When The Result Is A True Or False Value 9. Simplify Logical Comparisons When Evaluating A Boolean 10. Substitute The Self Operator For The Current Control Name Bonus: Alphabetize Patch & UpdateContext Function Arguments
1. Use The WITH Function
Power Apps With function makes long formulas more readable. For example, this formula which does not use the With function calculates the monthly mortgage payment for a house:
The mortgage calculation formula cannot be interpreted at-a-glance. It takes effort to parse. Compare it to the Power Apps code example using the With function. The formula is now human-readable because any complexity moved into one-time variables.
2. Apply Automatic Formatting
The formula bar’s format text command applies indentation, spacing and line-breaks to Power Apps code. Well-formatted code has two benefits. It is easier to read and quicker to spot mistakes. Use the format text command to achieve a consistent coding style throughout a canvas app. A consistent coding style makes it easier for developers to work together on an app.
3.Write Helpful Comments
Power Apps has two comment styles: line comments and block comments. Line comments are made on a single line and block comments are on multiple lines (see the Power Apps code examples below). Always write comments that can be understood by other developers and by your “future-self”. Write in full sentences and use plain-language. Do not use abbreviations, acronyms or slang language.
Use comments to clarify how a complex piece of code works. Do not use comments as an excuse to write complicated code in the first place. Comments are also helpful to explain what long-code blocks are doing. Leave comments every few lines to help orient the reader.
Comments create their own technical debt. As the code changes in an app its comments must be updated as well. Only write comments where necessary. Do not write comments where the intention of the code is already obvious. Use comments to temporarily remove code while debugging. Do not leave unused code in your app.
4. Flatten Nested IF Functions
Nested IFs are when multiple IF functions are placed inside one other. The more levels a nested IF contains the harder it becomes to understand. Use a flat structure whenever possible to improve code readability.
5. Have A Consistent Naming Convention For Controls & Variables
Every control should follow a naming convention that includes the control type, the screen it is located on and the purpose of the control. Variables should also have a standard format that includes their scope and purpose. Check out my article about Power Apps Standard Naming Conventions.
6. Join Text Strings And Variables
7. Choose Consistent Logical Operators
The logical operator And can be written 3 different ways: And, And(), &&. There are often many ways to do the same thing in Power Apps code. It’s OK to choose any option from the Power Apps code examples below but be consistent.
8. Remove IF Statements When The Result Is A True Or False Value
An IF statement that results in true or false is not necessary to write. Get rid of the IF statement and only write the logical comparison
9. Simplify Logical Comparisons When Evaluating A Boolean
A boolean value itself can be used as an argument to the IF function. It is not necessary to write a logical comparison.
10. Substitute The Self Operator For The Current Control Name
The Self operator is a concise way to access properties of the current control. Use Self instead of the full control name to make code quicker to understand.
Bonus: Alphabetize Patch & UpdateContext Function Arguments
When the Patch function have a large number of fields it takes more time to find and update them. Use alphabetical order so the desired field can be quickly located. This technique can also be applied to the UpdateContext function.
Did You Enjoy This Article? 😺
Subscribe to get new Power Apps articles sent to your inbox each week for FREE
If you have any questions about 10 Power Apps Code Examples To Improve Your Coding Style please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.