Curly braces vs. square braces

224 views
Skip to first unread message

mich...@chromium.org

unread,
May 15, 2015, 5:57:30 PM5/15/15
to polym...@googlegroups.com
Is there a guideline for when to use curly vs. square braces, when the effect would be the same?

For example PRIMER.md uses curly braces for text content, but it's just a one-way binding:

<span>{{first}}</span>

Computed bindings are another example. PRIMER.md uses curly braces, which seems confusing because it's not really two-way binding:

<x-child some-property="{{getValue(otherThing)}}">

Finally in one-way downward binding (notify: true is not set on a property), using curly braces won't result in the host property being updated.

Are there any performance reasons to use one or the other? Or suggested style or forward-compatibility reasons?

Arthur Evans

unread,
May 19, 2015, 1:27:39 AM5/19/15
to mich...@chromium.org, polymer-dev
There are no official guidelines. I think for the most part it's a style thing, and at the moment what you see in the docs may depend on who first wrote the example. There are two basic schools of thought:

School #1) Curly braces are "automatic" -- in almost all cases, they just do the right thing. Use 'em everywhere unless you specifically want to rule out the possibility of a two-way binding. (You'll see that a lot of the docs are written this way, partly for historical reasons -- 0.5 had only the curly brackets.)

School #2) Using square braces for all one-way bindings makes it easy to see the expected data flow. So use square brackets everywhere and only use curly brackets where you specifically want a two-way binding. (Most of the new elements follow this style.)

I'm not aware of big performance implications either way. I'd say pick your style and stick to it.

Cheers,
Arthur


Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/fdb138e1-dc82-420a-9d10-ec0d55a715cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages