Returning double.NaN in my custom function: is it a good practice?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Returning double.NaN in my custom function: is it a good practice?



(There is a kinda similar question about returning NaN but it does not answer this)



I'm a beginner coder (therefore always unsure about what is a good practice) and I'm writing a method that calculates the direction angle of some object moving in 2d space (it's for a Codingame challenge). For this, I wrote a method


public static double GetAngle(double deltaX, double deltaY)

if (deltaX==0 && deltaY==0) return double.NaN; //if not moving the direction is undefined
//...
//calculations etc
return result;



Is it a good practice to return a NaN like that? Seems convenient and logical (the object does not move, hence no speed, hence no speed vector), but somehow I feel that it could lead to some hard to discover bugs later when this method is used in other calculations (of course I will use a NaN check where I remember but still). If necessary, this method could return 0 instead of NaN and I could live with that.





You could use something similar to the NET TryParse methods and return a bool
– Plutonix
Aug 10 at 14:14


TryParse





Thanks, I'll read about that. Though I am thinking a function should not return two different types depending on conditions (if I understood you right).
– sinepuller
Aug 10 at 14:39





Int32.TryParse Method Microsoft thinks it is ok
– Plutonix
Aug 10 at 14:46






I must've misunderstood, I thought you meant for me to put a TryParse in my function's return statement so it would normally return a double and sometimes a boolean.
– sinepuller
Aug 10 at 15:39






No, more like use that as a pattern: bool TryGetAngle(double X, double Y, out double angle)
– Plutonix
Aug 10 at 15:44


bool TryGetAngle(double X, double Y, out double angle)




2 Answers
2



While in some ways it's logical, I'd suggest that a more idiomatic approach would be to use a Nullable<double>, also written as double?:


Nullable<double>


double?


public static double? GetAngle(double deltaX, double deltaY)

if (deltaX == 0 && deltaY == 0)

return null;

// ...
// Calculations etc
return result;



That's likely to be more consistent with how you express "non-results" for other code.



Aside from anything else, the fact that the return type is double? rather than double will force callers to think about the no-result option - whereas it's very easy to call a method that returns NaN and end up propagating that NaN value elsewhere accidentally.


double?


double





"Aside from anything else" => probably THE reason to use it this way,
– Paul Palmpje
Aug 10 at 14:24





Oh! I didn't think about it that way. I read that doubles are not nullable before so I thought I should use NaN (which, I thought, is sort of a null replacement for this stuff). Thank you!
– sinepuller
Aug 10 at 14:32



I often return null if no result is found, and stick to always using null. If you cant return null here then use double.NaN





Thanks, that was my intention before I learned about non-nullable types. I think that double? from Daisy Shipton's recommendation would work ideally here.
– sinepuller
Aug 10 at 14:36






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered