Skip to content

Functional Options: Don't recommend closures#77

Merged
abhinav merged 7 commits intomasterfrom
abg/functional-option-structs
Dec 17, 2019
Merged

Functional Options: Don't recommend closures#77
abhinav merged 7 commits intomasterfrom
abg/functional-option-structs

Conversation

@abhinav
Copy link
Copy Markdown
Collaborator

@abhinav abhinav commented Dec 11, 2019

Per #74, this changes the recommendation for functional options to
suggest manual interface implementations instead of closures.

To make the example more demonstrative, I had to add a new option which
hurt the overall readability of the side-by-side comparison. This lead
to a minor reorganization: side-by-side includes user-facing API and
usage only, and implementation is demonstrated separately in its own
paragraph.

Resolves #74

This changes the Functional Options example to not recommend closures,
instead favoring full implementations of the interface for each option.

To help demonstrate that options don't have to be new types, I added a
logger option to the example implemented around a `struct`. This made
the examples too long so I dropped the timeout option: I wanted to keep
cache around to demonstrate readability of naked booleans versus
`WithCache(false)`.

I renamed the function to Open to further reduce horizontal space
consumed by the examples.
To keep the Bad/Good examples short and comparable, this change reduces
the examples down to just the user-facing API and how it's used.

The implementation is discussed separately in its own paragraph.
This aligns the usage examples for functional options Bad v Good in
their own row in the table.
CHANGELOG.md Outdated
@@ -1,3 +1,8 @@
# 2019-12-11

- Functional Options: Recommend individual implementations of `Option`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Recommend struct implementations would be more accurate (some options may reuse the same struct)

style.md Outdated

</td><td>

Options must be provided only if needed.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"must be provided" sounds a little odd, maybe just "Options are only provided if needed"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, yeah

style.md Outdated
</tbody></table>
Note that there's a method of implementing this pattern with closures but we
believe that the pattern above provides more flexibility for authors and is
easier to debug for users.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we provide more details here?

  • loss of comparisons (also makes mocks much harder)
  • loss of string methods for pretty-printing

@abhinav abhinav merged commit 1f4b461 into master Dec 17, 2019
@abhinav abhinav deleted the abg/functional-option-structs branch December 17, 2019 19:43
mmelnyk pushed a commit to mmelnyk/guide that referenced this pull request Oct 8, 2020
This changes the Functional Options example to not recommend closures,
instead favoring full implementations of the interface for each option.

To help demonstrate that options don't have to be new types, a logger
option was added to the example. This made the examples too long so the
the timeout option was dropped; cache was kept around to demonstrate
readability of naked booleans versus `WithCache(false)`.

Further, the function was renamed to Open and the Bad/Good side-by-side
comparison was reduced to just the user-facing API. The implementation
was moved to its own paragraph.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Guideline: For functional options, don't recommend closures

2 participants