-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cachex function-names break Elixir naming convention #382
Comments
Hi @michielfbr! I originally wrote a huge reply to this but scrapped it to be more respectful of your time 😅. It's still fairly long, but I appreciate your thoughts on any of this! I broke it up into responding to your comments + my own, to try make it easier to follow. ResponseI am well aware of the problem; it was a hot topic on Twitter recently. Unfortunately, Twitter is quite simply the worst medium possible for actually having a conversation, so I'm glad you brought it to GitHub! FWIW it was polled there (not by me, so unbiased!) and the result was that it should not be changed. Having said that, I am totally willing to find common ground here - but there are concerns to address. What I want is a solution that takes the users into account. It's very easy to point at convention and say "your library sucks, fix this", but context does matter.
The core reason I chose
That being said, you are correct that it can be misleading in the case of
Yeah, these are the candidates I looked at so far. I originally wrote a full explanation of this, but it was largely pointless so I can summarize it:
Instead, rather than arguing about booleans specifically, I think we should be talking about revisiting the API convention in general. Maybe people didn't want to say "change all of it", but that's more compelling than just special casing booleans. ProposalI wrote the first version of the library almost a decade ago; Elixir was very new and not widely used. Nobody cared about convention, because the language was still growing. At the same time the approach I chose was also not based on the current Elixir audience, so maybe it's wrong in 2024! I defined the API because there is a case where a cache does not exist. So something like this: Cachex.get(:missing_cache, "key") I did not believe that crashing was the best approach here (I still don't, but I could maybe be convinced). So simply because of this, every call had to support So perhaps in 2024 this is simply the time to go through the API and do the following:
For the last point, I don't really like not having # removing the {:ok, _} syntax on fetch
case Cachex.fetch(:cache, "key", &String.length/1) do
{:error, reason} ->
# handle error during fetch
{:commit, value} ->
# handle newly generated values
value ->
# handle values already found
end
# keeping the {:ok, _} syntax on fetch
case Cachex.fetch(:cache, "key", &String.length/1) do
{:ok, value} ->
# handle values already found
{:commit, value} ->
# handle newly generated values
{:error, reason} ->
# handle error during fetch
end
Even in this case though, The problem with this is that the churn is quite high; it will affect literally every function in the I'm curious on your thoughts here, so please do let me know! |
As an aside, the conventions you linked would also have me rename Part of the slippery slope of conventions is that you should probably care about them all, or you might as well care about none. |
Hi @whitfin ! Thank you for sharing your thoughts so extensively! I'll move straight to responding to your proposal, trying to keep everything you mentioned in mind. But not before saying that "your library sucks, fix this" surely isn't the message I want to convey. On the contrary. The 3 changes you list, sound like a good approach to me.
Perhaps. Not entirely sure on non-! functions. Maybe return a self-defined exception-struct?
Yes :-)
Yes, but definitely not everywhere. As you say: "only where appropriate" And, although I feel some resistance on bang functions ( I realize that any of these changes have major impact on many existing users. Thus I totally get that, if applied, changes like these should be well overthought. And won't happen overnight in a minor patch. |
Elixir has a naming convention:
But I noticed that several Cachex functions, with a trailing
?
, don't return booleans. Instead they return booleans in an ok-tuple. Ie.{:ok, false}
I haven't checked every module, but in the main module I'm talking about
Cachex.empty?/2
andCachex.exists?/3
.In tests with non-existing cache, this results in false positives/negatives with assertions like these:
assert Cachex.exists?(:my_cache, :some_key)
refute Cachex.empty?(:my_cache)
I would suggest either returning plain booleans or removing the question-marks in the function name. Or even implement both flavours. I'd also be happy to submit a PR going for either solution, but first would like to hear others opinion on this.
Looking forward to your reactions!
The text was updated successfully, but these errors were encountered: