Thereâs a âgotchaâ either way
A lot of these are cases where the code could do either what you expect or something else. These âgotchasâ mean that no matter what the behavior the ambiguity could cause confusion.
To avoid confusion we can:
- code in the standard style to bring clarity
- code with some explicitly-ness added in to bring clarity
#2 is interesting because it keeps consistency. For example, this style forces consumption of the lambda and throws an error if itâs too many args:
bar()
{} // Always treated as an arg
The downside with trying supporting consistency for both styles (what OP is asking for) that is that it sacrifices consistancy when combined with some language choices. Trailing lambdas and implicit semicolons are features that run counter to next-line styling.
I can see an argument for limited changes. Iâm mostly speculating on there being more corner cases that would be annoying to solve. Youâd probably still need a version bump in Kotlin to change the grammar right?
{ A bit of a word spill here. Itâs related but enough of a side topic that Iâm hoping to keep it from being a distraction }
Compiler choices and style rant
This is one of several places code would get ambiguous.
The compilerâs interpretation switching to match interpretation, and favor including the next-line, is equally reasonable compared the one statement interpretation. No matter what there is a âgotchaâ. If we must pick an interpretation to favor, shouldnât the official style be favored?
Sacrafice consistency might be okay?
With that said I think you might have a stronger argument on very specific cases. Letâs look at the parathesis requirement for trailing lambdas on the next line.
In favor of forcing parentheses:
The compiler is helping you write consistent code by forcing you to explicitly choose the less-kotlin-like interpretation (the less common one) and wonât compile any other way when you write parathesis-\n-lambda.
The compiler errors on
println("")
{} // Error, this is consistently seen as an argument even though both lines could be valid statements
One could argue that itâs pragmatic to handle this case conditionally. Like when one statement is invalid. This is what I and gidds were originally suggesting just on different criteria. One could argue itâs worth limited sacrifice of the consistencyâlimiting to very specific cases might make the argument stronger.
Style isnât always subjective
I want to make the case that itâs okay for a languageâs official style to be favored, and for languages to not support a wide range of styles.
For example, Kotlin does not support next-line style to the same degree as Java. When the compiler is given equally valid options for interpreting statements. The compiler favors the interpretation that lines up with the official style.
For many languages, syntax features will be implemented favor a style. Sometimes language features enable more flexibility (i.e. Java ignoring all whitespace). Other languages will not allow that at all flexibility (which is just syntax at that point) such as Python using whitespace scope.
Kotlin is somewhere in the middleâit is flexible but requires slight code edits for style changes. Features like trailing lambdas and implicit semicolons force style to tie into language features. Kotlin supporting fewer styles enables more freedom in implementing these features.
A style being supported could be thought of as a language feature. It puts limits on future features. Seeking to support an additional style, such as ignoring all whitespace, comes at a huge cost.
Always prefer the standard, even in practice
I like the saying âwe code for humans, not for computersâ because it highlights that the top priority of source code is clear communication with humans. In this context, Iâd add that âwe code for other humans, not for ourselvesâ. Code is about communicationâitâs the reason we can say one should always prefer your teamâs style. In the same vain, your team should prefer the communityâs style over its own.
Iâd suggest that even in your own private code, which will never be seen by another human, you should still code in the official style. The reason being is that if one wishes to communicate clearly in any language/dialect, they should try to communicate as the native speakers do. They should practice and become comfortable with the idioms, grammar, structure of the words as it is spoken by the community.
Yes Itâll be uncomfortable at first but personal taste is malleable. Even if there isnât enough time to un-due oneâs discomfort in the style, thatâs okay. At least they communicated clearly.
Donât lead your team into your dialect
Choosing your teams style is good for communication, but what if your preferred style is chosen?
I have seen some people get away with making their team choose their style. This person liked to space their code differently with spacing and alignment. They also valided consistancy and the team adapted their dialect. They got their preferred style and consistency!
IMHO this is putting oneâs preference over your teams needs to communicate in the larger community and their projectâs/teamâs future. Team members change. Code doesnât live in isolation. Making a team adapt a preference thatâs against the grain is not good for the team.
Teams sometimes respond with âitâs just preference, we can appease your style needsâ to keep the top developer happyâbut thatâs an unfortunate scenario. Iâd hope all of the developers, especially the most valuable ones would seek the best for their project and team even if it means some discomfort in style.