3 ms·
Err, this isn't bad advice, just kinda junior-level. First, if you're defining interface indirection to support encapsulation and you wind up never rewriting (
by carapace 7y ago
Err, this isn't bad advice, just kinda junior-level.
First, if you're defining interface indirection to support encapsulation and you wind up never rewriting (or adding another) implementation behind that "external function" or method, you probably didn't need it. (YAGNI, yo?)
Second, if you're defining interface indirection and it makes it harder to understand the code, you're doing it wrong anyway.
- - - -
Indirection that hurts is like when you do dispatch through a dict of functions and now all your tools, debugger, etc. can't tell you about the static structure of your code:
dispatch = {
'name': handler_func,
...
}
...
dispatch[selector](*foo, **bar)
That kind of thing seems like a good idea but:
> "Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?"
–Brian Kernighan, "The Elements of Programming Style", 2nd ed., chapter 2
(Also, notice that, in Python at least, derp.baf(foo, *bar) is the same thing, eh? EH??)
- keymone 7y agowell technically, if you type-annotate the dict you should be fine: dispatch: Mapping[str, Callable[arg_types, ret_type]] = {}
- carapace 7y agoThat's neat. I hadn't realized type hints had gotten that good now. Cheers! I've had issues reading code where I'm trying to trace control flow and I hit some dispatch point in the code and it's suddenly really hard to tell where to go from there. I do (did) a lot of Python programming and sometimes it's a little too dynamic, ya know what I mean? :-)
- keymone 7y agotype hints got good, editors and typecheckers didn't really :) personally i like these dispatches more than giant if/switch statements for no reason other than better separation and management of logic. as for debugging - stepping through code in ipdb is still the way to go.