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) |...
Andriy Makukha's user avatar
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 ...
Bar Akiva's user avatar
  • 217
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 ...
gaout5's user avatar
  • 41
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 ...
Tom Taylor's user avatar
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 ...
melonT's user avatar
  • 243
-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 ...
ifelse.codes's user avatar
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 ...
Andreas Braun's user avatar
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 ...
Andreas Braun's user avatar
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 ...
Andreas Braun's user avatar
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 != ...
Mohit Kanwar's user avatar
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; } }...
gexicide's user avatar
  • 129