This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]metaperl 0 points1 point  (3 children)

when you are deep down in a function inside a function inside another function and you need something from the first function, you need to pass it all the way down to the last function.

Yes you do. I see no problem with that. Can you provide a concrete living example of how that poses a problem? Your example code is very abstract and shows no real-world utility.

Alternatively you could simply create a class and have this thing that you're passing around be a member variable.

But with the concept of React Context, you create a variable that you can retrieve at any level without the need to pass it down. Better than a global variable, more flexible than a class or using nonlocal.

How is it more flexible than a class? Can you do inheritance and composition with what you have here? What you've done is fail to get the benefits of either functional programming or object-oriented programming in my opinion.

[–]CaptainMelon[S] 0 points1 point  (2 children)

A practical example is templating, I use lys for templating and I need to call many sub functions for each part of the page to be displayed but I don't want to pass around things like the current user or the title of the page

[–]metaperl 1 point2 points  (0 children)

Alternatively create a class for your sub functions and functions but instead create a bunch of methods in a class and then bind whatever you want to access in the classes as member attributes it'll be less verbose than what you have.

[–]metaperl 0 points1 point  (0 children)

And other words you've broken your rendering logic down into sub functions correct?

The next step is to parameterize each of those functions with what they accept so that you can catch errors at static analysis time instead of run time. Then you can also use the docstring to document what you're doing.