5 best practices for f# development

Post on 21-Jun-2015

2.675 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

F# makes it very easy to develop high performance, readable, and efficient code. However, like all things, a lack of discipline and best practice adoption can lead to a mess. In this talk we will cover 5 best practices that you can start using today to make yourself a better F# developer. We will explore each best practice, discuss the reason(s) that the described approach is preferred, and explore a few examples.

TRANSCRIPT

5 BEST PRACTICES FOR F# DEVELOPMENT

dmohl@yahoo.com

www.twitter.com/dmohl

blog.danielmohl.com

DANIEL MOHLSOMMET GROUP

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

dmohl@yahoo.com

www.twitter.com/dmohl

blog.danielmohl.com

DANIEL MOHLSOMMET GROUP

top related