NOVA-Monorepo icon indicating copy to clipboard operation
NOVA-Monorepo copied to clipboard

Standard Java getter and setter convention

Open RX14 opened this issue 10 years ago • 19 comments

We should either use getXXX() or XXX() consistently throughout NOVA.

RX14 avatar Aug 08 '15 17:08 RX14

I'm a fan of getXXX() myself.

kashike avatar Aug 08 '15 17:08 kashike

As scala dev (same as @calclavia) we were promoting xxx() if there is not setter and getXxx()/setXxx(value) in case that setter exists.

Kubuxu avatar Aug 08 '15 17:08 Kubuxu

I much prefer getXXX() and setXXX() because:

  1. It's java convention: this is not a scala framework,
  2. Groovy transforms getters/setters into properties

RX14 avatar Aug 08 '15 17:08 RX14

What we could do:

Use set and get for everything in NOVA. Create implicit wrappers in Scala.

calclavia avatar Aug 08 '15 17:08 calclavia

I don't know if that is possible only with implicit but some macro blackmagic might work.

Kubuxu avatar Aug 08 '15 17:08 Kubuxu

:+1: for @calclavia's suggestion. Groovy can mixin to classes, I assume scala can too? Implicit classes?

RX14 avatar Aug 08 '15 18:08 RX14

implicit class SomethingWrapper(peer: Something){

  def xxx: X = peer.getX
  def xxx_=(x: X) = peer.setX(x)

  def yyy: Y = peer.getY
  def yyy_=(y: Y) = peer.setY(y)
}

ghost avatar Aug 08 '15 18:08 ghost

import nova.Something
import novascala.SomethingWrapper

val something = new Something(xx, yy)
something.xxx = null
println(something.yyy)

ghost avatar Aug 08 '15 18:08 ghost

There is also a BeanProperty annotation in Scala, but it's work in other way, adds Beans getters/setters to val/var 's

ghost avatar Aug 08 '15 18:08 ghost

@anti344 problem is that you would need wrapper per class. Macros should be able to solve it.

Kubuxu avatar Aug 08 '15 18:08 Kubuxu

Not a macros then, full-blown scalac plugin, for converting field/field_= calls to getField/setField calls where possible. Also think of IDE support for this

ghost avatar Aug 08 '15 19:08 ghost

Macros are supported by all Scala IDEs and it is much cleaner solution as it is just code generation (it has nothing in common with C macros).

Kubuxu avatar Aug 08 '15 19:08 Kubuxu

From the scala docs I don't see a way to add methods with macros...

RX14 avatar Aug 08 '15 19:08 RX14

You cam write implicit classes/methods with macros.

Kubuxu avatar Aug 08 '15 19:08 Kubuxu

But how would you.., oh i see on IRC, m-kay

ghost avatar Aug 08 '15 19:08 ghost

Use overloaded XXX. XXX() for getter, XXX(YYY) for setter.

SoniEx2 avatar Sep 05 '15 21:09 SoniEx2

Alright, so I think we should settle for standard Java conventions. setX() getX()

calclavia avatar Sep 10 '15 18:09 calclavia

I agree with Calclavia, after all, NOVA is written in Java (primarily). This was already talked about a lot and whole NOVA team agreed that getXXX() and setXXX(YYY) will be used (correct me if I'm wrong).

On 10 September 2015 at 20:15, Calclavia [email protected] wrote:

Alright, so I think we should settle for standard Java conventions. setX() getX()

— Reply to this email directly or view it on GitHub https://github.com/NOVA-Team/NOVA-Core/issues/175#issuecomment-139332043 .

Caellian avatar Sep 10 '15 18:09 Caellian

I personally prefer variable() for getters of immutable (final) values and getVariable()/setVariable(value) for getters/setters of mutable variables.

ExE-Boss avatar Mar 03 '17 16:03 ExE-Boss