Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is more performant, passing a method the entire object, or a property of that object?

Consider the following example.

I need to check if a CouponModel has a unique serial key.

I have two choices:

CouponModel model = GetFromSomewhere();

if (!CouponHasUniqueKey(model))
{
}

//or

if (!CouponHasUniqueKey(model.SerialKey))
{
}

Of course in the method where I pass in the whole object I would have to access the string property instead of working directly with the string.

Which option is better and why?

like image 384
Only Bolivian Here Avatar asked Dec 27 '22 02:12

Only Bolivian Here


1 Answers

I believe the performance will be the same.

I'd also prefer to give the responsibility to the object itself:

CouponModel coupon = GetFromSomewhere();
if (!coupon.HasUniqueKey()) {
  // ...
}
like image 149
Jordão Avatar answered Dec 30 '22 09:12

Jordão