Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm struggling with how to name this method, I don't like the "set" prefix, because I feel it should be reserved for normal "dumb" setters and some tools might not like it (i did not check it in checkstyle, pmd, etc., but I got a feeling they won't like it.)

for example (in java, but I feel its language agnostic)

public void setField1Field2(String field1, String field2) {
    this.field1 = field1;
    this.field2 = field2;        
}

The only purpose of this method is ONLY to set, this method is needed and cannot be joined with any other (because of framework used).

share|improve this question
I've put the java tag back in as this question isn't really language-agnostic, it's only really language-that-uses-getters-and-setters-agnostic, since not all do. *8') – Mark Booth Nov 14 '11 at 17:29
2  
How about setField(ClassContainingTwoStrings field)? – Patrick Nov 14 '11 at 17:30
@IAdapter: you need to get out (of Java) more often. This is not at all language-agnostic. Relatively few languages commonly use 'get' and 'set' methods. – kevin cline Nov 14 '11 at 19:14
Hi IAdapter, what you should name your variables isn't really a good fit for the Stack Exchange format: it's more of an invitation to bikeshed. – user8 Nov 14 '11 at 19:34
@Mark Trapp I knew I should ask this on stackoverflow, they are more open with this kind of questions. – IAdapter Nov 14 '11 at 20:39

closed as not constructive by Mark Trapp Nov 14 '11 at 19:34

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.

4 Answers

setThings where Thing is replaced with a word describing what the two fields have in common. The plural is the important bit. If you can't come up with a good word to specify why those two values need to be specified together look at where you are planning to use it, and find something else in common with them to add to make it more descriptive.

share|improve this answer

Being able to change the internal representation is the whole point of using a method instead of a public field. There's nothing wrong with it.

share|improve this answer
+1, If you're going to just have dumb setter and getter methods for a variable, it should just be public. – Malfist Nov 14 '11 at 14:33
Actually the Whole Point of Setters in particular was that Borland needed a way to mutate objects generically for their GUI builder and couldn't get the language updated in time to do so, so they invented the setter/getter pattern. Beyond that it's used by people who haven't quite gotten into the OO mindset who grabbed onto the pattern as a way to accomplish their goals without committing to OO (Eventually they figure out that you ask an object to do something for you, you do not ask it for it's data and do something with it). Getters are sometimes necessary, Setters should be shot on sight. – Bill K Nov 15 '11 at 0:01

I think the name of the method can be prefixed by the "set" keyword because that's exactly what you are doing. Sure, you can choose other words such as "configure" or "apply" for example but my point is not on the prefix.

My real point is that I don't think it should be named "field1Field2Field3etc.", it should be something that really gives a hint on the real purpose of calling this method. For example, if it is a method for configuring two currencies that can be exchanged, it could be

public void setExchangeableCurrencies(String from, String to) {}
share|improve this answer

OK, this is tagged language-agnostic, but in the case of Java, aren't some bean utilities going to assume there is a property called field1Field2 if you have a setter named setField1Field2.

And, on a more general line of thought, shouldn't a method do one thing well instead of doing multiple things at the same time?

In case you want to set both parameters or none at all, you can let each method throw an exception and then handle it and roll back the changes.

share|improve this answer

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