How long is too long for a callback function?

So for reasons discussed in more detail in this thread:

Implementing Dcc.Store on multi-page app.

I ended up writing a dashboard navigation callback with 25 Outputs and 10 State/Inputs for a bunch of nested and dependent radio buttons. The callback is currently 430 lines long. Which brings me to my question: Is this too long for a single callback? Does length impact speed in any way? Would it be better/faster if I were to split this single callback into, say, eight callbacks of around 50 lines each? Because almost every button depends on one or more Inputs from other buttons, there are a lot of nested if/else statements and I’m torn as to readability. Personally I think the single callback is easier to read because you can trace the values from top to bottom by simply following the logic. But is this bad practice? Any downsides?

Are you using the allow_duplicate=True for your outputs? This usually helps structuring the callbacks.

Concerning number of lines in your callback, I think it depends on what you do actually. If you are doing heavy calculations or something similar, it surely will impact the callback speed.

On the other hand, if transferring lots of data between-front end and back-end will slow things down more likely than some simple data manipulation in I e of the 450 lines of code.

I tend to separate all I can into single callbacks. In the end, if you have lots of inputs/states the have to travel front to back every time the callback is triggered- even though not all values might be needed for the triggering input

1 Like

There is almost 0 calculation. It is essentially one long nested if/else statement with ten Inputs dictating which and when Radio Outputs appear. If there is no specific issue with it, I’m inclined to leave it as a single. It is complicated enough as it is. My brain hurts thinking about splitting it up into individual callbacks.

I’m self-taught with no formal programming experience. I try to follow PEP conventions and use type hints, but due to the nature of my job and how the code is going to be used, it is highly likely that the poor schlub who is going to have to maintain this code once I’m gone will also have limited or no programming experience. So there are times where I purposefully skip best practices if I think it helps with readability.

1 Like