this post was submitted on 26 Jul 2023
23 points (100.0% liked)

Programming

13375 readers
6 users here now

All things programming and coding related. Subcommunity of Technology.


This community's icon was made by Aaron Schneider, under the CC-BY-NC-SA 4.0 license.

founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] eskuero@lemmy.fromshado.ws 3 points 1 year ago (1 children)

Damm I think I might do some bulldozing sometimes, but is fair game when splitting into a different file for organization?

[–] luciole@beehaw.org 8 points 1 year ago (1 children)

Yeah I don't agree with "bulldozer code" bit. Splitting a long function into smaller parts doesn't have to be about reuse. It's useful to do it for clarity. As a single function grows, the number of local variable often grows as well and eventually you end up juggling tons of variables and the slightest change ripples three hundred lines further down. Break it down into a handful of steps with a number of arguments you can count on your fingers and it suddenly becomes so simple. Same about deep nesting of loops/conditions.

Rule of thumb is a function needs to fit in my head - so if I stick my face on the screen and the function's code can still be partly seen, it's time to break it down.

[–] phoenixes@beehaw.org 2 points 1 year ago* (last edited 1 year ago)

The main thing I encourage here is: If you're breaking up longer functions into more smaller ones that are really only used in this context, don't mix them into the same file as functions that are general use. It makes code super confusing to navigate. Speaking from experience on an open source project I contribute to.