Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which style of return should I use?

This is related to conventions used in C#.

I've got a method that has two parameters (X and Y coordinates). These coordinates represent the position at which a "tile" may reside. If a tile resides at these coordinates, the method returns its number. If no tile resides at these coordinates, I'm wondering how the method should behave.

I see three options:

  1. Use exceptions. I may raise an exception every time Method finds no tile. However, as this situation is not rare, this option is the worst one.
  2. Do it the old fashioned C++ way and return -1 if there is no tile.
  3. Make the tile number a reference parameter and change the return type of method to boolean to show whether there is a tile or not. But this seems a bit complicated to me.

So, what should I do?

like image 952
undsoft Avatar asked Jun 05 '09 17:06

undsoft


1 Answers

You can return null, and check for this on the calling code.

Of course you'd have to use a nullable type:

int? i = YourMethodHere(x, y);
like image 69
Jay Riggs Avatar answered Sep 20 '22 17:09

Jay Riggs