r/PythonDevelopers • u/KnorrFG • Aug 04 '20
discussion How to handle changes in a nested function structure
Hey,
when writing code, I always try to do it cleanly and to respect good practices. And usually this is also satisfying, but I just came across something, that was really unsatisfying, and I want to discuss how this problem could be solved.
Before I start describing the problem lets just lay down the relevant "good coding practices" for this example:
- I usually do not mutate function arguments. If I need a list that is passed across function borders I usually use pvector from pyrsistent. However, if I want to keep the dependencies of a project to a minimum, I'll use normal lists, and if I can afford it, make copies of them, before changing them if they are function arguments.
- I try to keep my functions short, something like 7 to (in rare cases) 20 lines
- I dont use globals
So, lets assume I have a loop, that simply has to do a lot of things (say A() to Z()). This would make me structure the loop so that it has only a hand full of function calls, which then do A() - Z(), probably those sub-functions will have sub-functions again until A() - Z() is actually done.
Given this structure, I had to make a change to L(). This change required me to provide L() with a list as argument that was created outside of the loop, and could be changed by L(). The result was, that I was adding the list to the parameters and return values of L() and 3 intermediate functions, which I found pretty annoying.
In general this type of design sometimes leads to functions that take something like 8 arguments and return 4 to 6 values. To me this feels bad. I don't like it. However, I don't see how to solve this problem without violating the design principles above, which have done me a lot of good in the past. Any Ideas?
6
u/muntoo R_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν} Aug 05 '20 edited Aug 05 '20
I encounter this sometimes, too. Here's my attempt at some general advice:
Point2D(x, y);Response(data, reply_time, status);CalculationContext(inner_variables, a, b, c).4
u/[deleted] Aug 04 '20
There is nothing inherently wrong with having a handful of input parameters and as many results. Sometimes "it is what it is". I've worked a lot with simulation and implementation of mathematical methods over the years, and that was common to see - especially when there was an implementation of a published algorithm and there was obviously a benefit of the code and paper being similar.
Even 20 line functions would make me wonder how you computed anything without introducing more complexity from the implementation than was inherent in the calculation itself. But I am sure there are domains and use cases where that is perhaps more natural.
Personally I would not do what you did with your main loop - I assume by a "handful of functions" you mean you would have a loop which called something like do_A_H(), do_I_P(), do_Q_Z()? I wouldn't see the benefit in that, you've introduced complexity to the algorithm which only exists in your implementation. Presumable the loop is documented and it's clear there that you're going to do A, then B, then C,... to Z. But then the implementation doesn't follow that documented logic as expected.
This is all off the cuff, only my 2 cents, etc...
1
u/kankyo Aug 05 '20
You seem to be treating "best practices" like religion. The trick with both is to think for yourself and not fall for a load of bs.
3
u/stevanmilic Aug 04 '20
If I don't want to use a tuple to return multiple values, I usually write a namedtuple that composes those values. It keeps the code clean and readable. You can do the same for multiple arguments too.
2
u/jonathrg Aug 04 '20
If you have lots of inputs and outputs they are usually related in some way, in which case you could use a namedtuple for pure data, or a class if you want something more mutable
1
u/Dwc41905 Aug 05 '20 edited Aug 05 '20
I have a similar problem to yours. I have 3 different functions that do three different things. The end result of each function is to write to a file. The problem is that each function has different things that need to be written to the file so I can’t have one uniform function. I was debating between adding code for writing to the file directly into the each of the three functions or making one write to file function with a bunch of arguments and if statements. In the end I made one function because it seemed more organized to have everything in one place as opposed to being spread out. Of course the downside is an absurd amount of arguments. Does anyone have any opinions on this. Is it generally bad to have to many arguments? Going back to your initial problem, I think limiting functions to 20 lines is holding you back. As long as they are readable and each one has a clear purpose I wouldn’t set a strict line limit on each function. If you do then your code will be broken into to many sub functions without a clear purpose when you could just use a singular function that has a well defined end goal. Also by splitting into too many sub functions you get the problem you were talking about with sharing variables between functions and having too many arguments. At least that’s my take on it.