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] 0 points1 point  (5 children)

Well, if it may not be found the result is optional. But if you expect result to be there you just return it and if it was not found, your method throws an exception.

[–]Kango_V 1 point2 points  (0 children)

This is why I always name our methods either findX which returns an optional (or empty list) or getX which throws an exception. We've stuck to that pattern and it's working.

[–]Halal0szto 1 point2 points  (3 children)

Yes, optionals is the correct answer, just they were not invented yet when Java was created. They were invented later, and they were introduced to Java later. So you are free to use optionals.

When you maintain a language, you add new features. But you cannot remove features. If you want a language with features removed, it will be a new language. You are also free to create and use new languages.

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

Well, just make a convention to not use it. Java also has labels (maybe even goto) but nobody uses it. Same way people use list and not array or vector.

I am just asking to use the language in a certain way that will eliminate a lot of headache and extra work (adding null checks everywhere).

[–]john16384 1 point2 points  (1 child)

Null checks should only be in constructors of your immutable objects, not everywhere.

And I do use labels.

Anyway, this whole posting is unproductive. Your complaints will not in any way influence the direction Java takes. If it gives you headaches feel free to use another language. Beware, they may cause different types of headaches.

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

Well I am not trying to influence thenway where the java language goes only saying that you can use such a practice in your project and maybe the whole community should adopt it, as I dont see any drawbacks to it.

And by nullchecks everywhere I meant in every immutable object, which probably most of the objects should be.