Summary of GopherCon 2023: Clean Up Your GOOOP: How to Break OOP Muscle Memory - Dylan Bourque
Key Points and Concepts:
-
OOP vs. Go:
OOP focuses on classes, inheritance, and complex abstractions, which can lead to code that is hard to maintain. Go is designed for simplicity and efficiency, emphasizing structs and interfaces rather than classes and inheritance.
-
Structs in Go:
Go uses structs instead of classes. Structs are simple collections of fields and methods, and constructors are optional. There is no inheritance; instead, Go uses embedding, which can sometimes resemble inheritance but behaves differently.
-
Packages and Visibility:
Packages are the fundamental unit of design in Go. All code must be defined within a package, affecting encapsulation and visibility. Poorly structured packages can lead to long build times and bloated binaries.
-
Common GOOOP Examples:
- Refactoring to Shared Components: This can create hidden circular dependencies and reduce cohesion.
- Interface Design: Defining interfaces in a way that closely mirrors OOP can lead to tightly coupled modules and unnecessary complexity.
- Project Structure: Adopting architectural patterns like Clean Architecture or MVC without adapting them for Go can lead to "goopy" code that lacks clarity and maintainability.
-
SOLID Principles:
While SOLID Principles are valuable, they need to be adjusted for Go. For example, focusing on package responsibilities rather than class responsibilities can help maintain clarity.
-
Best Practices:
- Avoid creating unnecessary abstractions and complex hierarchies.
- Define interfaces at the consumer level to promote decoupling and flexibility.
- Maintain clear and cohesive package structures that reflect functionality rather than type.
Conclusion:
Dylan Bourque encourages Go developers to unlearn traditional OOP practices and embrace Go's unique paradigms to avoid creating complex, hard-to-maintain code. He advocates for simplicity and practicality in Go programming.
Main Speaker:
- Dylan Bourque, Senior Engineer at CrowdStrike.
Notable Quotes
— 03:10 — « The thing with oo though is that we can end up fixating on defining abstractions and creating type hierarchies that implement them in a way that mirrors some real world scenario. »
— 03:46 — « Fizzbuzz Enterprise Edition uses all 23 design patterns from the classic gang of four book; everything is insulated from everything else so the code is maximally flexible. »
— 08:31 — « The go compiler is happy to accept T3 do base thing as an argument but it will call base thing's do stuff method, not the overridden method on the fancy thing. »
— 20:34 — « Stop it! You have an interface that redeclares every method on an implementation which means it needs to be updated anytime any new functionality is added. »
— 28:10 — « Go is a different kind of language though; blindly applying those past oo experiences and using those patterns and idioms without adjusting them for go can lead to a complex hard to maintain mess. »
Category
Technology