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

Commenting Code

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

4 ACCEPTED SOLUTIONS

Accepted Solutions
CarlosFigueira
Power Apps
Power Apps

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
Community Champion
Community Champion

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

 

@Anonymous @CarlosFigueira @ManishM

Microsoft Employee
@8bitclassroom

View solution in original post

Great news, this was released yesterday

https://youtu.be/dnreydsFSHw

View solution in original post

ManishM
Microsoft
Microsoft

This is great! Thank you MSFT for the feature and @DavesTechTips for notifying.

View solution in original post

12 REPLIES 12
CarlosFigueira
Power Apps
Power Apps

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

@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

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).

Anonymous
Not applicable

@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?

mr-dang
Community Champion
Community Champion

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

 

@Anonymous @CarlosFigueira @ManishM

Microsoft Employee
@8bitclassroom

View solution in original post

Sienna
Memorable Member
Memorable Member

@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

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

@CarlosFigueira

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

Anonymous
Not applicable

@CarlosFigueira
Any news ?

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (1,137)