Skip to content

Kotlin code style consistency with other codebases #1122

Description

@tomholub

@DenBond7 this is an interesting thing - after a lot of discussion between me, Ivan and Julian on other repositories, we settled for the pattern that Ivan used above, and we disabled the IDE nudges to refactor it as you suggest.

The reason is that we'd like the code to be very understandable to people who only know Java and see Kotlin for the first time (which may be the case for the security reviewers for example, or for new team members).

And so instead of:

val x = if(cond) {
  1
} else {
  2
}

we'd write

val x: Int
if(cond) {
  x = 1
} else {
  x = 2
}

Similarly with try/catch. It's more code but we think it increases clarity.

In situations when you really want succinct usage, we can use when which is I think better structured for implicit returns:

val x = when (cond) {
  true -> x,
  false -> y
}

Generally also trying to avoid implicit returns in long lambdas, like this:

whatever(something, lambda = {
  something()
  else()
- thisIsReturnedButItsNotObvious()
+ return@lambda thisIsClearer()
})

We have never discussed code style much on Android because you were the only contributor and the code style was already sensible. However as we have several people who may contribute to Kotlin code these days, we should use relatively consistent styling across repositories.

(I'll file an issue with this comment)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions