-2

Given a string variable variable, for example, the convention to expose it is:

public class SomeClass {

public String variable = "";
public String getVariable() {return variable;}
public void setVariable(String variable) {this.variable = variable;}

}

Which looks like:

class.getVariable(); // Gets "variable"

class.setVariable("Hello"); // Sets "variable"


Is the following convention considered bad style?:

public class SomeClass {

public String variable = "";
public String variable() {return variable;}
public void variable(String variable) {this.variable = variable;}

}

Which looks like:

class.variable(); // Gets "variable"

class.variable("Hello"); // Sets "variable"


Keep in mind that the established design pattern of fluent interface seems to suggest that one create objects using something like:

public class SomeClass {

public String variable = "";

public SomeClass(String variable) {this.variable = variable;}

public String variable() {return variable;}
public SomeClass variable(String variable) {return SomeClass(variable);}

}

Which looks like:

SomeClass sc = new SomeClass().variable("Hello"); // Instantiates a new SomeClass object with variable equal to "Hello".

Evidently, the word "set" is implied in the fluent interface pattern. (Yes, technically, the fluent interface returns the class instead of void when setting. This is not important.)

closed as primarily opinion-based by gnat, amon, Bart van Ingen Schenau, Giorgio, user40980 Feb 3 '14 at 0:28

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    There are different conventions. Choose one and stick to it. Some languages like Java prefer a specific convention but beyond that it's pure opinion. – amon Feb 2 '14 at 12:00
  • Too broad, this depends on the language so you can only answer this question in list form. – Dave Hillier Feb 2 '14 at 12:30
2

The first is fine getXXX, setXXX The second won't work as you can't have three members all with the same name.

As mentioned in the comments it all depends on the language. Stick with the general style guide of the language if there exists any.

AFAIK in

  • Java you have get_/set_ prefix while the field storing the value is private.

  • in C# you use uppercase Xxx for getter and setter and xxx for the backing field (if you have any)

  • in Go you have Xxx for getter and SetXxx for setter. The backing field is named xxx because lowercase makes it private.

  • in Dart you have to use _ to indicate that a field is private, this results in xxx for getter and setter and _xxx for the backing field.

These are just the few languages I used recently and they have all different conventions.

  • Java uses camelCase, and EL enforces this with its reflection process. – user40980 Feb 2 '14 at 15:25
  • It's a few years already I worked last time with Java. Thanks for clarification. – zoechi Feb 2 '14 at 15:51
  • I've seen lots of _ in C# too. – Silviu Burcea Feb 6 '14 at 6:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.