4 ms·
> * Gradually pull out visually awkward chunks of code and put them into functions with no arguments at the top of the file. I have seen scientific code writte
by mbwgh 3y ago
> * Gradually pull out visually awkward chunks of code and put them into functions with no arguments at the top of the file.
I have seen scientific code written in this manner written in both Python and Fortran.
This may be some intuitive way to start off, and even complete the task at hand.
But for people trying to read, understand, and realistically, debug your code, this complicates things.
Because each no-argument function can only work via its side effects.
Your script becomes a succession of state transitions, and to understand it, you have to keep the intended state after each step in your head.
And in case of a mistake, you can't even call these functions individually via passing their intended arguments using the REPL. You have to set up all the state beforehand manually, call your no-arg function, observe the state afterwards. Which becomes more awkward the further down you are in your script, since all your dependencies are implicit and possibly even completely undocumented.
- raincole 3y agoThe correct way is: 1. Use comments to split visual awkward part into chunks. # a lot of code... ## === # more a lot of code... 2. Use inner functions if that chunk need to be reused 3. Only move the chunk to top-level function if you think it's worth to take time to make its required state into parameters/return value
- extr 3y agoSure that's like the worst case scenario, but in practice the entire point is that as soon as it becomes difficult to reason about, that's when you start cleaning up the functions and add some obvious OO. You don't leave it as a mess.