0

Java doesn't support operator overloading , so when I want to plus_equals [ += ] two objects like this :

    // pseudo : a += b ; 

In Java I have to write :

    a . set ( a . add ( b ) ) ; 

Which can be wrapped in a method to more closely mimic the syntax of the "+=" operator to get :

    a . add_equals ( b ) ; 

When I try to do the same for class member variables like this :

    // pseudo : a.x += a.x ; 
    a . setX ( add ( a.getX() , b.getX() ) ) ; 

I encounter a problem - the getters and setters get in the way . I either have to directly access the augend ( a.x ) of the addition which means I have to make it public ...

    a . getX ( ) . add_equals ( b . getX() ) ; 

or I have to make a kinda confusing looking mutator method

    a . setX_add_equals ( b.getX() ) ; 

Is there an elegant solution to this problem that both :

( 1 ) - Doesn't interfere with the access modifiers of the member variables .

( 2 ) - Is intuitive in terms of readability .

In advance , thanks for your time .

TheRealFawby
  • 112
  • 8
  • 2
    1) It should be `increment` rather than `add_equals`. 2) Why not use `incrementX`. This way you would use the same pattern as `setX`, i.e. "method name" = "action" + "property". – fabian Aug 27 '14 at 11:49
  • That is a prefect solution ! Thank you . – TheRealFawby Aug 27 '14 at 11:52
  • Thanks for your help , that solution works for addition and subtraction , but what would you recommend as an "action" to describe mutators such as "plus_modulus" , "plus_multiply" , "plus_divide" ? – TheRealFawby Aug 27 '14 at 12:00
  • 1
    what's wrong with `remainderOfX`, `divideX` and `multiplyX` – Johannes H. Aug 27 '14 at 12:05
  • (ok, actually, `remainderOfX` might be missleading. but unfortunately there is no verb for "set to remainder of" or a verb for "do modulo") – Johannes H. Aug 27 '14 at 12:10
  • In the presence of statements like `c = a . add ( b ) ;` a mutator method of the format `a . divideX ( b )` is side effect ambiguous . i. e. It is not clear whether the method modifies it parameters . – TheRealFawby Aug 27 '14 at 12:36
  • That'S what javadoc is for. I think using ambigous names is, when in doubt, better than trying to use unambigous ones that get so complicated nobody gets them anymore at all. I mean, once you read the javadoc, I guess you can remember what the method does fopr the rest of the time. – Johannes H. Aug 27 '14 at 15:13

0 Answers0