4 ms·
Devils advocate: If you make this a strict requirement for your code base people write wrappers all the time, which creates a huge mess: def newMethod(x):
by qznc 9y ago
Devils advocate:
If you make this a strict requirement for your code base people write wrappers all the time, which creates a huge mess:
def newMethod(x):
if x*2 == 42:
return 36
return oldMethod(x)
You cannot enforce the requirement anyway, because people will just copy-paste the code and edit it under a new name.
You just cannot push this methodology unto developers.
- yuliyp 9y agoSo the point of a methodology like this is that it guides you into hopefully making better code by driving you away from certain decisions. Yes you can certainly sabotage attempts at doing so with patch methods and copy-paste, but the point is that if followed it'd set up an environment which discourages spaghetti.
- MattHeard 9y agodef foo_with_edge_case(x): if x == EDGE_CASE: return 36 return foo(x) seems clearer to me than stuffing the edge case handling into the foo method
- oneeyedpigeon 9y agoI don't think the parent was supposed to be production-ready code, it was more illustrative of a different (more interesting, IMO) point.
- thejosh 9y agoIf only you could pattern match x in the function def, would remove the need for this whole thing.
- wruza 9y agoAside from compile-time optimization, pattern matching is just this additional if at runtime. It is questionable if pattern matches make code more readable since when you see foo(42) call and def foo(42) is defined elsewhere, you can spend some time on original function until realizing that it’s not what you’re looking for. The same for type-based and operator overloading. It only makes code beautifu^W hard to guess. Though it does look nice for fib and fac.
- oneeyedpigeon 9y agoYou definitely could enforce it: isolate the developer from the codebase, give them a function contract to implement. It doesn't sound like a good idea, and I wouldn't want to work that way, but it could be done.
- Retric 9y agoWriting detailed method contacts is very much programming and has just as many risks for bug creation.
- virgilp 9y agoEven if you could.... why not modify a method? What's wrong with modifying it? It's only bad if you don't fully understand what it does, and/or all the ways/contexts where it's used. But, that's a sign, right? You can't ever hope to safely change that which you do not understand. If you're afraid of the codebase - I argue it's better to set aside time for making the codebase less intimidating, than to devise clever hacks/ "software process" for working around your lack of understanding. The latter will just make the codebase more intimidating, over time.
- reificator 9y agoAll other criticism of this wrapper approach aside, it does at least prevent a near worthless history. If you're forced to rewrite, that is.