3 ms·
> [than] having a function which calls out to lots of other functions and I'm jumping around the code base, back and forward. i agree with longer functions and
by dijksterhuis 2y ago
> [than] having a function which calls out to lots of other functions and I'm jumping around the code base, back and forward.
i agree with longer functions and less jumping around, but there's also some nuance i find. I sometimes find converting a complicated multi-line condition into something like the below is much easier for me to read, so long as the function is named in a clear way and the function definition is just above the big function it gets called by (never in a different file!)
def is_something_valid(something, foo, bar):
return something > 0 and something != foo and something > bar
if is_something_valid(something, foo, bar):
it's like a convenience reading shortcut so i don't have to parse the logic in my head if i don't want to. also makes the condition nice and easy to test.
then again, can also do it the grug-brained way
gt_zero = something > 0
ne_foo something != foo
gt_bar something > bar
if gt_zero and ne_foo and gt_bar:
- jabiko 2y agoI think you are making a good point but if this function is only used in one place I would personally prefer to just use a variable: something_is_valid = something > 0 and something != foo and something > bar if something_is_valid: # do stuff That way you can document the intention of the condition using the variable name while at the same time keeping the locality of the check.
- robert_dipaolo 2y agoI mostly agree, but for short one liners and where there will be no reuse elsewhere, instead of a function I prefer; something_is_valid = something > 0 and something != foo and something > bar if something_is_valid: # .... It achieves the same thing without needing to scroll.
- jodrellblank 2y ago> can also do it the grug-brained way This way reads like: x = 1 // set variable x equal to 1 in that gt_zero echoes what the > operator does and says nothing about intent. Comparing, e.g. gt_zero = space > 0 // there is some space I guess? space_for_logfile = space > 0 // oh, logfiles need space > 20 there's the mistake.
- dijksterhuis 2y agohttps://grugbrain.dev/#grug-on-expression-complexity https://grugbrain.dev/#grug-on-expression-complexity i skipped off the `space` in `space_gt_zero` because i was on my phone and couldn’t be bothered to type it out all the way each time. don’t read too much into it. it was just laziness while brining up an existing concept.