The 8 Golden Rules of Writing Clean Functions - Clean Code In Kotlin

The 8 Golden Rules of Writing Clean Functions - Clean Code In Kotlin

Brief Summary

In this video, Philipp Lackner presents eight essential principles for writing clean functions in Kotlin. These principles emphasize clarity, maintainability, and simplicity in code, applicable in various programming languages.

  • Single Responsibility Principle: Functions should have one reason to change.
  • Avoid Side Effects: Functions should not have hidden effects that alter the state elsewhere.
  • DRY Principle: Avoid code duplication to enhance maintainability.
  • Minimize Parameters: Reduce the number of function parameters to improve readability.
  • Fail Fast: Throw exceptions as early as possible when errors are detected.
  • Early Returns: Use early returns to streamline code and reduce nesting.
  • Appropriate Level of Detail: Adjust function details to match the overall class purpose.
  • Use of Extension Functions: Create extension functions only when it makes sense for the design.

Intro

Philipp introduces the video, outlining the focus on eight evergreen principles that enhance the cleanliness of Kotlin functions. He emphasizes that these principles are applicable across multiple programming languages.

Principle 1: Single Responsibility

The first principle discusses the Single Responsibility Principle, which asserts that functions should have only one responsibility. Philipp illustrates this with a poorly designed registerUser() function that carries multiple responsibilities, making it difficult to understand and maintain. He suggests refactoring into smaller functions, enhancing readability and reusability.

Principle 2: Avoid Side Effects

The second principle is to avoid side effects, which occur when a function's execution impacts external states, such as databases or analytics. Philipp provides an example of an applyDiscount() function that modifies multiple states unexpectedly. He stresses creating clear, self-contained functions to prevent unintended consequences and improve clarity.

Principle 3: DRY (Don't Repeat Yourself)

The DRY principle emphasizes not repeating code. Philipp explains this principle by examining repeated formatting across multiple functions. He advocates for creating a single formatting function to consolidate logic, thereby enhancing maintainability and decreasing the chances of bugs.

Principle 4: Minimize Parameters

Minimizing parameters in function definitions is the focus of the fourth principle. Philipp critiques a createInvoice() function with an unwieldy number of parameters, suggesting that related parameters be grouped into data classes. This restructuring improves code clarity and lowers cognitive load for developers.

Principle 5: Fail Fast

The fifth principle, "Fail Fast," encourages developers to throw exceptions as soon as a failure condition is detected. Philipp advocates for early exception handling in functions, preventing unnecessary code execution and helping developers identify problems swiftly.

Principle 6: Early Returns

Philipp discusses the advantage of using early returns to simplify code flow. By checking conditions and returning early when possible, developers can avoid complex nesting and improve code readability. He advises structuring code to make explicit what conditions result in early returns.

Principle 7: Appropriate Level of Detail

The seventh principle stresses tailoring the level of detail in functions according to their purpose. Philipp critiques a checkout() function with excessively low-level implementation details. He emphasizes that functions at a high conceptual level should reflect that in their implementation, calling upon simpler, higher-level functions for complex operations.

Principle 8: Create Extension Functions

The final principle focuses on the use of extension functions in Kotlin. Philipp explains that extension functions should be utilized when the calling object is the owner of the operation being performed. He advises against using extension functions arbitrarily and instead suggests thoughtful implementation to enhance code clarity and design.

Conclusion

In the conclusion, Philipp reinforces the importance of adopting these eight principles for writing clean and maintainable code. He invites viewers to explore further resources for developing their skills in Kotlin and enhancing the quality of their applications.

Share

Summarize Anything ! Download Summ App

Download on the Apple Store
Get it on Google Play
© 2024 Summ