Skip to main content

All 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) |...
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 ...
12 votes
5 answers
2k views

How to introduce new language features in a legacy source code?

I have a coding style related question. In my example it is java, but I think this can be a generic question regarding languages that are changing rapidly. I'm working on a java code base which was ...
2 votes
1 answer
846 views

Better way to implement a feature with turn on/off based on a flag

I'm trying to implement a feature with option to turn it on/off based on a flag. I'm doing it in the following way which I feel like a overkill, public interface Feature { public interface ...
-2 votes
1 answer
2k views

How to clean a refactor Java for-if-try-catch-else kind of messy code [duplicate]

Often I came across situations like this, how to write this code in a neat and clean way. One more issue I find here is performance as I am iteration a list and then it's properties. Edit : - while ...
17 votes
5 answers
12k views

Boolean return of set.add() in if conditional?

The add operator of the set class returns a boolean which is true if the element (which is to be added) wasn't already there, and false otherwise. Is writing if (set.add(entry)) { //do some more ...
1 vote
2 answers
2k views

Return array of multiple different objects?

I have code along the following lines: public void processInput() { List<String> input = readInput(); final Object[] returnObj = createInternalStructureFrom(input); final Dictionary ...
3 votes
3 answers
2k views

Tradeoff between clean code, duplicate code and code efficiency in java

I have a question on writing clean code. I’m trying to refactor the following method: private static Map<String, String> createMapOfAttributes( final String Id, final String ...
1 vote
1 answer
268 views

Sorting Array before looping : best practice

I was going through JBAKE code at https://github.com/jbake-org/jbake/blob/master/src/main/java/org/jbake/app/Asset.java : 58 PFB the code. Why are we sorting the array here? if (assets != ...
2 votes
1 answer
2k views

Why does Java support brackets behind variables and even behind method signatures? [closed]

Java allows this: class X{ int i,j[]; // j is an array, i is not } and even worse, it allows this: class X{ int foo(String bar)[][][] // foo actually returns int[][][] { return null; } }...