Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming techniques: Passing objects or values as method parameter

Passing parameters is common in daily programming, but should we pass parameters as object or values?

(A)

public boolean isGreaterThanZero(Payment object) {
    if (object.getAmount() > 0) {
       return true;
    }

    return false;
}

(B)

public boolean isGreaterThanZero(int amount) {
    if (amount > 0) {
       return true;
    }

    return false;
}
like image 683
ilovetolearn Avatar asked Dec 07 '22 00:12

ilovetolearn


1 Answers

Neither of those.

With proper OOP, you would have isGreaterThanZero() on the Payment object, ie:

class Payment {
  int amount
  public boolean isGreaterThanZero() {
    return amount > 0
  }
}

and then:

Payment payment
boolean flag = payment.isGreaterThanZero()
like image 146
Reverend Gonzo Avatar answered Dec 28 '22 23:12

Reverend Gonzo