levine":13e46je5 said:
Richard Berg":13e46je5 said:
Got funky, ill-defined business requirements? Try any number of higher-level programming techniques that make the DRY principle easier to implement; you'll thank me when the specs change (again).
A bit off topic, but can you expand on this? Really, all the information you care to dump would be appreciated
Let's say you want to refactor several places in your code that have similar (but not identical) business logic. Ideally you'd do it in such a way that one function (or perhaps one class) encapsulates a particular chunk of logic entirely, while remaining flexible enough to handle the special cases that real world customers & bugs have thrown at you -- a list that will no doubt grow in the future.
Common techniques include:
1) having the inputs & outputs be substitutable types. (fancy term: "parametric polymorphism")
*** in the statically typed OO world this means some form of inheritance. In Java/.Net in particular, interfaces >> superclasses
*** in dynamic languages, it usually means duck typing or prototyping
*** in languages without reified generics, you cry a lot
2) putting extreme care into the overall API design, such that:
*** every function is small, robust, well defined, and easy to unit test
*** the inputs & outputs of various functions are highly composable
#1 is where you find a lot of programmer holy wars, because the best known "patterns" all have drawbacks: annoyingly verbose boilerplate, compiler/platform limitations, or code that somehow becomes *less* maintainable after the so-called refactor.
#2 is similar to the classic Unix ethos. (whether or not *nixes actually achieve those ideals is another debate...). Everyone agrees with it in principle. Main problem is the sheer time & talent it would take to get a large, complex API into this state and keep it there through the codebase's evolution.
Functional programming^ does not change the fundamental picture. But it makes each technique more effective, and each compromise less painful.
** Taking anonymous fns as input & output offers similar flexibility to inheritance or duck typing, but with less overhead.
** Closures can encapsulate local state, kind of how object instances do, but without the temptation to make that state mutable. Screwing with locals inside a lambda looks, "feels", and often behaves wrong -- as it should -- while screwing with class member vars is practically encouraged.
** Coroutines (eg lazy iterators, continuations) make it easier to put logically independent code into small, independent functions without turning your control flow into spaghetti
** Applying a more math-y, function-based, set-aware style of problem solving will help just about any CS project approach the "clean API" ideal. (ok, this one is pure subjective opinion, sue me)
** The techniques themselves are composable too. You can write an inheritance hierarchy whose methods accept/produce functionals. Iterators can have loose/duck typing. Etc etc.
Hmm. I realize my elaboration became just as abstract as my initial statement -- perhaps moreso. Was the concrete intro enough to understand what I'm getting at, or did I fail miserably? I know I could not write
Think Like a Computer Scientist nearly as well as its author did, despite agreeing 100% with his goals and methods.
^by which I refer to style more than language purity....though some languages make this style far easier!