All Questions
Tagged with java coding-style
106 questions
106
votes
11
answers
17k
views
Has / can anyone challenge Uncle Bob on his love of removing "useless braces"?
I hate referencing paywalled content, but this video shows exactly what I'm talking about. Precisely 12 minutes in Robert Martin looks at this:
And says "One of my favorite things to do is getting rid ...
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)
|...
14
votes
2
answers
10k
views
Which is a better pattern (coding style) for validating arguments - hurdle (barrier) or fence? [duplicate]
I don't know if there are any accepted names for these patterns (or anti-patterns), but I like to call them what I call them here. Actually, that would be Question 1: What are accepted names for these ...
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 ...
17
votes
4
answers
68k
views
How to deal with Classes having the same name (different packages)
Me and my R&D team maintain a large codebase. We've divided our business logic into multiple packages. some of which have classes with identical names.
As you can guess, the names conflict when ...
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....
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 ...
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 ...
47
votes
9
answers
61k
views
Line break before or after binary operator? [closed]
While Sun's Java code convention suggests to put line break before the operator, many other guidelines disagree with it. I do not see any obvious pros and cons. Are there advantages of using one of ...
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 ...
136
votes
5
answers
86k
views
In Java, should I use "final" for parameters and locals even when I don't have to?
Java allows marking variables (fields / locals / parameters) as final, to prevent re-assigning into them. I find it very useful with fields, as it helps me quickly see whether some attributes - or an ...
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 ...
13
votes
3
answers
4k
views
In Java 8, is it stylistically better to use method reference expressions or methods returning an implementation of the functional interface?
Java 8 added the concept of functional interfaces, as well as numerous new methods that are designed to take functional interfaces. Instances of these interfaces can be succinctly created using ...