Kotlin / kotlin-style-guide

Work-in-progress notes for the Kotlin style guide

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

String Interpolation

shyiko opened this issue · comments

Should ${property} be always used instead of $property?

Some of the reasons in favor of the former:

  • no need to add {..} whenever property becomes property.value (or some other expression containing . (in this particular case IDE can help)) (and vice versa, no need to remove {} when going from property.value to propperty)
  • no need to add {...} just to prevent a clash with the text that follows the expression in the same string template (e.g. $property/0 -> ${property}0)
  • more visually distinguishable from the rest of the text (matter of personal taste)
  • one rule = no need to switch between two different styles that accomplish the same thing (except that ${...} is a proper superset of $property)

NOTE: Redundant curly braces in string template is set to "Weak Warning" by default in Intellij IDEA.

EDIT: crossed out 3rd item (based on #39 (comment)) & added a comment about IDE to the 1st.
Everything else still stands.

I think, for consistency's sake, we should always use the curly braces. How would one go about muting the warning that intellij shows for redundant curly braces?

I don't agree with this. Curly braces are visual clutter where that isn't necessary. When you have an interpolated variable without curly braces and you want to call a method on it, you can simply type .call, use the autocompletion and the braces will be inserted for you.

grafik

The current draft of the style guide explicitly says that curly braces in string interpolation should not be used when not required. Code without extra curly braces is easier to read, and all the arguments you have provided apply to code modifications, not code reading.