Lesson 12. Organizing code without classes

So far you’ve learned all about relatively low-level elements of F#: language syntax, tuples, records, and functions, but you haven’t yet seen how to organize larger amounts of code that should logically be grouped together. In this lesson

  • You’ll review namespaces in F#.
  • You’ll cover F# modules, a way to statically group behaviors in a library.
  • You’ll see how to use both namespaces and modules within a standalone application.

Organizing code elements can be tricky in the OO world, not just in terms of namespacing, but in terms of responsibilities. We often spend a lot of time looking at whether classes obey concepts such as single responsibility, or moving methods from one class to another along with associated state. I think that the way things work in F# is much, much simpler. We’re not so worried about classes or inheritance or behaviors and state. Instead, because we’re typically using stateless functions operating over immutable data, we can use alternative sets of rules for organizing code. By default, follow these simple rules:

  • Place related types together in namespaces.
  • Place related stateless functions together in modules.

That’s pretty much it for many applications. So, the obvious questions are, “What are namespaces in F#?” and “What are modules?” Let’s take a look.

12.1. Using namespaces and modules

Let’s start by learning about the two core elements in F# for organizing code.

12.1.1. Namespaces in F#

12.2. Moving from scripts to applications

12.3. Tips for working with modules and namespaces