cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Microsoft Employee

Commenting Code

Is there a way to add comments to a code set or a code line?

3 ACCEPTED SOLUTIONS

Accepted Solutions
PowerApps Staff CarlosFigueira
PowerApps Staff

Re: Commenting Code

There's no way to comment code directly in the formulas. One workaround that I've used for complex formulas is to break it down in components (using context variables), and use some If(false) statements that do nothing except to allow for some information to be added. For example, in the app below, the expression button used to calculate the roots for the equation is a little complicated:

ForumPost-App.png

I could have a single expression to calculate both roots, but that would be quite complex:

UpdateContext({
    root1: If(Value(TextInputB.Text)*Value(TextInputB.Text) - 4*Value(TextInputA.Text)*Value(TextInputC.Text) < 0, 0/0, (-Value(TextInputB.Text)+Sqrt(Value(TextInputB.Text)*Value(TextInputB.Text) - 4*Value(TextInputA.Text)*Value(TextInputC.Text)))/(2*Value(TextInputA.Text))),
    root2: If(Value(TextInputB.Text)*Value(TextInputB.Text) - 4*Value(TextInputA.Text)*Value(TextInputC.Text) <= 0, 0/0, (-Value(TextInputB.Text)-Sqrt(Value(TextInputB.Text)*Value(TextInputB.Text) - 4*Value(TextInputA.Text)*Value(TextInputC.Text)))/(2*Value(TextInputA.Text)))
})

If I break it down into sub-expressions, I can interpolate some "comments" to make it more readable:

If(false, "Save control values in variables for easier manipulation");
UpdateContext({a:Value(TextInputA.Text), b:Value(TextInputB.Text), c:Value(TextInputC.Text)});

If(false, "Calculate the value for the discriminant");
UpdateContext({delta: b*b - 4*a*c});

If(false, "Calculate the roots");
UpdateContext({
    root1: If(delta < 0, 0/0, (-b+Sqrt(delta))/(2*a)),
    root2: If(delta <= 0, 0/0, (-b-Sqrt(delta))/(2*a))
})

View solution in original post

mr-dang
Level 10

Re: Commenting Code

Hi everybody,

I have learned another way you can include comments. Normally you chain actions with a semi-colon. But you could also write text where an action would go, then set it off with a semi-colon. It won't do anything, but it provides the comment you want.

 

Example:

"Add 1 to the current score each time you press the button";

Set(score,score+1)

 

Here's another example of it in practice: 
https://youtu.be/j30xOM5OmRE?t=7m57s

 

@MelindaK @CarlosFigueira @ManishM

Microsoft Employee
@8bitclassroom

View solution in original post

Dawidvh
Level 10

Re: Commenting Code

Great news, this was released yesterday

https://youtu.be/dnreydsFSHw

View solution in original post

11 REPLIES 11
PowerApps Staff CarlosFigueira
PowerApps Staff

Re: Commenting Code

There's no way to comment code directly in the formulas. One workaround that I've used for complex formulas is to break it down in components (using context variables), and use some If(false) statements that do nothing except to allow for some information to be added. For example, in the app below, the expression button used to calculate the roots for the equation is a little complicated:

ForumPost-App.png

I could have a single expression to calculate both roots, but that would be quite complex:

UpdateContext({
    root1: If(Value(TextInputB.Text)*Value(TextInputB.Text) - 4*Value(TextInputA.Text)*Value(TextInputC.Text) < 0, 0/0, (-Value(TextInputB.Text)+Sqrt(Value(TextInputB.Text)*Value(TextInputB.Text) - 4*Value(TextInputA.Text)*Value(TextInputC.Text)))/(2*Value(TextInputA.Text))),
    root2: If(Value(TextInputB.Text)*Value(TextInputB.Text) - 4*Value(TextInputA.Text)*Value(TextInputC.Text) <= 0, 0/0, (-Value(TextInputB.Text)-Sqrt(Value(TextInputB.Text)*Value(TextInputB.Text) - 4*Value(TextInputA.Text)*Value(TextInputC.Text)))/(2*Value(TextInputA.Text)))
})

If I break it down into sub-expressions, I can interpolate some "comments" to make it more readable:

If(false, "Save control values in variables for easier manipulation");
UpdateContext({a:Value(TextInputA.Text), b:Value(TextInputB.Text), c:Value(TextInputC.Text)});

If(false, "Calculate the value for the discriminant");
UpdateContext({delta: b*b - 4*a*c});

If(false, "Calculate the roots");
UpdateContext({
    root1: If(delta < 0, 0/0, (-b+Sqrt(delta))/(2*a)),
    root2: If(delta <= 0, 0/0, (-b-Sqrt(delta))/(2*a))
})

View solution in original post

Microsoft Employee

Re: Commenting Code

@CarlosFigueira Thanks for the workaround solution Man Wink. It is time for the Dev team to add the ability to comment code in a direct way.

Anonymous
Not applicable

Re: Commenting Code

Something I've just started to use thanks to the new global var Set() function is e.g.

 

Set(c_icn_Save_Props_OnSelect,"### TODO: Some comment here ###")

 

 ... note the "c_" prefix for comments and a name to reflect the object/method (debatable usage esp. if object name is changed!) .

 

I can then review-and link to-all of my comments from one place (the Variables (Experimental) tab).

MelindaK
Level 8

Re: Commenting Code

@CarlosFigueira thank for the workaround! That is very clever! 

 

One thing that worries me though, wouldn't these ifs add more load on the app when we are talking about complex apps with multiple screens and complicated formulas?

MelindaK
mr-dang
Level 10

Re: Commenting Code

Hi everybody,

I have learned another way you can include comments. Normally you chain actions with a semi-colon. But you could also write text where an action would go, then set it off with a semi-colon. It won't do anything, but it provides the comment you want.

 

Example:

"Add 1 to the current score each time you press the button";

Set(score,score+1)

 

Here's another example of it in practice: 
https://youtu.be/j30xOM5OmRE?t=7m57s

 

@MelindaK @CarlosFigueira @ManishM

Microsoft Employee
@8bitclassroom

View solution in original post

Sienna
Level 10

Re: Commenting Code

@mr-dang

Thanks for sharing. I will definitely try that. However I just hope that all this workarounds for comments will not stop PG to implement proper comments in the code

PowerApps Staff CarlosFigueira
PowerApps Staff

Re: Commenting Code

Don't worry, it will not stop the team from implementing comments - it should be coming soon. Thanks for the feedback!

Sienna
Level 10

Re: Commenting Code

@CarlosFigueira

Thank you... We can't wait for it

Anonymous
Not applicable

Re: Commenting Code

@CarlosFigueira
Any news ?

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

Top Kudoed Authors (Last 30 Days)
Users online (4,992)