Skip to content

Instantly share code, notes, and snippets.

@fogfish
Created November 12, 2024 21:00
Show Gist options
  • Save fogfish/14507ef9871c6611c4131306119f4e39 to your computer and use it in GitHub Desktop.
Save fogfish/14507ef9871c6611c4131306119f4e39 to your computer and use it in GitHub Desktop.
Option Pattern - Practical tips
Option Pattern Structs
Flexibility and Extensibility ✅ Provides high flexibility for future changes. Since options are typically functions that modify internal fields, adding new options in the future won’t require changes to existing struct definitions or method signatures. ☣️ Using structs with fields offers less flexibility for future extensions. Once a struct is defined with specific fields, adding new configuration options often requires creating a new struct or modifying the existing one, which could lead to breaking changes in the API.
Readability and Usability ✅ May be less readable if there are too many options, as users may not immediately know what fields are being set without referencing the documentation. However, for complex configurations, the option pattern can make code more expressive and readable by allowing named options. ✅ Provides clearer readability because users can see all configuration fields in one place. This makes it easier for users to understand what configurations are available.
Safety and Type Checking ✅ Since options are functions, they can provide stronger compile-time type checking and can validate values more dynamically when applied. For example, you can define each option function to accept specific types or check for valid ranges. ☣️ With structs, you risk users incorrectly setting fields, especially if some fields are related. You can use custom types for stricter typing, but it is harder to enforce constraints on field values directly through the struct.
Complexity ✅ Off-the-shelf implementation requires more boilerplate code. Each option function needs to be defined, and you need a method to apply these options to the final configuration. This increases code complexity and verbosity, especially if many options are needed. However, the ⎡🅾🅿🆃🆂⎦ library reduces the complexity. ✅ Simpler to implement as you define a struct and set its fields directly. This is often more straightforward and reduces the maintenance overhead compared to defining multiple option functions.
Encapsulation ✅ Supports better encapsulation. Since options are applied via functions, you can keep your internal state private, exposing only the necessary configuration APIs to the user. This can help prevent misuse by hiding certain internal details from the API user. ☣️ Users may directly modify struct fields, which can lead to unintended consequences if not carefully managed. While fields can be made private, that might limit usability or complicate the API.
Default Values and Optional Parameters ✅ Makes it easy to provide default values and only override those that are explicitly set by the user. It enables a “fluent” API style that is expressive and easy to customize without requiring many constructors. ☣️ You can set defaults within the struct directly or through constructor functions, but it can become unwieldy if there are many optional parameters or if defaults need to be conditionally set based on other fields.
Documentation and Discoverability ☣️ It can be harder for users to discover all available options, especially if the options are spread across various files or if the documentation doesn’t clearly list them all together. However, well-named option functions can improve discoverability. ✅ All fields are typically visible within the struct definition, which can make it easier for users to quickly understand all configurable parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment