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
Combining text can be done multiple ways in Power Apps: the & operator, the Concatenate function or $-String notation. Choose one way of doing it and be consistent.
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
Questions?
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.
Man, this is great content. I finally understand the value of With from the example you provided.
Will,
I’m glad to hear it finally clicked for you. The Power Apps With function is awesome and I just know you’re going to use it everywhere now!
Thank you Matthew!
The bonus alphabetize patch tip is a gem!
And #4 flattening nested ifs also sort of came up for me today in Power Automate. I’m not sure if my syntax is correct but I’m trying to illustrate the correct concept (perhaps) to do basically what I needed to achieve in Power Apps using the gblNumber example below followed by the method I ended up using in Power Automate using equals. (And believe it or not my very thought after getting it all to work was ‘there’s got to be a more elegant and less spaghetti like way to get this done – especially if I have perhaps 100 use cases rather than just the three below).
If(
gblNumber = 1,
Notify(“1 eq 1”,Info),
gblNumber=2,
Notify(“2 eq 2”, Info),
gblNumber=3,
Notify(“3 eq 3”, Info)
);
VS (in Power Automate)
If(equals(1,1),true,If(equals(2,2),2,3))
Sorry if the syntax I’ve reconstructed above from memory is flawed, but I think you get what I mean – and if you have any tips on how to simplify my Power Automate expression (especially were it to required to handle scores or hundreds of use cases) I would appreciate any advice!
PS – I think I need a poster by my desk will all 10 of your Improve Your Coding Style blog tips.
Thank you,
Gerald Dahl
Gerald,
I’m happy you liked the bonus tip to alphabetize fields. It’s something I started doing because it was getting hard to quickly locate the field when there are 20 or more in a record. Then the pattern just kind of stuck and now I always use it. Once you work in Power Apps 40 hours a week you start to develop habits.
In Power Apps we could use the SWITCH statement to get even simpler:
Switch(
gblNumber,
1,
Notify(“1 eq 1”,Info),
2,
Notify(“2 eq 2”, Info),
3,
Notify(“3 eq 3”, Info)
);
For Power Automate, I like this suggestion by @DamoBird365 (Twitter). He shared a method where gblNumber is used to filter on the unique identifier in an array. You supply an array to a compose action (i.e your table of values) then use the Filter Array action to get the result you need.
A link to his article can be found here:
https://www.damobird365.com/if-elseif-else-in-power-automate-simplified/
Thanks, Matthew. I have reached out to Damien! I also appreciate the ‘switch’ tip your reply has – I need to add that technique along with ‘with’ from this blog post of yours not to mention ‘as’’ as demonstrated to me on different occasions by Darren Neece https://twitter.com/developermct?s=21&t=v8TbG3u1wontmaLFLI26rw and others. Hopefully I’ll look back in several months and see some of these new practices in place.
Thanks Matthew. This is a great post. I love the examples you provide, it just seems to click with me. I find canvas apps the hardest part of the Power Platform, so your blog is invaluable to me.
Great stuff, again !
Great stuff Matthew. You’re Power App articles are spot on!
Andrew,
Thanks for leaving a comment. I enjoyed your idea for a Power Apps drinking game 🙂
Wow. I’ve learnt a lot reading this! Thanks Matt! I specifically love the WITH function, very nice going to give that a try.
Billy,
The WITH function is awesome. Shane Young swears by it!
I updated one of my Apps yesterday and re-wrote a bit of code from the ground up, and I was pretty happy and proud of how much better it looked. Then today I went through this list and made even more changes based on your suggestions (especially WITH and the $-string notation). I feel like my code is much clearer and easier to maintain. Thanks heaps!
Thank you Matthew! Great content. I like it when the code looks nice too.
Have a suggestion or better a question: when developing and especially maintaining an app, I like to work with test data. How do I solve the switch between the productive and the test data in PowerApps? I suppose this would also be interesting for the community.
Daniel,
Best practice is for production & testing to be done in two separate environments. In fact, I typically have 3 environments: Development, Test and Prod. Each environment has its own data. Once developers are done coding, the app is moved to the test environment in a Solution (i.e. a package). Then once testers approve the app it gets moved into production.
I recommend you watch this recent video by Reza Dorrani. It will clarify many things I am saying:
https://www.youtube.com/watch?v=Xo-TvZ9N3BM
Thanks for the tip and best regards from Switzerland
Daniel,
Your welcome Daniel. I hope to visit Switzerland someday!
I love WITH but I am still a little confused when you have Nested WITH’s. I certainly couldn’t explain nested WITH’s to anyone 🙁
Richard,
Now that you say that, I agree, nested WITHs are definitely an anti-pattern. Nested WITHs are insanely hard to troubleshoot. You cannot inspect anything two-levels deep so I end up putting TRACE functions inside the damned thing.
From this day forward, I will no longer nest WITHs.
Awesome 👍
Really like this post, useful information. Thanks Matthew.
Anujit,
Glad you enjoyed this post.
Is there a duplicate date check code?
Bank,
Yes, you can check for duplicate dates in Power Apps. In some data sources you can even create rules to only allow unique values.
Hey Matthew, great tips! Thank you for them. I have just one question – what is the best way to split long code into multiple, more readable parts?