by jaequery · 8y ago · 0 comments
a top-down approach would be clean and simple and the complexities are hidden away from interface:
User.create(data)
whereas, a bottom-up would be look like you just hacked things on top of each other:
create_user( validate_user_data(format_user_data(data) ) )
but, i do know lisp kinda forces you to code that way, so pg's article may have a lot to do with that.
create = User (validate . format $ data)
Only the create function needs be exposed, but the useful bit is that the create function is built out of the composition of smaller functions which makes it easy to change or completely swap out a component of the whole process. A top-down User.create function would likely be a rather large, monolithic chunk of code.
After a lifetime of programming, I actually prefer the latter approach: composing functions that are without side effects and independently testable.
I'm not sure if your examples reflect the top-down/bottom-up concepts in the original article, and certain don't agree that "a lot of jr developers" prefer the bottom-up approach, or that it looks "hacked on top of each other". Maybe it's just personal preference..
The one downside is the second style of code is a little uglier in languages without a good function composition operator, like Python. I enjoy writing stuff like
user = data
|> format_user_data
|> validate_user_data
in languages that support the pipeline operator.