avatarGabriel Shanahan

Summary

This web content discusses the use of scope functions in Kotlin, emphasizing their role in enhancing code readability and expressiveness rather than solving coding problems, and offers a series of exercises to solidify understanding.

Abstract

The article "Scope Functions: Closing Remarks & Exercises" delves into the philosophical aspect of Kotlin's scope functions, highlighting their purpose in improving code readability and communication between developers, rather than providing new coding functionalities. It acknowledges the lack of standardization in how these functions are used and encourages developers to adopt a consistent style that aligns with their reasoning. The author warns against the overuse of scope functions, particularly for the sake of brevity, and advocates for their thoughtful application to convey intent and enhance readability. The article also provides a set of exercises through embedded Kotlin Playground frames to help readers practice and internalize the concepts discussed.

Opinions

  • The author believes that scope functions are designed primarily to benefit the reader of the code, not to simplify problem-solving.
  • There is no standardized meaning associated with scope functions, and different developers may use them differently, as evidenced by the lead designer of Kotlin using with in a context where apply or run might be considered more appropriate.
  • The article suggests that developers should strive for consistency in their use of scope functions and develop a personal style that reflects their understanding and reasoning.
  • Overuse of scope functions is a common pitfall for new Kotlin developers, and it is important to use them judiciously, mainly to improve code readability and express intent.
  • The author recommends critical thinking over dogmatic adherence to rules when using scope functions and encourages watching a talk on avoiding the overuse of Kotlin features.
  • The exercises provided are intended to give readers hands-on experience with scope functions, reinforcing the theoretical knowledge gained from the article.

Scope Functions: Closing Remarks & Exercises

How scope functions solve readability problems, not coding problems, how their usage is not standardized, how to use them properly and a whole range of exercises to really get them down.

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

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

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

Tags: #FUNDAMENTAL CONCEPT #EXERCISE

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.

I hope this excursion into some of the most important functions from the Kotlin standard library has opened a new door in the way you think about designing code. One of the things that makes Kotlin unique is that it contains features solely for the benefit of the future reader, and not actual functionality. They are not there to make solving some type of problem easier, but to better express ourselves and communicate with the person who will be reading our code. Nowhere is this more true than in the context of scope functions.

However, as was said before, the meaning associated to these functions does not seem to be standardized. I have shown you my point of view, and shown you why I think it’s a reasonable one, but this is by no means what you will always encounter in the real world, and you don’t have to go far to find usages that are different. Indeed, the lead designer of Kotlin wrote an article in which he used with in a situation where, according to what was just said, he should have used apply or run. That does not make it right or wrong, merely open to interpretation. This is why I encourage you to find a style that you feel is right, and be consistent about it.

At the same time, I would like to stress that it is very easy to overuse scope functions, and in fact, many people do, especially when they first start out using Kotlin. While this is to be expected, it is good to keep in mind that usually, the only good reason to use a scope function is to communicate intent and/or make things easier to read. It is almost never a good idea to use scope functions only for the purpose of making code shorter, although, naturally, there are exceptions to every rule. As always, I encourage you to think critically about what you write, as opposed to binding yourself to a set of rules dogmatically.

I also strongly recommend watching the excellent talk Putting down the golden hammer, which discussed the abuse of Kotlin features (not only scope functions) in depth.

Exercises

Go back to Scope Functions: run & with, jump to the Table of Contents, or continue to Domain Specific Languages.

Kotlin
Java
Programming
Kotlin Scope Functions
Functional Programming
Recommended from ReadMedium