Hey Coders,
we all know the neverending discussion about the following examples:
fun getName() {
return name
}
and
fun getName()
{
return name
}
Putting the opening curly bracket in the same line or not. I personally like the second way. When you write more code, the first way tend to get messy. But official style-guides use the first way alot, so you kinda have to stick with that.
Other Languages, like ruby or python, don’t have curly brackets. If you are not used to it, you need a little bit of time to adapt to it. But I bet you will like it. Because the code looks so much cleaner without all the curly brackets. Especially when you write nested code.
So I was wondering what would it look like, if Kotlin would not have the curly brackets too. Lets say we remove the opening bracket and replace the closing bracket with the keyword ‘end’ (just like in ruby). Like this:
fun getName()
return name
end
Doesn’t look that much improved, right? But what if we have something a little bit more complex, for example creating an Overservable:
Observable
.just(1, 2, 3, 4, 5)
.subscribe(object:Observer<Int>() {
override fun onNext(data: Int) {
// todo
}
override fun onComplete() {
// todo
}
override fun onError(throwable: Throwable) {
// todo
}
})
And here without the curly brackets:
Observable
.just(1, 2, 3, 4, 5)
.subscribe(object:Observer<Int>()
override fun onNext(data: Int)
// todo
end
override fun onComplete()
// todo
end
override fun onError(throwable: Throwable)
// todo
end
end)
If we need the opening curly bracket as separator, like here:
Observable.create { emitter ->
emitter.onNext(1)
emitter.onComplete()
}
We could replace it with the keyword ‘do’:
Observable.create do emitter ->
emitter.onNext(1)
emitter.onComplete()
end
So, what do you think?