All Questions
Tagged with java programming-practices
135 questions
296
votes
16
answers
30k
views
Grokking Java culture - why are things so heavy? What does it optimize for? [closed]
I used to code in Python a lot. Now, for work reasons, I code in Java. The projects I do are rather small, and possibly Python would work better, but there are valid non-engineering reasons to use ...
283
votes
6
answers
168k
views
Choosing between Single or multiple projects in a git repository?
In a git environment, where we have modularized most projects, we're facing the one project per repository or multiple projects per repository design issue. Let's consider a modularized project:
...
276
votes
14
answers
125k
views
Should we avoid object creation in Java?
I was told by a colleague that in Java object creation is the most expensive operation you could perform. So I can only conclude to create as few objects as possible.
This seems somewhat to defeat ...
87
votes
10
answers
111k
views
How many lines per class is too many in Java? [closed]
In your experience, what is a useful rule of thumb for how many lines of code are too many for one class in Java?
To be clear, I know that number of lines is not even close to the real standard to ...
84
votes
8
answers
73k
views
Is modifying an incoming parameter an antipattern? [closed]
I am programming in Java, and I always make converters sort of like this:
public OtherObject MyObject2OtherObject(MyObject mo){
... Do the conversion
return otherObject;
}
At the new ...
59
votes
9
answers
49k
views
Should the methods of a class call its own getters and setters?
Where I work I see lots of classes that do things like this:
public class ClassThatCallsItsOwnGettersAndSetters {
private String field;
public String getField() {
return field;
}...
56
votes
3
answers
74k
views
Which is a better practice - helper methods as instance or static?
This question is subjective but I was just curious how most programmers approach this. The sample below is in pseudo-C# but this should apply to Java, C++, and other OOP languages as well.
Anyway, ...
49
votes
7
answers
48k
views
Is it a bad practice to have an interface to define constants?
I am writing a set of junit test classes in Java.
There are several constants, for example strings that I will need in different test classes.
I am thinking about an interface that defines them and ...
46
votes
6
answers
7k
views
How necessary is it to follow defensive programming practices for code that will never be made publicly available?
I'm writing a Java implementation of a card game, so I created a special type of Collection I'm calling a Zone. All modification methods of Java's Collection are unsupported, but there's a method in ...
45
votes
6
answers
14k
views
Is it better to check `c >= '0'` or `c >= 48`?
After a discussion with some my colleagues, I've a 'philosophical' question about how treat the char data type in Java, following the best practices.
Suppose a simple scenario (obviously this is only ...
38
votes
5
answers
9k
views
When should I extend a Java Swing class?
My current understanding of Inheritance implementation is that one should only extend a class if an IS-A relation is present. If the parent class can further have more specific child types with ...
36
votes
9
answers
15k
views
Why do schools teach arrays over List? [closed]
Most of the assignments in my school for the initial programming classes required me to use arrays. I work full time now, and I never used an array for any project that I have worked on. Even in the ...
34
votes
8
answers
15k
views
Is throwing an exception an anti-pattern here?
I just had a discussion over a design choice after a code review. I wonder what your opinions are.
There's this Preferences class, which is a bucket for key-value pairs. Null values are legal (that'...
33
votes
6
answers
13k
views
Is throwing new RuntimeExceptions in unreachable code a bad style?
I was assigned to maintain an application written some time ago by more skilled developers. I came across this piece of code:
public Configuration retrieveUserMailConfiguration(Long id) throws ...
27
votes
6
answers
34k
views
Is it a bad habit to (over)use reflection?
Is it a good practice to use reflection if greatly reduces the quantity of boilerplate code?
Basically there is a trade-off between performance and maybe readability on one side and abstraction/...