10 Power Apps Code Examples To Improve Your Coding Style

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.

Power Apps With function code example




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.







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.

Matthew Devaney

Subscribe
Notify of
guest
21 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Will
Will
2 months ago

Man, this is great content. I finally understand the value of With from the example you provided.

Gerald Dahl
2 months ago

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 Dahl
2 months ago
Reply to  Gerald Dahl

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.

Jon Russell
2 months ago

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.

George Winters
2 months ago

Great stuff, again !

Andrew Burns
Andrew Burns
2 months ago

Great stuff Matthew. You’re Power App articles are spot on!

Billy Prater
Billy Prater
2 months ago

Wow. I’ve learnt a lot reading this! Thanks Matt! I specifically love the WITH function, very nice going to give that a try.

Andrea
Andrea
2 months ago

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!

Daniel
Daniel
2 months ago

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
Daniel
2 months ago

Thanks for the tip and best regards from Switzerland

richard clowes
2 months ago

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 🙁

Himanshu
Himanshu
1 month ago

Awesome 👍

Anujit
Anujit
1 month ago

Really like this post, useful information. Thanks Matthew.