0

Is there a way to enforce never divide (/) two int in C# at compile-time/VS/editorconfig/etc?

e.g.

var a = 1;
var b = 2;
var c = a / b; // NO!
var d = (double)a / b; // OK

Thanks!

Context: I am working on a legacy code and it was dividing two int values that rounded towards 0. I suspect there are other places that the same scenario exists. Instead of enforcing, I think it will still be very beneficial to analyze the code to show all occurrences of division of int and proactively fix the issue before it happens again.

Jan Paolo Go
  • 5,842
  • 4
  • 22
  • 50
  • 4
    I can't imagine you actually want to. Integer division is valid. Yes you have to understand what it does, but that goes for every line of code you write. – Crowcoder Jun 19 '20 at 17:49
  • I agree. I guess at least to show all occurrences of division of int? I'm working on a legacy project and we just had a bug because of this. I suspect there may be other places that this same bug exists. – Jan Paolo Go Jun 19 '20 at 17:51
  • I'm sure you could write an analyzer to identify that scenario, but this is what units tests are for. Is your code designed to be testable? – Crowcoder Jun 19 '20 at 17:53
  • I'm just curious... someone divided an integer and that caused a bug? Can you explain that? – John Wu Jun 19 '20 at 17:54
  • Unfortunately, most parts of the legacy code are **not** testable. – Jan Paolo Go Jun 19 '20 at 17:56
  • @JohnWu on the example above, `c` is still an `int` and is actually `0`. See also [docs](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#integer-division) – Jan Paolo Go Jun 19 '20 at 17:59
  • @JanPaoloGo just stop using `int` then. – Franck Jun 19 '20 at 18:06
  • You can certainly write a [Roslyn analyzer](https://learn.microsoft.com/dotnet/csharp/roslyn-sdk/tutorials/how-to-write-csharp-analyzer-code-fix) that detects integer division and then crank the message it gives up to an error (or at least promise to never ignore the warnings), though there is a learning curve associated with that. You're probably much better off simply searching for all occurrences of " / " in your code (a slash surrounded by spaces), i.e., find all uses of division and inspect them manually. Most code bases do not heavily use division. – Jeroen Mostert Jun 19 '20 at 18:15
  • 1
    Possible duplicate of [How to be warned about potential arithmetic errors due to type conversion](https://stackoverflow.com/questions/13840428/how-to-be-warned-about-potential-arithmetic-errors-due-to-type-conversion) – John Wu Jun 19 '20 at 18:15
  • Also, *all* code is testable (trust me on this); it's just a matter of how difficult it is to set up. Things like [Microsoft Fakes](https://learn.microsoft.com/visualstudio/test/isolating-code-under-test-with-microsoft-fakes) can be a huge help in isolating code without changing it, then writing tests to ensure the semantics are preserved, then refactoring it with the tests as a backstop. Things like LocalDB can be used for databases. Takes a lot of effort, sure, but that still beats staying with code that has no effective tests forever. – Jeroen Mostert Jun 19 '20 at 18:18
  • @JohnWu Thanks for finding the dup! – Jan Paolo Go Jun 19 '20 at 18:29
  • @JeroenMostert finding " / " is a good idea. I guess what I meant was that most parts of the legacy code are not *easily* testable. – Jan Paolo Go Jun 19 '20 at 18:29

0 Answers0