Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In a functional application, should you wrap common higher-level functions in domain-meaningful names or should you leave them "bare"?

For instance, if you have a list of Addresses, and "sorted by zipcode" is a common domain-meaningful ordering (targeted mailers, etc.), is it preferable to write:

val sortedCustomers = customers.sort((a,b) => (a.zipCode compareTo b.zipCode) < 0) 

Or is it better to create a function sortedByZip(cs : Iterable[Customer]) : Seq[Customer]?

Creating the function has the advantage of being (minutely) more abstract, but has the disadvantage of being opaque, creating a name to remember, etc.

I'm asking in the context of a significant professional codebase, one that you intend to live for years, be as maintainable as possible, be "true" to the expectations of functional programmers, etc.

share|improve this question
4  
in haskell the right side would be sortBy (compare 'on' zipCode) customers :D (except ` instead of ') –  sparkleshy Feb 23 '12 at 18:20
1  
You can get backticks inside code spans: compare `on` zipCode. The trick is to start and end the span with two backticks, so single backticks are allowed inside. –  Tikhon Jelvis Feb 23 '12 at 20:43
add comment

2 Answers

It depends (but then, you knew that). If this call site is the only place customers are sorted, then I'd leave it as is for the moment. If the sorting code were more complicated (CC > 1), I'd definitely extract a function for the sorting criterion. If I had ten call sites like the one above, I would definitely have a sortedCustomers() function. Somewhere between 1 and 10 (really between 2-4, assuming I noticed the duplication), I'd extract the function.

share|improve this answer
add comment

I usually keep make a distinction between natural/canonical domain order and arbitrary order. If zipcode is a canonical order in this domain I would write the function as: customers.inNaturalOrder().

If you add a sortBy function you can have something like this: customers.sortBy(_.zipCode), which reads much better a

Minor nitpick, byZipCode reads like grouping to me, I would expect it to return a multimap of zipcode to customers.

share|improve this answer
    
Question edited to address your "byZipCode implies grouping" point –  Larry OBrien Feb 23 '12 at 23:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.