In the presentation Functional Thinking is SOLID and clean by Juan Chiaradia, he discusses functional thinking and its application to SOLID principles using the example of building a car UI in a cloud-based application. He emphasizes the importance of functional concepts like function composition, partial application, and high-order functions in creating clean and solid code, even when working with object-oriented languages. Juan provides examples of how these concepts can be applied to Kotlin code and abstract dependencies using the repository pattern and functional interfaces. He also discusses the relationship between functional programming and object-oriented programming and the benefits of using a combination of both approaches.
Functional Thinking is SOLID and clean: A comprehensive overview
Introduction to Functional Thinking
Juan Chiaradia began his talk by clarifying that his objective was not to teach functional programming but to demonstrate how functional concepts can be used to enhance code quality, making it both SOLID and clean. SOLID, an acronym for five key software design principles — Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion — can often feel cumbersome in practice. Juan argued that functional thinking could simplify their application, especially in object-oriented languages like Kotlin and Java.
The essence of Functional Thinking
Functional thinking, as Juan described it, involves solving problems by composing functions that are declarative and free of side effects. This paradigm shift encourages developers to think about code in terms of transformations from inputs to outputs, rather than as a series of state changes. By embracing function composition, developers can break down complex problems into smaller, manageable pieces, leading to clearer separation of concerns and easier maintenance.
Juan illustrated this concept with a practical example: building a car UI in a cloud-based application. In this scenario, data flows from the cloud, through domain and application layers, ultimately rendering on the user interface. By applying functional composition, each layer can focus on its specific role, reducing dependencies and making the codebase more modular and testable.
Applying functional concepts to SOLID principles
- Single Responsibility Principle (SRP):
The SRP states that a class or module should have only one reason to change. In functional programming, this principle is naturally adhered to by ensuring that functions have a single input-output relationship and avoid side effects. Juan explained how data classes and monadic error handling in Kotlin can help achieve SRP, allowing developers to maintain a clear separation of concerns. - Open-Closed Principle (OCP):
OCP advocates that software entities should be open for extension but closed for modification. Juan demonstrated how functional programming allows for this by using high-order functions and dependency injection. By abstracting behavior into functions that can be passed around and modified without changing the underlying implementation, developers can add new functionality without risking existing code stability. - Liskov Substitution Principle (LSP):
LSP ensures that objects or parameters can be replaced with subtypes without affecting the correctness of the program. Juan discussed how functional programming supports this principle by promoting immutability and encapsulation, preventing unexpected behavior when subtypes are introduced. - Interface Segregation Principle (ISP):
ISP emphasizes that no client should be forced to depend on methods it does not use. In functional programming, this is achieved by abstracting dependencies and passing only what is necessary through high-order functions. Juan illustrated this with an example from a Kotlin codebase, where the view model interacts with a repository without needing to know its details, thus adhering to ISP. - Dependency Inversion Principle (DIP):
DIP suggests that high-level modules should not depend on low-level modules, but both should depend on abstractions. Juan highlighted how functional programming naturally supports DIP through the use of functional interfaces and high-order functions, allowing developers to invert dependencies and reduce coupling between modules.
Navigating side effects and immutability
One of the challenges in functional programming is dealing with side effects, which are often necessary in real-world applications. Juan tackled this by discussing the concept of side effects in the context of locking and unlocking a car. He explained how side effects can be isolated and managed, ensuring that the functional core of the application remains pure and easy to reason about.
Immutability, another key tenet of functional programming, also plays a crucial role in maintaining software integrity. By defining fields as immutable, developers can prevent unintended changes, leading to more predictable and stable code.
Bridging object-oriented and functional paradigms
Juan’s presentation concluded with a discussion on the practicalities of combining object-oriented and functional programming. He emphasized that most modern programming languages support both paradigms, allowing developers to choose the best approach for their specific needs. The key takeaway was that developers don’t need to strictly adhere to one paradigm; instead, they can leverage the strengths of both to create cleaner, more maintainable code.
Conclusion: A balanced approach to software design
Juan Chiaradia’s talk provided valuable insights into how functional thinking can complement and enhance the application of SOLID principles in software design. By embracing functional concepts such as immutability, function composition, and high-order functions, developers can create more robust, scalable, and maintainable systems.
As Volvo continues its journey towards becoming a software-centric company, the lessons from Juan’s presentation will undoubtedly resonate with developers looking to build the next generation of clean, SOLID, and functional software systems. Whether you’re a seasoned developer or new to these concepts, integrating functional thinking into your coding practices can lead to significant improvements in code quality and long-term maintainability.
Additional resources
- Juan Chiaradia: LinkedIn, X
- Functional thinking
- Benefits of Functional Programming