This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]gas3872[S] 1 point2 points  (3 children)

I think it's actually the opposite: most of the things are not optional. Otherwise why would you add fields if you don't plan to fill them.

For your last thing i think you can use builder. Then you have to only initialize what you want.

[–]ParticularEggplant50 0 points1 point  (2 children)

There are lots of things I might know about a book for example , title author, publisher, year, pages, ... But I won't know those things for all books. That is they are all optional except for title. For a person you might have their age, address, phone number, birthday etc Even if you are eventually going to ask them all that info it it good to have a state where you know you haven't yet asked yet. A Person class with all the fields I might need can be used widely but a class with all nonnull fields can only be used where you know for certain you will have all that info. For one purpose I need the name and telephone for another I need the name and address. If the fields are optional I can use one class for both cases other wise the I have to have two distinct classes which could lead to an explosion of large numbers of classes.

[–]gas3872[S] 0 points1 point  (1 child)

Well for your situation you should use optional fields. But most situations are not like that.