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 5 years ago.
Improve this question
Which one is better to do...
importing package or using them directly inside a method
for eg:-
import java.util.Arrays;
int arr[] = new int[10];
Arrays.sort(arr);
Is the above one better?
-----(OR)-------
int[] arr = new int[10];
java.util.Arrays.sort(arr);
According to me both perform same thing...
But I wanted to know which of these is the best programming practice...
Right now there is no difference but you'll start seeing it when you have your own Arrays class.
If it is unique class name go with first way. That's the best way to organise your imports. If you have duplicated class names (although not suggestable), you'll be forced to have the second way.
It's all by developer but I prefer import classes because code it's more readable.
Second is old best practice but as I say it is from the developer to the developer
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 5 years ago.
Improve this question
I have the following simple question. Why is the usage of this:
Collection<String> something = new ArrayList<>();
Considered a bad practice?
My thoughts are the following:
This is opening the scope quite a lot without this being needed.
The actual assigned implementation (this case an ArrayList) does not
logically match the Collection (a list more or less means an ordered
data structure, whereas a collection not).
Does anyone have any other idea as to why this is not OK?
I think this counts as an opinion-based question, which isn't normally allowed on Stack Overflow. But while it's still open, I'll take the opportunity to give my own opinion:
It's not bad practice. You want to limit the interface as much as possible for clients. If a client doesn't need ArrayList methods, don't provide them. If a client shouldn't "know" what specific collection type it is, don't tell it. This will help leave the option for switching to a different kind of collection in the future.
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 1 year ago.
Improve this question
Where is preferred place to store functions for improve readibility of streams?
For example:
private BinaryOperator<Cash> sumPayments() {
return (payment1, payment2) -> payment1.sum(payment2);
}
I'm not sure if it should be in service class where it's used, but maybe it will be in many places so whats about some extra static Utils? or in entity?
Where possible avoid unnecessary helper methods.
In this particular example, the helper method can easily be replaced with a method reference, which is (arguably more) readable:
Cash total = subtotals.stream().reduce(Cash.ZERO, Cash::sum);
In other cases, where you actually do need a helper: think about where you would search for such a helper method in a year, when you have to adapt your now-barely-familiar code to new requirements.
Is it more entity related? Put it in the entity class
Is it more business logic related? Put it into the business logic
Is it generally useful? Put it into a utility class
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 6 years ago.
Improve this question
Is there a Java convention for packaging enums? if not is there a best practice?
Should I put them all in one package "myapp.enum" or should I put each enum in its related package?
enums in Java should be treated like any other class, and should probably be placed in the package that's most related to them. There's no advantage in having a separate "enums" package.
enum is a reserved keyword so you cannot put your enums into the enum package. Packages could be named after the layers of the application (Model, View, Controller, ..) You are free to choose. The only recommendation is to use unique package names like the reverse domain name of your company: mycompany.com -> com.mycompany.myapp...
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
How can I, and What is the best/proper way (ie, most performant and clearest syntactically) in Java to create object instances based on a prototype object instance, when this will occur repeatedly and in a performance critical code path?
I have thought about cloning via a cloning support library, but is that the best/only way? (These need to be arbitrary objects, btw, not ones that implement Clonable).
To clarify what I mean: I have an existing instance of Class T, which has fields set on it, and I want to pop out many versions of the same object to use separately, with the best performance and syntactic clarity possible.
Thanks.
Create a builder, which receives this class instance:
Person newOne = new PersonBuidler(oldOne).setAge(42)
Implementation of this builder may use apache common BeanUtils for cloning Java Beans or some other utility library for cloning arbitrary class.
See How do I copy an object in Java?
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
I have common methods written in different classes, making code duplicate and now to avoid this duplicacy, I am thinking of 2 approaches:
Make static methods in some util class and call them, or
Make a super class and write all these methods in super class and extend each class with this super class.
Definitely, with approach 2, I will loose the ability to extend my class further. So I am thinking to go with approach 1.
Can you please help me in identifying which approach is good and also in suggesting better approach, if you have any?
Creating Utility packages and/or class is a common solution to this problem. Apache Commons is a prime example. I would favour approach 1