Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which kind of method signature do you prefer and why?

Tags:

methods

c#

.net

Ok, this is probably highly subjective but here it comes:

Let's assume I'm writing a method that will take a printscreen of some region of the screen. Which method signature would you prefer and why?

  1. Bitmap DoPrintScreen(int x, int y, int width, int height);
  2. Bitmap DoPrintScreen(Rectangle rect);
  3. Bitmap DoPrintScreen(Point point, Size size);
  4. Other

Why?

I keep seeing myself repeatedly implementing both 1) and 2) (redirecting one of them to the other) but I end up usually just using one of them, so there really is no point in having both. I can't decide which would be better. Maybe I should use the signature that looks the most with the method I'll be calling to make the printscreen?

like image 293
devoured elysium Avatar asked May 12 '10 16:05

devoured elysium


1 Answers

It depends. If this is a library for general use, by all means add all three. The .NET framework, especially GDI+, is full of examples of this. If the conversion is trivial, it's not much effort, and your users will be thankful.

If it's just an internal class, I'd go for the option that is easiest to use from the call site. For example, if the caller already has a Rectangle available, the Rectangle overload is better. If there are multiple call sites with different preferences, it's probably better to add multiple overloads as well.

like image 129
Thomas Avatar answered Nov 15 '22 06:11

Thomas