5 best practices for f# development

22
5 BEST PRACTICES FOR F# DEVELOPMENT

Upload: dmohl

Post on 21-Jun-2015

2.675 views

Category:

Technology


1 download

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

Page 1: 5 Best Practices For F# Development

5 BEST PRACTICES FOR F# DEVELOPMENT

Page 2: 5 Best Practices For F# Development

[email protected]

www.twitter.com/dmohl

blog.danielmohl.com

DANIEL MOHLSOMMET GROUP

Page 3: 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

Page 4: 5 Best Practices For F# Development

Prefer small functions with only one primary responsibility.

PROGRAMMING IN THE SMALL

Page 5: 5 Best Practices For F# Development

1. Helps enable function composition

2. Follows the Single Responsibility Principle (SRP)

WHY IS IT GOOD?

Page 6: 5 Best Practices For F# Development

1. FSharpCouch

2. IoC Container

EXAMPLES

Page 7: 5 Best Practices For F# Development

Prefer function composition over argument passing.

FUNCTION COMPOSITION

Page 8: 5 Best Practices For F# Development

1. Allows for function chaining

2. Makes the code terse yet readable

WHY IS IT GOOD?

Page 9: 5 Best Practices For F# Development

1. Asynchronous Workflows

2. WebSharper 2010

EXAMPLES

Page 10: 5 Best Practices For F# Development

Make sure your recursive functions are tail recursive.

TAIL RECURSIVE FUNCTIONS

Page 11: 5 Best Practices For F# Development

1. Without it, there’s a risk for a stack overflow

2. Tail Call Optimization (TCO)

WHY IS IT GOOD?

Page 12: 5 Best Practices For F# Development

1. Tail Recursive vs. Non-Tail Recursive

2. Polyphony (Join the Node Cluster)

EXAMPLES

Page 13: 5 Best Practices For F# Development

VERIFYING TAIL RECURSION

NOT TAIL RECURSIVE

Page 14: 5 Best Practices For F# Development

VERIFYING TAIL RECURSION

TAIL RECURSIVE

Page 15: 5 Best Practices For F# Development

Prefer active patterns over multiple ‘when guards’ during pattern matching.

ACTIVE PATTERNS

Page 16: 5 Best Practices For F# Development

1. Makes code more readable

2. Supports Don’t Repeat Yourself (DRY)

WHY IS IT GOOD?

Page 17: 5 Best Practices For F# Development

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

Page 18: 5 Best Practices For F# Development

1. Example of Active Patterns (XML parse)

2. Node Join in Polyphony (DHT)

EXAMPLES

Page 19: 5 Best Practices For F# Development

Prefer pattern matching to if/else syntax.

PATTERN MATCHING

Page 20: 5 Best Practices For F# Development

1. Makes your code easier to extend

WHY IS IT GOOD?

2. Helps get you out of the procedural mindset

Page 21: 5 Best Practices For F# Development

1. MVC Template (Account Controller)

EXAMPLES

Page 22: 5 Best Practices For F# Development

[email protected]

www.twitter.com/dmohl

blog.danielmohl.com

DANIEL MOHLSOMMET GROUP