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

Commenting Code

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

3 ACCEPTED SOLUTIONS

Accepted Solutions
Power Apps Staff CarlosFigueira
Power Apps 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

Super User
Super User

Re: Commenting Code

Great news, this was released yesterday

https://youtu.be/dnreydsFSHw

View solution in original post

11 REPLIES 11
Power Apps Staff CarlosFigueira
Power Apps 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 ManishM
Microsoft

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

Power Apps Staff CarlosFigueira
Power Apps 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
New Ranks and Rank Icons in April

'New Ranks and Rank Icons in April

Read the announcement for more information!

Better Together’ Contest Finalists Announced!

'Better Together’ Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

thirdimage

Community Summit North America

Innovate, Collaborate, Grow - The top training and networking event across the globe for Microsoft Business Applications

Top Solution Authors
Top Kudoed Authors
Users online (8,607)