beardgame / utilities

General C# (game) utilities library, containing a variety of useful types and algorithms
MIT License
28 stars 14 forks source link

Define code contracts #168

Open tomrijnbeek opened 5 years ago

tomrijnbeek commented 5 years ago

https://docs.microsoft.com/en-us/dotnet/framework/debug-trace-profile/code-contracts

Would love to have a play with this. I know we've been using DebugAssert. in TD, but it looks like the code contracts is a built-in way to do this. Not sure if we want to go as far as having contract classes (though it may be interesting for our collection classes, since we aren't testing them very well), but having some of the preconditions and attributes in there sounds cool.

At work we have some of these annotations (e.g. marking something as immutable or mustusereturnvalue), and it can actually help preventing bugs.

We'll have to investigate if there is a performance hit, but from what I can see this will only apply to debug builds, which should be absolutely fine.

Alternatively we could use the JetBrains annotations instead: https://www.jetbrains.com/help/rider/Code_Analysis__Code_Annotations.html. These actually look a bit more useful to me as there are many more different ones to choose from, but they are only useful for people using Rider or R#.

tomrijnbeek commented 5 years ago

It looks like the static checking of the original Microsoft Code Contracts project is no longer supported, which makes it seem like we'd have a good case using the JetBrains annotations instead.

Not sure if we want to also use runtime contract checking, but generally there are some subtleties involved with that, so that seems like a separate discussion altogether.