Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should F# type annotations be removed when refactoring?

In cases where I'm writing a function before it is used anywhere, I'm finding it useful to add type annotations to its parameters. This means I can autocomplete on its values and (especially as an F# newbie) won't get confused by unexpected type inference.

However, once the function is finished, I'm tempted to remove the parameter's type annotations because they're ugly. Does this sound like a reasonable thing to do?

I expect that it might depend on what kind of function I'm talking about. For example it might make sense for a private function, but not for a public one.

like image 700
Geoff Avatar asked Sep 02 '12 09:09

Geoff


1 Answers

I think it depends on lots of factors. Here are a few arguments in favor of leaving the annotations:

  1. Type annotations can arguably serve as a form of compiler-checked documentation.
  2. Don't modify working code without a good reason.
  3. Some type annotations may be necessary for compilation, in which case perhaps it makes sense to leave all of them rather than remove only the unnecessary ones for consistency.

However, on the other hand, there are also some compelling reasons to remove them:

  1. The compiler may actually infer more general types, in which case you can reuse functions in a broader set of contexts.
  2. Your code will be more concise, which you may find to be more readable.
like image 165
kvb Avatar answered Oct 23 '22 12:10

kvb