Skip to content
This repository has been archived by the owner on Nov 8, 2018. It is now read-only.

Use Task.FromResult to return already computed results #3

Open
BillWagner opened this issue Nov 19, 2014 · 3 comments
Open

Use Task.FromResult to return already computed results #3

BillWagner opened this issue Nov 19, 2014 · 3 comments

Comments

@BillWagner
Copy link

I'm not sure how well to enforce this, because the anti-patterns are very diverse, and subtle.

During coded reviews I've seen code (often in mocks for tests) that use some form of task construction and execution (like Task.Run()) to produce a result when Task.FromResult() would have been much cleaner.

@sharwell
Copy link
Member

The obvious case is an async method that returns a result, but does not await anything.

@jaredpar
Copy link

Another obvious case is when the contents of the lambda body passed to Run are constant. In that case there isn't really a plausible argument for scheduling a task to return a constant value. Task.FromResult is a much better option

@andrewmurphyio
Copy link

@jaredpar that doesn't work when you're using a dictionary to cache a result

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants