Download - 5 Best Practices For F# Development
5 BEST PRACTICES FOR F# DEVELOPMENT
1. Define the Best Practice
2. Explain Why It’s Good
3. Provide Concrete Examples
HOW THIS WILL WORK
Prefer small functions with only one primary responsibility.
PROGRAMMING IN THE SMALL
1. Helps enable function composition
2. Follows the Single Responsibility Principle (SRP)
WHY IS IT GOOD?
1. FSharpCouch
2. IoC Container
EXAMPLES
Prefer function composition over argument passing.
FUNCTION COMPOSITION
1. Allows for function chaining
2. Makes the code terse yet readable
WHY IS IT GOOD?
1. Asynchronous Workflows
2. WebSharper 2010
EXAMPLES
Make sure your recursive functions are tail recursive.
TAIL RECURSIVE FUNCTIONS
1. Without it, there’s a risk for a stack overflow
2. Tail Call Optimization (TCO)
WHY IS IT GOOD?
1. Tail Recursive vs. Non-Tail Recursive
2. Polyphony (Join the Node Cluster)
EXAMPLES
VERIFYING TAIL RECURSION
NOT TAIL RECURSIVE
VERIFYING TAIL RECURSION
TAIL RECURSIVE
Prefer active patterns over multiple ‘when guards’ during pattern matching.
ACTIVE PATTERNS
1. Makes code more readable
2. Supports Don’t Repeat Yourself (DRY)
WHY IS IT GOOD?
1. Single-CaseExample: let (|UpperCase|) (x:string) =…
2. Multi-CaseExample: let (|Odd|Even|) x =…
3. PartialExample: let (|DivisibleByTwo|_|) input
=…
4. ParameterizedExample: let (|MultipleOf|_|) x input = …
- http://blogs.msdn.com/b/chrsmith/
TYPE OF ACTIVE PATTERNS
1. Example of Active Patterns (XML parse)
2. Node Join in Polyphony (DHT)
EXAMPLES
Prefer pattern matching to if/else syntax.
PATTERN MATCHING
1. Makes your code easier to extend
WHY IS IT GOOD?
2. Helps get you out of the procedural mindset
1. MVC Template (Account Controller)
EXAMPLES