0

I would like to know which of these options is better in terms of clean (Java) code:

MyClass myObject = new MyClass(
    getParameter1(...), 
    getParameter2(...), 
    getParameter3(...)
);

or this one:

String param1 = getParameter1(...);
String param2 = getParameter2(...);
String param3 = getParameter3(...);

MyClass myObject = new MyClass(param1, param2, param3);
Bustikiller
  • 2,423
  • 2
  • 16
  • 34
  • Well if you need the parameters for something else besides creating an instance of `MyClass`, I would say the second option is the best choice. If not the first one is fine. – gonzo Nov 25 '15 at 14:16

2 Answers2

2

I'd prefer to use the first one. Cause with the second one you have 3 extra variables that are not used anymore.

Naxos84
  • 1,890
  • 1
  • 22
  • 34
  • 1
    Equally the 2nd one can be easier to debug if you're connecting up a remote socket debugger. Really a question of personal style more than anything though! – Mike Palfrey Nov 25 '15 at 14:39
  • @Mikey Palfrey you're correct. It's more a personal style thing. That's the reason i gave my own preference as an answer. – Naxos84 Nov 25 '15 at 15:10
0

I think it is a duplication of: Getter-Setter and private variables

Clean code does tell you to use getters and setters.

To give you the proper answer, you should describe the scenario.

Community
  • 1
  • 1
Franky
  • 11
  • 4