# There is no total freedom for spacing in Kotlin

**URL:** <https://discuss.kotlinlang.org/t/there-is-no-total-freedom-for-spacing-in-kotlin/13641>\
**Category:** Support\
**Created:** [July 26, 2019, 10:10pm UTC](https://discuss.kotlinlang.org/t/there-is-no-total-freedom-for-spacing-in-kotlin/13641 "2019-07-26T22:10:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pbuchsbaum](https://avatars.discourse-cdn.com/v4/letter/p/f17d59/32.png) [@pbuchsbaum](https://discuss.kotlinlang.org/u/pbuchsbaum)\
**Post date:** [July 26, 2019, 10:10pm UTC](https://discuss.kotlinlang.org/t/there-is-no-total-freedom-for-spacing-in-kotlin/13641/1 "2019-07-26T22:10:15Z")

</div>

Look at this code in _Kotlin Playground_

```
fun main() {
  val eBin = true
  val eHexa = true
  var s = "99"
  println( when { eBin -> "b" 
          eHexa -> "h" else ->"" } + s)
}

```

It works nice. However…

```
fun main() {
  val eBin = true
  val eHexa = true
  var s = "99"
  println( when {eBin -> "b" eHexa -> "h" else ->"" } + s)

```

}

It fails just because `when` expression is all in one line. There is no ambiguity, because a string cannot appear together with a variable.

If one puts curly braces in the first `when` option:

```
fun main() {
  val eBin = true
  val eHexa = true
  var s = "99"
  println(when { eBin -> {"b"} eHexa -> "h" else ->"" } + s)
 }

```

It works again!

Most language that I now don’t consider error any space usage, except inside strings.  
I think that `Kotlin` should follow the same philosophy.

---

<div class="post-metadata">

**Author:** ![wdoker](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wdoker/32/4748_2.png) [@wdoker](https://discuss.kotlinlang.org/u/wdoker)\
**Post date:** [July 26, 2019, 11:26pm UTC](https://discuss.kotlinlang.org/t/there-is-no-total-freedom-for-spacing-in-kotlin/13641/2 "2019-07-26T23:26:20Z")

</div>

use semicolons

```kotlin
println( when {eBin -> "b"; eHexa -> "h"; else ->"" } + s)

```

---

<div class="post-metadata">

**Author:** ![pbuchsbaum](https://avatars.discourse-cdn.com/v4/letter/p/f17d59/32.png) [@pbuchsbaum](https://discuss.kotlinlang.org/u/pbuchsbaum)\
**Post date:** [July 27, 2019, 2:58am UTC](https://discuss.kotlinlang.org/t/there-is-no-total-freedom-for-spacing-in-kotlin/13641/3 "2019-07-27T02:58:02Z")

</div>

Yes, it works, but without semicolons we have a strange combination between compiler errors and no errors.

---

<div class="post-metadata">

**Author:** ![EWLameijer](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@EWLameijer](https://discuss.kotlinlang.org/u/EWLameijer)\
**Post date:** [July 27, 2019, 8:40am UTC](https://discuss.kotlinlang.org/t/there-is-no-total-freedom-for-spacing-in-kotlin/13641/4 "2019-07-27T08:40:23Z")

</div>

I thank the OP for pointing me towards a problem I had not considered yet: that semicolons (or other visible separator tokens), annoying as typing and reading them can be, are needed for compilers to parse every weird code formatting corner-case that programmers can think of. And if we do not want semicolons (like in Kotlin, Python, Ruby, Go, Swift and other languages) developers still have to sacrifice something, namely complete freedom of layout. No such thing as a free lunch.

Personally, I like uniform code/a uniform code style, and I am also convinced that uniform code style is the way of the future for programming languages (much better for maintenance, and professional developers tend to do much more maintenance than ‘greenfield coding’). Python’s PEP8, C#/Visual studio’s autostyling, Go’s automatic formatter… Kotlin too (in IntelliJ) has an auto-formatter, which transforms the original line into

```
println(
    when {
        eBin -> "b"
        eHexa -> "h"
        else -> ""
    } + s
)

```

No semicolons there, no confusion about how many items one can squash into one line (because, frankly, you shouldn’t if you want to understand the code two months from now, and overall more readable than the original. Though, if I were to need to get this code past code review, I’d probably make it:

```
val prefix = when {
    eBin -> "b"
    eHexa -> "h"
    else -> ""
}
println(prefix + s)

```

(and if I were really nitpicky I would point that eBin and eHexa are likely exclusive, so they would be modes in an enum, instead of separate booleans which can give weird results if more than one is set to ‘true’, enum class DisplayMode(val prefix: String) { DECIMAL(“”), BINARY(“b”), HEXADECIMAL(“x”) } …val mode = DisplayMode.HEXADECIMAL… println(“${mode.prefix}99”) which would make the when statement superfluous - but the issue here is the effects of whitespace, not domain-driven design or programming best practices)

So overall, while I agree with the OP that the Kotlin parsing situation is not ‘ideal’, and possibly there could be a request to JetBrains to make their parser yet more intelligent (though I am not sure they can/should do so, as that will decrease compilation speed and thereby negatively affect development, likely more than any added safety or convenience for corner whitespace formatting cases), I consider that as long as people write ‘default formatted code’, the problem is not that handicapping. Compared with the position of Python, for example, where the freedom from semicolons comes with sacrificing easy iterable operation chaining (or at least requires Python developers to insert extra ()), I think Kotlin/the JetBrains team is doing a pretty good job.

---

<div class="post-metadata">

**Author:** ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)\
**Post date:** [July 27, 2019, 7:37pm UTC](https://discuss.kotlinlang.org/t/there-is-no-total-freedom-for-spacing-in-kotlin/13641/5 "2019-07-27T19:37:23Z")

</div>

Since we’re talking about semicolons… My problem with Kotlin’s semicolon inference is that it’s too eager.

Right now it assumes a semicolon at the end of any line that would compile on its own, even if the following line then gives an error. In my humble opinion, it would be better for it to assume a semicolon only if _both_ the current and following lines would make sense.

I hit this issue very frequently, because I find code much easier to scan if lines wrap _before_ an operator. For example:

```
override fun toString() = "MyObject(field1=$field1, field2=$field2, field3=$field3, "
                        + "field4=$field4, field5=$field5, field6=$field6)"

```

This sort of wrapping is fine in Java, but Kotlin wrongly infers a semicolon at the end of the first line, and then gives a syntax error on the second.

Even worse, there are cases where it silently does the wrong thing. For example (within a function):

```
val a = someLengthyNumericExpression
      - someOtherNumericExpression

```

Here both lines stand alone. (The second is simply a numeric expression, with a unary minus.) So it compiles, and evaluates the second expression, but does _not_ take it into account when setting the val.

That strikes me as a very dangerous situation!

Why can’t Kotlin infer a semicolon _only_ where _both_ lines would make sense with one? That change wouldn’t break any existing working code, and would prevent annoyances and dangers like the above!

---

<div class="post-metadata">

**Author:** ![nbness1337](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@nbness1337](https://discuss.kotlinlang.org/u/nbness1337)\
**Post date:** [July 28, 2019, 2:49am UTC](https://discuss.kotlinlang.org/t/there-is-no-total-freedom-for-spacing-in-kotlin/13641/6 "2019-07-28T02:49:44Z")

</div>

I don’t think there needs to be “total freedom for spacing” in kotlin, as that usually leads to weird unreadable code. This code snippet is a great example of code you should not write.

`when` is not a one line minded thing. That’s like putting a `switch` on one line in Java. It makes no sense and it’s annoying to read!

For example, if your `when` has only 1 branch, you can just use a 1 line if. And if you are using `when` as an expression, you need 2 conditions (unless you are using a sealed class or enum with only 1 “known value”) and should be using 2 different lines to more easily present that you have 2 different conditions.

So you should definitely be writing it as @EWLameijer stated. Easily understood.

---

<div class="post-metadata">

**Author:** ![nbness1337](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@nbness1337](https://discuss.kotlinlang.org/u/nbness1337)\
**Post date:** [July 28, 2019, 3:01am UTC](https://discuss.kotlinlang.org/t/there-is-no-total-freedom-for-spacing-in-kotlin/13641/7 "2019-07-28T03:01:31Z")

</div>

It makes sense that your examples work as they do. There’s nothing “eager” about the semicolons inference as it does exactly what you tell it to according to the language. You can move the operator to the end of the first line and keep the second operand on the second line and it will work as expected.

I find code easier to read the way I stated as well. I don’t get to the end of a line, see nothing and then go to the next line and see the operator. I see the operator at the end of the first line, see the operator and nothing after it and come to the conclusion that there is an operand on the next line.

---

<div class="post-metadata">

**Author:** ![pbuchsbaum](https://avatars.discourse-cdn.com/v4/letter/p/f17d59/32.png) [@pbuchsbaum](https://discuss.kotlinlang.org/u/pbuchsbaum)\
**Post date:** [July 31, 2019, 12:03am UTC](https://discuss.kotlinlang.org/t/there-is-no-total-freedom-for-spacing-in-kotlin/13641/8 "2019-07-31T00:03:07Z")

</div>

I don’ t use my example as a “example” of good programming style. It’s just for make a reflection about syntax rules. I don’t like strict rules, I don’t like semi colons and I don’t like spacing syntax limitations…  
There is not a only one good style. Some people love spaces and long indents, other people don’t. There is no right answer. But it is just my personal opinion.
