Skip to content

Instantly share code, notes, and snippets.

@liammclennan
Last active December 15, 2015 06:39
Show Gist options
  • Save liammclennan/5217817 to your computer and use it in GitHub Desktop.
Save liammclennan/5217817 to your computer and use it in GitHub Desktop.
Things Considered Harmful

C# things to avoid where practical:

Loops

Use linq when querying. For loops are better when mutating state but let's avoid that too. Recursion is also an option.

Conditionals

Null checking type conditionals can be replaced with null coallescion in a variant of the null object pattern:

(callback ?? ()=> {})();

Polymorphism allows types to replace explicit conditionals. Tell don't ask can also eliminate conditionals. Use imagination.

Switch statements (particularly nasty special case of conditionals)

Polymorphism or convert to a dictionary (declarative instead of imperative).

Statements (C# does make these hard to avoid :( ) (variable declarations are ok if they make code more readable)

Use language features that are expression oriented, like linq. Factor functions properly. Write pure functions.

Mutable state

Don't have it if you don't need it. Return copies of things.

State mutations

As above.

async

The TPL is great when you need to do something asynchronously, but it is viral in the sense that all callers become async too. Don't use async/TPL without a good reason and do it as shallow in the callstack as possible. Separate async / IO code from pure code

  • null
  • ref params
  • out params

Most of the justification for these exclusions is in my presentation Why Functional Programming Matters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment