TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Maybe Functions

100 点作者 gitgud大约 1 年前

42 条评论

jmduke大约 1 年前
Agreed with this essay, and I think it rhymes with two others that I&#x27;ve found pretty influential over the past five years:<p>1. Parse, don&#x27;t validate (<a href="https:&#x2F;&#x2F;lexi-lambda.github.io&#x2F;blog&#x2F;2019&#x2F;11&#x2F;05&#x2F;parse-don-t-validate&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lexi-lambda.github.io&#x2F;blog&#x2F;2019&#x2F;11&#x2F;05&#x2F;parse-don-t-va...</a>)<p>2. Pipeline-oriented programming (<a href="https:&#x2F;&#x2F;fsharpforfunandprofit.com&#x2F;pipeline&#x2F;" rel="nofollow">https:&#x2F;&#x2F;fsharpforfunandprofit.com&#x2F;pipeline&#x2F;</a>)<p>In my experience, the &quot;best&quot; code (defining &quot;best&quot; as some abstract melange of &quot;easy to reason about&quot;, &quot;easy to modify&quot;, &quot;easy to compose&quot;, and &quot;easy to test&quot;) ends up following the characteristics outlined by the sum of these three essays — strictly and rigorously elevating exceptions&#x2F;failures&#x2F;nulls to first-class types and then pushing them as high in the stack as possible so callers _must_ deal with them.
评论 #39666916 未加载
评论 #39666991 未加载
评论 #39668445 未加载
评论 #39667217 未加载
评论 #39666967 未加载
评论 #39670948 未加载
moomin大约 1 年前
I feel like there&#x27;s a whole genre of essays (red vs green functions is the worst example) that could be summarised as:<p>* Monads naturally arise out of many problems in programming.<p>* But I don&#x27;t want my language to support monads.<p>* So here&#x27;s something you can do to stay in denial about how much you need monads.<p>At least this example only involves writing hard-to-analyse code and doesn&#x27;t lead to you trying to invent green threads.
评论 #39667280 未加载
cowsandmilk大约 1 年前
Can’t disagree more. Solution 1 just presents risk that some calls getUser() without doing the log in check. Then what happens?<p>It is false that getUser being a “maybe function” forces the other functions like getFriends to be maybe functions. Don’t let them take null in their arguments. Force the caller to deal with the null when it is returned by getUser.
bakhy大约 1 年前
This looks too easy, the first solution. If there is no logged on user, which User object is fetchUser going to return? Which friends? At the top level, if I were to forget to check if someone is logged in, who knows what would happen here.<p>I&#x27;ve worked on codebases where people were so allergic to the &quot;billion dollar mistake&quot; of nulls, that they created empty objects to return instead of returning null. This bit us in the ass a couple of times, e.g., when caller code was mistakenly passing the wrong ID variable into a fetch method, and just happily continued working and writing garbage into the DB, because it did not realize that its fetch had actually failed. It took data from the empty result object and happily continued its computation with it.
评论 #39668828 未加载
评论 #39668006 未加载
koliber大约 1 年前
Another option is Exceptions. The function either does what it&#x27;s supposed to, or freaks out.<p>You can remove the null checks and the software will raise a null pointer exception. In the first example, could raise a NotLoggedInException.<p>It&#x27;s still a maybe function, but you have a mechanism for expressing the why-notness of the function run, as opposed to returning a generic null.<p>As an aside, I prefer the &quot;Unless&quot; model of thinking vs the &quot;Maybe&quot; model of thinking. It&#x27;s biased towards success. It presumes that the function is most likely to do something unless a precheck fails. filterBestFriendsUnless vs maybeFilterBestFriends. getUserUnless vs maybeGetUser. If we go this far down the rabbit hole, we can assume there&#x27;s always an &quot;unless&quot;. Programs run out of memory, stacks have limited depth. There are maybe conditions for which we can not account.
评论 #39666700 未加载
评论 #39666951 未加载
评论 #39666875 未加载
评论 #39666732 未加载
bluetomcat大约 1 年前
The proliferation of conditional &quot;maybe&quot; functions is a sign that your call graph is contrived and unnatural. You shouldn&#x27;t be checking &quot;userLoggedIn == true&quot; in each and every accessor function. Ideally, such checks should bubble up towards the top of the call stack, and be performed once in an event loop iteration. The calling code should make sure that some basic prerequisites are met.
nyanmatt大约 1 年前
I use maybe functions a lot for things like &quot;maybeShowReminderDialog&quot;. The conditions for displaying the reminder are wrapped in this maybe function.<p>Surely that&#x27;s simpler than specifying those conditions before every call to show this dialog, resulting in plenty of duplicated code. And if those conditions change, there is only one place I need to update it.<p>Of course I can make a single operation to check those conditions like &quot;shouldShowReminder&quot;, but that too is doubling the surface area of this code.<p>I see the merit of the argument here but disagree with the absolutist stance against &quot;maybe&quot; functions.
评论 #39667583 未加载
jbandela1大约 1 年前
I would argue that the vast majority of functions in real world software are maybe functions in that they can fail. You need to be able to deal with failure. Not only can the user not be logged in, there can be a network issue, etc that makes even downstream functions fail.<p>Also, you have to deal with developer mistakes and what happens when they call incorrectly. This can be something as simple as getting the first element of a collection. What happens when the collection is empty? You can adopt the C++ approach of “undefined behavior” but it turns out to be dangerous.<p>Monads provide a nice disciplined way to dealing with this and composing together functions that can potentially fail.<p>Thankfully, newer languages such as providing support for monads and older languages are evolving features&#x2F;libraries for monadic error handling.
评论 #39666948 未加载
otter-in-a-suit大约 1 年前
Respectfully, I don&#x27;t think this articles uses monads correctly, because it&#x27;s not using any. This could be very elegant:<p><pre><code> getUser: Option[User] getFriends(u: User): Seq[Friend] bestFriends(f: Seq[Friend]): Seq[Friend] renderFriends(f: Seq[Friend]): Option[UI] &#x2F;&#x2F; Unit or type UI or HTML or ... </code></pre> Only `getUser` actually returns an option and is explicit about it. `renderFriends` could arguably do without.<p>To call, we can do<p><pre><code> bestFriends: Option[Seq[Friends]] = getUser.flatMap(u: User =&gt; renderFriends(bestFriends(getFriends(u)))) </code></pre> The render function could either gracefully render an empty list or error check as part of the `flatMap`, which takes the form of<p><pre><code> flatMap[B](f: A =&gt; Option[B]): Option[B] </code></pre> I really, really dislike it when functions signatures are lying to me, since `User` is clearly != `Option[User]` and `null` will not fit the type semantics of `User`, whatever those are.<p>And if you don&#x27;t _call_ it mondads (but rather something more approachable), it&#x27;s not that wild and scary sounding a concept all of a sudden.<p>That way, your compiler error checks null-type scenarios for you, your type signatures are clean, don&#x27;t lie, and your compiler forces you to explicitly do something like `runSafely` (or `runUnsafe` etc.), usually a single point of failure.<p>Bonus, `MonadError`-type constructs are awesome too, since I get<p><pre><code> handleErrorWith[A](fa: F[A])(f: E =&gt; F[A]): F[A] </code></pre> type functions (this is from cats in scala) to deal with errors explicitly.
评论 #39668525 未加载
lixquid大约 1 年前
Program logic fundamentally has to contend with different conditions. Sometimes the user will be logged in and have friends, sometimes they won&#x27;t.<p>The &quot;maybe&quot; style has the inconsistency embedded in the type system; it&#x27;s impossible to have an invocation to getFriends and then not handle the resulting possibility of not being logged in.<p>Shifting it up to the caller just means that you&#x27;re going to have to remember to ensure the user is logged in before calling getFriends otherwise you&#x27;ll get some kind of error, which might give you more control, but now there&#x27;s no guarantee in the type system that you&#x27;ve handled the case where the user isn&#x27;t logged in.<p>Writing ifs everywhere to handle failure conditions might be a bit of a pain, but that&#x27;s more of a failing of the language than the style.
评论 #39666934 未加载
tubthumper8大约 1 年前
I know it&#x27;s a simple example, but the Maybe class should probably use a null check internally rather than a truthy check, so that types like number and string which have falsy values that are nonetheless valid for a use case can be used with Maybe.
mrkeen大约 1 年前
The author is conflating two separate concepts, and calling them both &quot;the maybe function&quot;:<p><pre><code> 1) functions which do hidden things outside of their contract and&#x2F;or whose implementation doesn&#x27;t properly line up with their types. 2) functions which (by necessity) cannot always return the desired value and must return something else instead. </code></pre> &gt; The maybe function is a subtle monster that spreads it’s tentacles across the code-base.<p>This applies to both points 1 and 2.<p>&gt; It’s alternating functionality of “does&#x2F;doesn’t do something” makes code hard to understand, maintain and debug.<p>This only applies to point 1.<p>&gt; They seem to be trivial to add, but difficult to remove. But hopefully this illustrates the concern and ways to fix it.<p>This cannot apply to point 2, because you cannot take a function which might return a user and `fix` it to make it always return a user.<p>&gt; Solution 2 - Monads<p>This is not a monad.<p>He has implemented the Maybe Functor. <i>runSafely</i> most likely corresponds to <i>map</i> in whatever library you&#x27;re using, not <i>flatMap</i>.<p>This is visible from its type signature:<p><pre><code> &gt; runSafely(fn: (val: T) =&gt; V): Maybe&lt;V&gt; &#x2F;&#x2F; map </code></pre> which should be<p><pre><code> &gt; runSafely(fn: (val: T) =&gt; Maybe&lt;V&gt;): Maybe&lt;V&gt; &#x2F;&#x2F; flatMap </code></pre> And it&#x27;s also visible in the example function:<p><pre><code> function getUser(): User { if (!loggedIn) { return null } return fetchUser(); } </code></pre> ... which <i>still suffers from points 1 and 2</i>. Because it&#x27;s the same function which was highlighted as bad code at the top.
fbn79大约 1 年前
I think the post is related tothe &quot;Null vs Maybe&quot; problem. See <a href="https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20230329075114&#x2F;https:&#x2F;&#x2F;www.nickknowlson.com&#x2F;blog&#x2F;2013&#x2F;04&#x2F;16&#x2F;why-maybe-is-better-than-null&#x2F;" rel="nofollow">https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20230329075114&#x2F;https:&#x2F;&#x2F;www.nickk...</a> already discussed here <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=5577364">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=5577364</a>
评论 #39666980 未加载
TYMorningCoffee大约 1 年前
&gt; Here’s a specific example, it’s a “maybe” function as it only returns the friends of a user, if the user is logged in. Basically it introduces a possible return null.<p><pre><code> function maybeGetUser(): User | null { if (!loggedIn) { return null; } return fetchUser(); } </code></pre> I believe this is an error. The code sample I took from the article is about getting a user, not the user&#x27;s friends.<p>Since that function will return a list, an empty List might work.
aziis98大约 1 年前
I can&#x27;t wait for more languages to adopt the &quot;?&quot; operator [1] like the Rust one. It&#x27;s just syntactic sugar for &quot;if expr null return null&quot; but makes it far easier to write code in a more monadic style.<p>(mostly waiting for this in JS and Go)<p>[1]: <a href="https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;reference&#x2F;expressions&#x2F;operator-expr.html#the-question-mark-operator" rel="nofollow">https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;reference&#x2F;expressions&#x2F;operator-exp...</a>
评论 #39666935 未加载
评论 #39667328 未加载
评论 #39666900 未加载
croes大约 1 年前
Not everthing that can return null is a maybe function, sometimes you need a difference between zero and nothing.<p>Solution3 for their example:<p><pre><code> function maybeRenderBestFriends() { const user = maybeGetUser(); if(user!=null){ const friends = maybeGetFriends(user); const bestFriends = maybeFilterBestFriends(friends); return render(bestFriends); } return null; }</code></pre>
yellow_lead大约 1 年前
An alternative I&#x27;ve used or seen used in Java is to put @Nullable on the function. The caller knows the result could be null, and must check for it. Linters&#x2F;Static analysis can verify when you haven&#x27;t checked it as well.<p>There&#x27;s an urge to return Optional&lt;Object&gt; but now you must check Optional.isPresent AND object != null.
n4r9大约 1 年前
In C# the closest analogy I can think of is the &quot;Try&quot; pattern. For example, you have int.Parse(string) which returns int, and int.TryParse(string, out int) which returns bool. The fact that the returned value is the validation is a strong incentive to do something with it.
评论 #39667067 未加载
tuyiown大约 1 年前
This is a pattern you cannot always avoid, due to react, but i don&#x27;t think it should be normalized.<p>Two remarks:<p>• the render function is omitted, this pattern as a huge impact on application behaviors, if not for display-as-you-load issues, on DOM hidden state (things like focus, animations, etc…) for web apps.<p>• App&#x27;s do have a global state, with self-consistency, scattering it in a mixed match of loading cache and self contained components just make it hard to work with. I think it&#x27;s better to have a centralized upper level parent component that manage the transitional initialization states and consistency, not necessarily for the whole app, but at least for the whole displayed UI content.
Waterluvian大约 1 年前
One thing I like about typescript is that unless you’re a masochist, it basically pushes you towards option 1.<p>If you have to constantly check for null&#x2F;undefined it gets annoying and you naturally think about narrowing the state space so entire sections of your program don’t have to think about those possible states.<p>It should also become obvious when you have a possible null&#x2F;undefined state and it’s super unclear what that piece of your program ought to do about it other than alert the parent (such as throwing an error). If a component doesn’t have a role to play when null, maybe it shouldn’t ever be seeing null as a possible state.
ValentinTrinque大约 1 年前
You do have another solution that can lower the amount of conditions: Null Objects. These don’t fit every use cases, but they can allow you to express what’s missing, or not defined, or empty, and avoid nil pointers dereference or conditions to check the state.<p>As Sandy Metz is used to say « Nothing is Something »[0]<p>0: <a href="https:&#x2F;&#x2F;youtu.be&#x2F;OMPfEXIlTVE?si=qmizH1OvqV7eLKNK" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;OMPfEXIlTVE?si=qmizH1OvqV7eLKNK</a>
评论 #39667462 未加载
zubairq大约 1 年前
After reading this I realise that I am guilty of building many &quot;maybe&quot; functions in my own code. Definitely something I need to be aware of
aleksiy123大约 1 年前
I feel like this isn&#x27;t really a general statement on maybe functions but unnecessary maybe functions. If you can make it not &quot;maybe&quot; but &quot;always&quot; then of course that&#x27;s obviously better.<p>The real use case is when it really is maybe. (Network call, error handling). Then it&#x27;s about forcing people to deal with that in a typesafe way and not hiding that it really is maybe.
PartiallyTyped大约 1 年前
This is a great example of the issue with using monads and monad-like patterns in languages that don&#x27;t have proper support via language constructs for these.<p>In rust for example, this is trivially handled with the questionmark postfix operator — which is just sugar for match — whereas in languages like JS and Java, stacking Optionals and so on can be rather painful as all this sugar is done manually.
planede大约 1 年前
What seems that the fundamental problem is that the functions depend on global state that is not explicitly passed in (is the user logged in?). Maybe an explicit session parameter could work better here. You only have a session when the user is logged in, so you can&#x27;t even pass anything to the functions otherwise. It can of course be passed further recursively.
giankam大约 1 年前
I got stuck at option 1. Rendering code becomes lot more complex. Also few errors that make it difficult to follow the essay, like &quot;it’s a “maybe” function as it only returns the friends of a user&quot; but the function is getUser, not getFriends.<p>Or function getFriends(user: User): Friend[] { return fetchUser(); } The body of the function is wrong.
metalrain大约 1 年前
You can also handle optionality when using the value.<p><pre><code> function maybeRenderBestFriends() { const user = maybeGetUser(); if (!user) { return null } const friends = maybeGetFriends(user); const bestFriends = friends ? filterBestFriends(friends) : null; return bestFriends ? render(bestFriends) : null; }</code></pre>
noelwelsh大约 1 年前
I&#x27;m surprised the option (pun intended) that immediately came to my mind was not discussed: change the getUser function so it has a &quot;LoggedInUser&quot; parameter, instead of pulling the User from some global state. Then (so long as you have a type system) you can&#x27;t call the function without the user being logged in.
jupp0r大约 1 年前
Great point, but I want to bring up another one I&#x27;m seeing all the time:<p>Maybe functions that don&#x27;t have maybe in their name and just silently don&#x27;t do something without informing the caller.<p>This is extremely common and the source of many bugs. If your function is a maybe function, name it accordingly.
somishere大约 1 年前
And what if `fetchUser` hits an error? At the very least pop your de-maybeifier after the async call. Or use something language standardised (like a promise in JS where you can just throw).<p>I&#x27;m all for a perfy shortcut &#x2F; early return but this maybe just seems like an abstraction on a non-issue.
评论 #39666970 未加载
zem大约 1 年前
this is basically like bubbles under wallpaper - the &quot;maybeness&quot; is not due to your code, but due to the underlying problem you are solving (a user can either be logged in or not, and if they are not then all user properties are null). the article identifies the problem with various solutions as extra layers of abstraction, but i feel like the real issue is <i>ceremony</i> - whether propagating maybe-coloured functions through your code and checking the return value everywhere, or wrapping everything in a monad, you have to do something to handle the null case when all you really care about is the non-null case, and that something inevitably feels like clutter and overhead.
redleader55大约 1 年前
I find it very interesting that the higher they are in the stack, the more people tend to talk about algebra (monads, functors, etc). I wonder why is that the case? Doesn&#x27;t kernel or firmware require this level of abstraction?
wesselbindt大约 1 年前
That&#x27;s not a monad, that&#x27;s just a functor. And that&#x27;s great, because functors are easier to grok than monads! A functor F consists of two things: - a kind of function on types which transforms any type T to some new type F(T) - a rule which associates to any function f: T -&gt; S a function fmap(f): F(T) -&gt; F(S) That&#x27;s exactly what the author defines here, to a type T we associate Maybe&lt;T&gt;, and to a function f: T -&gt; S we associate `fmap(f)(x) = None if x is None else f(x)`.<p>A monad needs some structure in addition to fmap, namely bind and return. These allow you to take a function T -&gt; F(S) and a function S -&gt; F(U) and compose them together to a function T -&gt; F(U).
评论 #39670639 未加载
tobr大约 1 年前
You can address this with explicit parameterization instead of global state. That way the missing data is an obvious type error rather than a surprise in the middle of a running function.
dolmen大约 1 年前
Go has a related idiom: the &quot;Must&quot; functions<p><a href="https:&#x2F;&#x2F;pkg.go.dev&#x2F;text&#x2F;template#Must" rel="nofollow">https:&#x2F;&#x2F;pkg.go.dev&#x2F;text&#x2F;template#Must</a>
luc4大约 1 年前
To be pedantic: Their Maybe class is just a Functor, not a Monad.
评论 #39667329 未加载
brlebtag大约 1 年前
<i>maybe</i> the author does not know wtf he is talking about...
pavel_lishin大约 1 年前
What&#x27;s the advantage of the Monad approach? Doesn&#x27;t the render function still have to check whether those Maybes contain values or not?
评论 #39666807 未加载
评论 #39666870 未加载
skerit大约 1 年前
&gt; “Functions should do something, not maybe do something…”<p>But it did do something, it checked if the user logged was logged in first.
评论 #39669476 未加载
gardenhedge大约 1 年前
I think that naming functions with a prefix of maybe is awful. I treat all functions as &quot;maybes&quot;.
jolt42大约 1 年前
For collections, returning [] (empty collection) instead of null is almost always what I want.
botpag大约 1 年前
What on earth did I just read
评论 #39666643 未加载