Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I just wonder about best way to write custom exception name, like if I have user and i want make exception for add and delete and update, what is better using names like :
UserAddException
UserUpdateException
UserRemoveException or UserDeleteException ?
or like:
UserAdditionException
UserUpdateException
UserDeletionException
Exception name must describe what it handle, but not sure use "verb" describe the action where exception happened or "noun" as what exception itself do.
What I want to understand here the best way that make developers understand my exception usage and what to use later, and if there is pattern or standard used for Java development in this case.
I would go with the second type of exception names. The reason why I would say that is because the exception: InstantiationException uses the noun InstantiationException. However, he most important thing is that you are consistent with the naming of exceptions and that the exception names give the development team a clear idea of what those exceptions indicate and their meaning. That is really the critical thing here.
What I would do will be, use UserManagementException instead of too many names, and specify the exact cause of it in some message or error code defined additionally in the class.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am wondering how should I deal with the save method. Let's consider a situation when I want to register a new user. Firstly I need to check if there is one with that Id or login. If I can not find anyone I am able to create a new one and use save method from Spring Data.
What in the case when I found someone with such login? Should I throw an exception? I don't like them, for me, it's sometimes like running away from a problem. Returning a null instead of an object is not a good choice either, isn't it? That is my problem which I want to solve in the most gentle way. What to return and how to deal with such a situation. Maybe the exception is necessary? Then just handle it in my controller and deal with it. I hope you will advise me. I am rather asking for tips and a few words from experienced guys rather than getting an ordinary solution from the majority of pages on the internet. Thanks :D
An exception literally means "I couldn't complete this operation normally because of a circumstance outside the ordinary workflow", and AccountAlreadyExists is a perfect example of when to throw an exception. You're not "running away", you're informing a higher layer of the application that it will have to handle the problem. As a thought experiment: What if you have more than one reason that something could fail? How do you distinguish between null (duplicate account) and null (banned domain name)?
Note in this case that you definitely should have something like a UserAccountService whose responsibility is enforcing rules like "no duplicate accounts", and this will be the object that actually calls userRepository.save(newUser).
If you're using Spring MVC views, then you'll want to catch the exception in your controller and send the user to an error page. If you're using a JSON API, then you'll probably want to let the exception escape to return an error to the client; consider annotating your exception class with #ResponseStatus(UNPROCESSABLE_ENTITY) or similar.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Let's say you have some issue to develop. And as recommended practice it is good idea to use interfaces ( I don't mean GUI, I mean interface or abstract class ). And you can apply two ( I'm pretty sure, but for now I noticed I apply two ) ways:
Design interfaces upfront and then implement them.
Implement classes and then on basics of classes discover interface.
Personally I prefer second option, but during discussions with other developers I noticed that somebody prefers first approach. I can say that I prefer second approach for the following reasons:
I can faster write code
I avoid unnesessary code ( something that I never will use )
Interfaces in that case are more binded to "real" life
For me it is more convenient.
I'd like to hear other advices why somebody prefers option 1 or option 2.
As usually I code in C#, but AFAIK java also have idea of interfaces
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to track down a java variable in a java file - which variable it got assigned to, which method it was passed to.
How should I begin with?
Should I use line by line parsing or is there any other method?
It looks like you are asked to build a huge mansion; and you start by asking: "should my shovel to dig the cellar be better round; or more rectangular". Meaning: if you don't understand that parsing a java program requires more than "line by line" reading; then you are doomed to fail.
Anyway, depending on your underlying requirements, there are two possible answers:
As suggested by duffymo, you might want to learn using an IDE which allows you to easily identify "variable usage" within a project; and make modifications via "reflection"
Start using a fully fledged Java parser; like https://code.google.com/p/javaparser/wiki/UsingThisParser
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Why errors are not handled? Since Error class is derived from Throwable class(JAVA) it can also be handled.But why it is not a good practice to handle the error?
Compilers are not predictive to get the result exactly in the format humans want.
They obviously work on the limited syntax and semantics rule and as per some grammar (or rules you can say).
Errors are also Exceptions in Java that define exceptions which aren't expected to be caught under normal circumstances.
So basically, an error is that problem which requires human handling for getting the correct result. Also, your assumption that errors aren't handled is incorrect, as errors are reported like errors occurring during runtime. But, they don't specifically correct the error and also they don't provide much detail about the error.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
(This might be the wrong place to ask the question, please let me know).
Should I name my method isStaticallyImported or isStaticlyImported?
(They'd be pronounced pretty much the same way, I believe)
Of course they should be in good english. Even if the human brain will likely have no problems reading garbled up words, compilers do not enjoy the same luxury.
How many times have you miswritten a variable name, then later on used the correct spelling, only to find out that the program crashed at run/compile time?
This problem is only amplified when working on code that was not written by you, because we think of things as, well, things, and having to specially remember that the thing had to be spelled in a special way is just an unneeded break to your workflow.
Yes, your variables should be clear to the developer. You can name it whatever you want and it will work because the compiler doesn't care. When you name the variable in a human readable manner then developers after you will be able to read and understand your code much easier. You should name it "isStaticallyImported".
They should be in the most easily understandable language for those using and maintaining it in my opinion.
I'm also pretty sure the compiler doesn't care about the quality of spelling.