#How to have user inputs as object parameters?

28 messages · Page 1 of 1 (latest)

stray snow
#

I have an object that requires 10 parameters. What is the norm for doing this? Should they all be input in Main or should the be input that objects class?

normal escarpBOT
#

This post has been reserved for your question.

Hey @stray snow! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

mint canopy
#

Whenever you have an either/or, try and tell yourself you're wrong and what should be done is neither. Have a dedicated class for that.

stray snow
#

So a dedicated class for user input?

#

So this constructor is bad?

#

What would that dedicated class look like?

mint canopy
#

it's extremely bad mojo to put IO or interaction inside a constructor

#
public final class FlagInput {

  public static Flag fromUser(Scanner scanner) {
    // the steps here
  }

}
stray snow
mint canopy
#

Of course not, it's the other way around. It calls the constructor

stray snow
#

3-5 parameters

#

I did this entire thing to avoid it

#

Like constructors in general shouldn’t have more than 3-5 parameters

mint canopy
#

It's not untrue. Ideally you'd design subparts rather than have that many fields

#

You could also pick a minimum amount of fields that are absolutely necessary for the self-consistency of your class, and have the others set with setters

stray snow
#

I’ll think if that’s even possible

stray snow
#

@mint canopywhy is it a final class?

mint canopy
#

It's just that utility classes, classes that contain only static members, shouldn't be instantiated. To avoid that, they should be final (extending them should make no sense) and preferably they should make their constructor private so it isn't used.

stray snow
mint canopy
#

Doesn't matter, we were talking about my utility class

stray snow
#

Ty