All Questions
Tagged with javaprogramming-practices
24 questions
283votes
6answers
168kviews
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: ...
22votes
6answers
13kviews
Efficient try / catch block usage?
Should catch blocks be used for writing logic i.e. handle flow control etc? Or just for throwing exceptions? Does it effect efficiency or maintainability of code? What are the side effects (if there ...
276votes
14answers
125kviews
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 ...
59votes
9answers
49kviews
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; }...
56votes
3answers
74kviews
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, ...
17votes
3answers
43kviews
How to test the data access layer?
I have a DAO method that utilizes Spring for JDBC access. It calculates a seller's success rate of selling an item. Here is the code: public BigDecimal getSellingSuccessRate(long seller_id) { ...
46votes
6answers
7kviews
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 ...
10votes
4answers
28kviews
Replacement for instanceof Java?
So I'm fairly new to programming in the real world (outside of academic projects) and have come across lots of posts saying that using instanceof is a bad thing to use to determine what class a ...
87votes
10answers
111kviews
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 ...
84votes
8answers
73kviews
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 ...
49votes
7answers
48kviews
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 ...
27votes
6answers
34kviews
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/...
11votes
3answers
12kviews
When comparing a string variable to a string literal with .equals(), is there a standard practice for the order of items? [closed]
There are benefits to each and I understand the differences, but what is considered best / standard practice? And why? For example : "myString".equals(myStringVar) Avoids a potential NPE and does ...
0votes
5answers
2kviews
Do I have to write a lot of boilerplate code if I keep working using Java? [closed]
I'm working for a company writing ERP applications. My problem is that I have to write tons of boilerplate code. I came up with ideas to automatize/prevent the drudgery but only some of them were ...
21votes
9answers
3kviews
How to teach Exception Handling for New Programmers? [closed]
How do you go about teaching Exception Handling to Programmers. All other things are taught easily - Data Structures, ASP.NET, WinForms, WPF, WCF - you name it, everything can be taught easily. With ...