Mathematical operation on nullable

We are migrating some old code c# and most of double are becoming double? But the developper was a javascript programmer and there is a major difference between c# and javascript : 5 + null => 5 in JS, null in .net !

Thus we add a lot (a very lot of !) ?? 0 for each operation but we are not sure we do not forget some (and sure we forget some because we have some bugs and we need to correct), i see a lot of option in resharper, but i found nothing, there is an option to detect +/- ... with nullable type ?

7 comments
Comment actions Permalink

Hello Fabrice, thank you for your message. You can create custom patterns that will be used to find the needed structures. Custom patterns can be found here: ReSharper | Options | Code Inspection | Custom Patterns.

For example, you can create patterns for:

double? + double?
double? + double
double + double?

And the same for other operators and types.

Please let me know if you have any questions. Thank you!

1
Comment actions Permalink

Very thanks to you, with your suggestions, we found other bugs !

It creates a lot of rules, we add a rules with * too (it seems that + works with -, * with /).

We need to create a rule for +=, -=n .... with permutation !

0
Comment actions Permalink

Fabrice, happy to hear it helped! Could you please describe the task with +=, -= in more detail? Thank you!

0
Comment actions Permalink

 

But for this project, we finally "disable" compound statement, less rules to create ! (because there is *=, /= too)

But we found other problem like :

 

Unable to find this, event if we change double to object in the rule.

0
Comment actions Permalink

Fabrice, try using something like this? Can it work for you?

0
Comment actions Permalink

It is too general expression !

0
Comment actions Permalink

I understand, but unfortunately, based on your example, I can not offer anything else yet. The fact is that in the previous screenshot "a" is used both as an identifier and as an expression. Therefore, when creating a custom pattern, such a scenario cannot be implemented, since for "a" we can specify only one "meaning".

0

Please sign in to leave a comment.