avatarGabriel Shanahan

Summary

The provided web content introduces scope functions in Kotlin, discussing their purpose, usage, and the confusion surrounding them, while emphasizing the importance of understanding their nuances beyond the official documentation.

Abstract

The article "Scope Functions: Introduction" delves into the concept of scope functions in the Kotlin programming language. It acknowledges the simplicity of these functions, which are part of the Kotlin Standard Library, yet highlights the complexity in their application and interpretation. The author stresses that scope functions are designed to enhance code conciseness and readability, and to effectively communicate intent. However, their misuse can lead to the opposite effect, complicating the codebase. The article points out that the official Kotlin documentation, while providing a basic overview, lacks depth in explaining the nuances and the rationale behind the recommended usage patterns of these functions. The author aims to fill this gap by offering a more detailed exploration, including examples and explanations, to help developers understand the distinctions and appropriate contexts for using each scope function. The functions covered include also, apply, let, run, with, as well as takeIf and takeUnless. The article encourages developers to form their own opinions and coding styles based on the insights provided.

Opinions

  • The author believes that the Kotlin documentation on scope functions is somewhat lacking, particularly in providing in-depth explanations and justifications for their usage.
  • Scope functions are seen as powerful tools that can improve code quality when used correctly, but their overuse or misuse can have a detrimental effect.
  • The article suggests that the interpretation of when and how to use scope functions is subjective and that developers should conduct their own research and experiments to determine the best practices that align with their intent.
  • The author expresses gratitude to Etnetera a.s. for supporting the creation of the Kotlin Primer, which this article is a part of.
  • It is recommended that readers familiarize themselves with the introduction and table of contents of the Kotlin Primer before proceeding with this article to gain a better understanding of the context and the series' structure.

Scope Functions: Introduction

An introduction to scope functions, what they are, why they can be confusing and how the official documentation is somewhat lacking

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FUNDAMENTAL CONCEPT

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Extensions enable us to create many interesting utilities that give programming Kotlin an entirely new dimension compared to many other mainstream languages. One of the prime examples are the so-called scope functions.

Scope functions are a set of fairly simple extension functions, and are part of the Kotlin Standard Library. Indeed, they are so simple that at first glance, one has no idea why they exists in the first place, much less why they’re included as part of the standard library.

Like many other Kotlin features, their purpose is mainly to make code more concise, easier to read, and most importantly to communicate intent. As is often the case with such features, they only shine if used properly, and there is a lot of confusion around when and how to use them, as evidenced by the multitude of articles written about them. With great power comes great responsibility, and overuse can actually make matters worse.

The scope function are also, apply, let, run, with, and we will also be mentioning takeIf and takeUnless.

It is important to emphasize that the intent with which these functions are used is very much open to interpretation, and you may come across very disparate opinions if you do your own research (which you absolutely should). The Primer presents its own point of view, and gives supporting arguments for why it thinks it is a reasonable one, but does so with the disclaimer that it is nothing more than that — a point of view.

While the documentation page does discuss when you should use a given scope function, and how it should be interpreted, I feel that it is somewhat lacking in certain ways. For one, the descriptions are often brief and non-illuminating, but more importantly, much of the information stated there is simply declared, without demonstrating why a certain style of usage should be preferred. The differences between the individual scope functions are often very nuanced, but this nuance is not always addressed explicitly in the documentation.

It could be argued that this is not what the documentation is for, however I think it is important to understand these things a little deeper. Therefore, the following articles will expand on, and clarify, what is written in the documentation, give examples, as well as give an explanation for why things should be interpreted in a particular manner, and compare the individual scope functions to tease out the differences in how they should be used.

As always, I would like to encourage you to make up your own mind, and find a style that you feel best expresses what you mean. In other words: you do you.

Go back to Extension Function Applications: Operators as Extensions, jump to the Table of Contents, or continue to Scope Functions: also & apply.

Join me in Etnetera
Kotlin
Java
Programming
Kotlin Scope Functions
Functional Programming
Recommended from ReadMedium