DEV Community

loading...

Discussion on: Back to basics: Naming

Collapse
nektro profile image
Meghan (she/her)

Functionally, I like the second one the best.
I also concur with the

if (email.isValid()){
    //do some stuff
}

However, to go with the object-oriented I'd prefer

public Email(String email) throws InvalidParameterException {}
Collapse
sadick profile image
Sadick Author

If we were building a lexical analyzer, then this could be one way of modeling that. But in this case we are not. Also note: the question is on naming not structure. How naming affects code readability

Collapse
nektro profile image
Meghan (she/her)

Naming wise, I like the second one. But if it was purely my choice I would not make a function to name in the first place and have the constructor do the validation during instantiation.

Thread Thread
sadick profile image
Sadick Author

Does this mean that you will have to create a class and do validation in the constructor for everything in your code that needs validation?

Thread Thread
nektro profile image
Meghan (she/her)

For high level objects that take arbitrary input, like an Email or URL, yes. Other things it's not always necessary because the type system ensures everything else is in order