All Questions
Tagged with java coding-style
106 questions
0
votes
2
answers
325
views
Which is better: a chain of OR statements or IF in a loop? (Java)
Which code style is preferred in Java?
final boolean result = isCausedBy(e, ExceptionType1.class)
|| isCausedBy(e, ExceptionType2.class)
|| isCausedBy(e, ExceptionType3.class)
|...
1
vote
5
answers
210
views
End method in normal flow versus exception flow
Consider the two following examples:
public Something fetchSomething(String key) {
if(somethingsMap.containsKey(key)) {
return somethingsMap.get(key);
}
throw new ...
14
votes
5
answers
5k
views
How to "Tell, don't ask" when 2 objects involves in the condition and the decision at the same time?
According to Explanation on how "Tell, Don't Ask" is considered good OO, I know the following is bad:
if(a.isX){
a.doY();
}
public class A{
public boolean isX;
public void ...
2
votes
1
answer
198
views
Declaring code style settings in a Java project
In a Java open source project built with Maven, is there a standard way to declare code style settings (such as indentation settings and import order)? I would like that people who import the project ...
0
votes
4
answers
410
views
Are "easier to search for the interface" and "avoid yo-yo to interface to find implementations to fix bugs" reasons to add prefix "I" on interfaces?
According to Should interface names begin with an "I" prefix?, I should not add prefix "I" to interfaces. However, I think prefix "I" in interface may be useful sometimes....
6
votes
7
answers
7k
views
How to avoid repeating "a==b" when comparing "condition a" and then "condition b" and then...?
For example, I have an object:
public class Obj{
public int a;
public float b;
public String c;
}
I need to find best "Obj": largest a and then largest b and then longest c:
int ...
1
vote
3
answers
258
views
To enforce column limits on long strings? [closed]
We're trying to update our style guide (using google's guide as a starting point) and I'm currently in the middle of a debate with my colleagues about column limits. I believe we're all in agreement ...
8
votes
4
answers
2k
views
Are there advantages to using an interface, rather than a class, for a Java application?
Java is often (rightly IMHO criticized) for overusing the class keyword, as it can denote:
a factory to instantiate objects (traditional classes)
a collection of global methods (when all methods are ...
6
votes
5
answers
7k
views
Assertion statements: Remove or retain in production code
I'm working in a large company and am doing my first Java project here. We have certain code style guidelines. In the Java guideline, it is said that "assertions are only intended for debugging ...
9
votes
2
answers
1k
views
Should function names describe their parameter types?
If you wish to perform the same action using different parameters, you can either make differently named functions:
public Apple findAppleById(long id){
return repo.findById(id);
}
public Apple ...
1
vote
2
answers
289
views
Granularity of a Method
I have a general design question. Suppose I have a List:
List<String> list = new ArrayList<>();
list.add("Str 1");
list.add("Str 2");
list.add("Str 3");
I ...
6
votes
1
answer
791
views
How does Dependency Inversion solve switch cases?
I want to understand how Dependency Inversion gets rid of switch cases.
Consider a service that needs to communicate with several databases.
class StockUpdater {
private final DataStore datastore;
...
-1
votes
1
answer
241
views
Is it bad to break out of a labelled block in Java?
I sometimes write Java code that looks like:
success: {
fail: {
if (...) break fail;
// some code
if (...) break fail;
// some code
if (...) break fail;
...
-4
votes
1
answer
156
views
Java Class Structure
I have a problem during a project of mine using Java,
as usual my java classes have the structure:
import ...;
import ...;
...
private Type0 myVariable0;
private Type1 myVariable1;
...
...
private ...
5
votes
3
answers
3k
views
Significant difference between functional and procedural collection handling [closed]
I'm planning an empirical study on function passing - specifically lambdas aka anonymous functions aka arrow functions. Now, although functional or even object-oriented approaches are highly favored ...