How to organize my functions?

I’m new to F# and functional programming in general. I’ve been doing O-O for many years and I have an instinctive feeling for how to organize my code. With F#, I’m not so sure. I feel like I’m just littering my files with a bunch of functions, organized only such that all the references work out. I’ve done some organizing of functions in to namespaces and modules, but I haven’t developed a good feel for the best way to organize, or even when to organize. Can anyone offer any tips on dealing with code organization? When to use namespaces, modules within namespaces, etc.? Are there some good guidelines?

Thanks!

Firstly, you should remember that F# requires files (and code within a file) to be in dependency order.
And a simple tip that I always do:

namespace A
type B
module B =
// functions with type B

Also there is a post about it on F# for fun and profit

2 Likes

Thanks for the information and the pointer to the blog post on F# for fun and profit. There is so much good information on that site and I must have overlooked this particular post.

That is an option but I prefer:

module A.B // produces module B within namespace A

// types ...

// functions ...

This doesn’t enforce that functions can’t be defined in the section for types but it’s easy to do that by convention and to fix by moving code. It also means you don’t have to add another level of indentation to all your functions.

F# for Fun and Profit is a great resource but it contains many recommendations and suggestions that aren’t considered to be a best practice or necessary practice by the F# community as a whole, or even the author currently as his views have evolved over time (this is my understanding through hearsay, and I know Scott, he is a lovely man :slight_smile:).