in reply to Anonymous subroutines

Update: I misinterpreted the OP as antagonistic; my response below is based on that misinterpretation. I redact the emotion, but I'll leave the content intact.

Recently, I've noticed a few places where anonymous subroutines have been declared for no apparent reason:

I urge you to take a step back when you look at code. Never assume. Just because you think something has been declared "for no apparent reason", doesn't mean that's factual.

I'm not going to argue this specific case, I just want to argue that just because you think something shouldn't be done a certain way doesn't mean someone doesn't have a reason for doing it that way.

An intermediate-or-above Perl hacker could list multiple cases where an anon sub could be useful in this use case.

TIMTOWTDI

I learned a long time ago in my early years that when I saw something that didn't make sense, I asked questions instead of making statements that things were done "for no apparent reason".

This is the first time I've ever admonished someone in my 20 years of being here, but I'm going to do so in a kind, respectful way...

I don't think I have ever written a stand-alone anonymous subroutine like this. The only place I generally use anonymous subs

*I*, *i*, *I*. See the repetition? Doesn't matter what you have done, reality is that you seem to have judged the code without understanding the full impact before comparing it to your own code. In that, you presume that your code is superior to that of the code you are judging, therefore you discard that code as inferior.

When I open my mind, I allow things that I couldn't have imagined to infiltrate. This way, I ensure I don't put code or people down due to judgement, I allow ideas to permeate my own, it promotes wide discussion and idea sharing and makes for better software.

Tolerance and acceptance first. If there's a "what the fsck?", ask questions. Don't condemn prior to investigation.

-stevieb

Replies are listed 'Best First'.
Re^2: Anonymous subroutines
by hv (Prior) on Dec 11, 2023 at 12:25 UTC

    I'm not sure what behaviour you are admonishing here. The OP did not say "for no reason", but "for no apparent reason" - it seems clear that by this they mean "for no reason apparent to me". They saw some code using a construct that they considered unusual, so they asked about it. I see no condemnation or judgement here, only a request for enlightenment.

      Spot on hv!

      The question was asked because I don't understand why one would write a sub like that. Having seen it twice in two very different settings from two different coders, I am either missing a reason to do it that way or it is how some people used to write code at one time. Both instances where I saw it are not recent bits of code.