jvermeir (5) [Avatar] Offline
#1
The refactoring done in the 'A night at the movies' looks fine to me, however I also think it is incomplete. I think it would be useful to elaborate on how the refactored code is supposed to be used.

If client code needs to send an email with a ticket, the following could be done:

sendEmail(bookSeat(nextAvailableSeat(d, m)))

This would make using the code really cumbersome: as a user of this code I just want a ticket emailed to a customer. One way of fixing that problem would be to add a method that combines all three methods into one and then we're back where we started, right?

I wonder if there's a better way (maybe I should just continue reading ...).
aslam.khan (50) [Avatar] Offline
#2
Re: incomplete example 'A night at the movies'
This ...
sendEmail(bookSeat(nextAvailableSeat(d, m)))

is useful if the client code is 100% guaranteed that
- there is always a seat available
- the customer is satisfied with the seat (does not want to change)
jvermeir (5) [Avatar] Offline
#3
Re: incomplete example 'A night at the movies'
Yes, I agree, it is exactly the example the book starts out with (only coded in an obfuscated way). The code sample in the book will also work only under the conditions you describe.

I finished reading chapter 2 yesterday, which helps a lot in understanding the idea better. The point seems to be that we're used to encapsulate stuff in methods. this tends to fixate a process, a use case maybe, in our code.

By providing clients of our code with the basic steps we allow reuse of use in different contexts.

would that be fair to conclude?
aslam.khan (50) [Avatar] Offline
#4
Re: incomplete example 'A night at the movies'
That is a reasonable conclusion. Part of the difficulty of design is that we are always left wondering how much responsibility should be left to the client code. I don't think this is an FP problem - it is a design decision that must be made in that specific context.

Later, when we get into higher order functions in more detail, we shall encounter function composition which your one line of code hinted at.

-- Aslam