With currying, you do away with OO and parameter initialization altogether.
@curry
def SMTPSender(host, message):
...
emailSender = SMTPSender(host='localhost')
# partially evaluated fn with `host` fixed
emailSender(message=message) emailSender = SMTPSender(host='localhost') # in container
emailSender.send(message) # in implementation
The consumer of the SMTPSender only interacts with the send() method.An email doesn't send itself nor does it interact with the transport. The transport (MUA/MTA for example) is responsible for delivering it via whatever method it happens to support. The container delivers the transport to the code. The consuming code doesn't care what the transport and method does, merely introduce the two and let them make sweet email love.
The article stinks of circa 2002 purist babble.
It's hard to swim the trends.
I agree. By the way, you can do that even in imperative languages. I do it all the time in Python or Go.
Only if you decide to change the signature, there are other ways to achieve the same result.
(let ((*emailer* mailgun-client))
(send email))
Or, using Go contexts (which are just a way of hacking in dynamic variables): ctx = email.NewContext(ctx, mailgunClient)
email.Send(ctx)
And in email.Send: emailer, ok := EmailerFromContext(ctx)
if !ok {
emailer = defaultSMTPEmailer
}Is this approach is so dangerous (as we're constantly told) why has it never caused me any problems? Maybe I'm missing something but I've never read an article that explains what it is I'm missing in a way that clicks.
Adding an optional parameter to an already existing method is not always a backwards-compatible change. For example, in C# this will effectively change method signature and break API.
http://haacked.com/archive/2010/08/10/versioning-issues-with...
http://haacked.com/archive/2010/08/12/more-optional-versioni...
...yet I've used this technique for years in VB.Net without issue....weird.
What if two threads need to send mail via SMTP and Mailgun...
Consider the "classical OOP" method signature:
email.send()
vs "the anemic way"
emailSender.send(message: Email)
If there is only one way to send email (SMTP), things works fine. But let's assume we have to implement another way (e.g. Mailgun API). In the "anemic" case everything is simple: extract interface from EmailSender, add new implementation, all other method signatures remain unchanged. Refactorings are simple and can be done easily in any modern IDE. However, in "classical OOP" way, we have to do something like:
email.send(sender: EmailSender)
Which changes Email::send signature, which leads to breaking API changes and extensive manual refactoring of the entire codebase.
The "anemic way" had won because it is much more reusable and maintains API stability. Incidentally, this approach have smoothed the transition to functional languages — "NounVerber" objects became modules (collections of functions), and value objects became immutable data types.