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 have a method entangled with control structures. It has many ways to exit. Before leaving the method I need to do some final processing. Instead of repeating the same logic before each exit or refactoring that logic in a method and calling it several times it seem handy to leave that in a finally block. Is it really a legitimate use of finally or am I abusing it?
finally is there for a reason, to add logic that must be execute before the exiting block
It's a valid choice for a method if you don't want/need to use AOP/AspectJ
Notice you may have to use finally for release resources as Connection
For example you can use it when you must audit/log or do autonomous transaction at the end of the method
As #DaveNewton comment, in some cases there might be a better way of refactoring/separating logic, but you can't ignore that it's a valid usage
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 4 years ago.
Improve this question
I am very new to Java so excuse me if this sounds like a dumb question.
Why is such a big effort made when following the state-design-pattern (creating an interface, context and concrete sub-classes for each state) when you could just save the state of a given object in a variable and then make decisions based on switch and if-else statements later on?
if-else and switch statements encourage brittle code and responsibilities mixing.
Every time you add/remove/update a state, the same class and method has to be changed or a sub method invoked, so you increase the odds of introducing regressions in any state logic, whereas you would change one of them instead.
By separating the concerns, the states are not coupled; you could easily modify them without risking changes to any others. You could even validate this with unit tests.
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 6 years ago.
Improve this question
So, I just finished my first program, but I haven't use any methods in it, since I'm just beginning to learn how to use them. Here's the original code : http://codepad.org/JiBfJI8Q I started to fractionate it but realised that it would be a method inside another all the way down. Is that actually the way to do it, or did I get the idea wrong?
without having looked at your code:
The general idea of methods is to separate small
portions of code which might be used at multiple other places in your code.
so yes, calling methods from within other methods is a good thing to do.
ideally your so called "composed methods" read out like a little story:
public void transaction(){
openDatabaseConnection();
addRecordsToDatabase();
closeDataseConnection();
}
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 6 years ago.
Improve this question
I want to programmatically move a Thread in another method. How I can do this?
that's entirely not possible in Java.
There are some ways around that - using tools like AtomicReferences, AtomicBoolean, wait/notify or Channels. With these tools, you could inform the other thread that it should do something specific.
Another approach would be to copy SwingUtilities invokeLater - like here: http://www.javamex.com/tutorials/threads/invokelater.shtml
However, I would like to ask the question why that method execution needs to be run in a specific thread? Wouldn't just another (new thread) be fine too? That should significally simplify your problem. In that case, just start a new thread to call that method
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
How important is it to use labels in Java? I haven't seen labels used, except in academic books.
I saw them used with jump statements such as break and continue.
You can use labels, but they are considered bad form in general, sort of unrestrained jumping within a method, it makes the code harder to maintain and can introduce bugs if not handled carefully.
As a rule with OO there is usually an easier/better way to achieve things.
In too many years of coding Java I have never used a label.
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 8 years ago.
Improve this question
As in the topic name, I wonder what is the best convention
The answer to this question is essentially another question: what makes your code easier to read and maintain?
Martin Fowler, a well-known author and programming guru, suggests a refactoring called Replace Nested Conditional with Guard Clauses.
I am definitely more of the mind to use guard clauses because they usually make the code cleaner and easier to read. However, once in a while there is a scenario where the intent of the code comes through clearer without them.