7

How is code commonly organized in FP applications? Along what lines do you separate files, modules, directories, etc.?

1 Answer 1

9

How to structure an application written in a functional language?

No differently than in any other kind of application:

  • Every module hides a secret.

  • A design decision that is likely to change should be encapsulated in a module.

  • A module might export a single main type, a couple of auxiliary types, and a whole bunch of related operations.

  • Module cohesion and coupling are still as important as Yourdon and Constantine said they were back in the 1970s—although in a functional language you seldom have to worry about "sequential" forms of cohesion and coupling.

  • Many functional languages require one module per file, but the ML dialects offer a great deal more flexibility. However the overwhelming practice is to put a module and its interface into separate files. Objective Caml enshrines this convention into their compiler.

In short, it's still true that representation is the essence of programming (Fred Brooks), and as in any other large application, your modules should be organized to avoid exposing representation gratuitously.

The one thing that's a little different in a functional language is that you might provide a module that encapsulates some pattern of execution as embodied in a higher-order function. For example, I wrote a module based on Bubble Search; my code takes any greedy algorithm and automatically converts it into a bubble-searchy algorithm. But a module like this, which is entirely about code and has no real data structure, is quite rare.

Sign up to request clarification or add additional context in comments.

1 Comment

The re-use in contrast to OOP does happen at the computation (function) level, not component (object) level.