https://kotlinlang.org logo
Title
d

Danish Ansari

12/02/2021, 2:52 PM
Today in an interview I was asked about scoped functions in Kotlin. I explained all of them very briefly and also explained the difference between each of them very clearly. But I got a counter question "What's the reason scope functions exists? I mean if we can do all of the things/ operation on an object without scope functions, then why use scope functions?" I told "It reduces boiler plate codes" Interviewer: "Any other reason apart from reducing boilerplate code". Now this is the part where I got confused and went speechless. And started wondering about the question. I always s*ck at "WHY" part of the questions in an interview 😅
r

Richard Gomez

12/02/2021, 3:05 PM
Did the interviewer elaborate at all? Saying "it reduces boilerplate code" seems like a fair answer to me — a lot of Kotlin's standard library is helpful sugar around common problems.
d

Danish Ansari

12/02/2021, 3:07 PM
I mean he accepted my answer but wanted more "reasons" of using scope functions
p

Paul Griffith

12/02/2021, 3:15 PM
In addition to reducing boilerplate, extension can make code easier to read, in my opinion, and also reduce potential errors E.G. (this is bad code, but still kinda demonstrates)
val label1 = JLabel()
    val label2 = JLabel()
    label1.text = "label1"
    label1.icon = null
    label1.horizontalAlignment = SwingConstants.HORIZONTAL
    label2.text = "label2"
    label2.icon = null
    label2.horizontalAlignment = SwingConstants.HORIZONTAL
You're constantly repeating
label1
and
label2
, your modifications of the object aren't 'attached' to the initialization, etc
f

Fleshgrinder

12/02/2021, 3:21 PM
Scope functions are called scope functions because with them we can control the scope of symbols (their lifetime and accessibility).
someMap.forEach { (k, v) -> }
Here both
k
and
v
are only available within the scope of
forEach {}
, not before, not after. There are no questions regarding what value
k
or
v
has after the loop concludes. Compare this with the following (pseudo-code):
for (i = 0; i < x; ++i) {}
println(i)
In some languages this actually works because
i
leaks since it’s not scoped to only the loop.
👍 5
There are also languages where this scope control is really important (e.g. Rust).
d

Danish Ansari

12/02/2021, 3:46 PM
@Fleshgrinder nice explanation, thanks
😊 1
d

Dominaezzz

12/02/2021, 4:24 PM
Maybe your interviewer didn't know the answer himself and also wanted to know why. 😛
d

Danish Ansari

12/02/2021, 4:38 PM
@Dominaezzz that can also be the case 😂
a

Astronaut4449

12/03/2021, 7:32 PM
Maybe he wanted you to mention the open closed principal of the SOLID design pattern? It makes code easily extendable (open) without needing to modify it (closed)? Just an idea. Personally my favourite reasons are readability, less boilerplate and context dependent code completion...
Oh man that got me thinking as well
It must be about readability and autocompletion. It doesn't add any real functionality.
Well it makes lambdas with receivers possible which again enables DSL capability
☝️ 2
d

Dominaezzz

12/03/2021, 7:46 PM
I tend to use them only when I can't be arsed to name variables.
it
is sometimes more readable that
superContextualLongVariableName
. Otherwise, overuse can make code trickier to read, especially with
?.
and
?:
.
a

Astronaut4449

12/03/2021, 7:48 PM
The good old Java times when you had to remember that you needed the static
Arrays.asList()
method to create a list.