7
Fabian
7y

public static Map<Integer, List<Integer>> stuff(arguments) {
HashMap<Integer, List<Integer>> map = new HashMap<>();
method(map, otherVariables);
return map;
}
public static void method(HashMap map, otherVariables) {
map.put(things);
}

So... You know how to return a map from a method. Then why do you create the map outside the method and make it an argument that does not get returned, making it confusing because the map gets created empty, given to a different method, then returned, making it look like you're returning an empty map...
...instead of just creating it inside the called method, returning it and assigning it to a map in the calling method? Even if you think that would create another map (it doesn't), the compiler is intelligent and can optimise that away.

Comments
  • 1
    Also there:
    int start=0;
    int end=array.length;
    for(int index=start;index<end;index++){
    //lalala
    }
    "start" and "end" are used nowhere else. The array does not change during the loop (which was a bit difficult to analyse).
  • 0
    Also just found this (it's like negative easter):
    boolean foo=false; // even marked as useless assignment by Eclipse
    for(bla;bla;bla){
    foo=method(aaaaa);
    if(foo){
    stuff;
    }
    }
  • 0
    It keeps going... So many useless assignments and other weirdness. After understanding how the method works my task is to rewrite it more efficiently anyway, so for now I'm refactoring and I bet it's half its length when I'm done.
  • 0
    Oh wow, it actually sounds much more like natural English after refactoring. So there was no reason to do it differently before. The lines don't even become that long. They still fit on my vertical screen.
  • 0
    Also this:
    if(bool){
    boolean bar=true;
    //lots of code
    } else {
    boolean bar=false;
    //the exact same code again
    }
  • 1
    Damn that spam
  • 1
    You know that you can simply edit your previous comment as long as it didn't reach the length limit xor time limit, right?
  • 2
    We have similar cases in my company, one's called PropertyMap. It has an attribute Map<String, Property> and around 4 methods like this:
    public void meth(params){
    map.meth(params);
    }

    WHY EVEN REWRITE EVERYTHING, WHY NOT
    class PropertyMap extends HashMap<String, Property>{
    ... copy constructors
    }

    SO MANY TIMES I HAVE TO ADD ONE METHOD TO THOSE KIND OF CLASSES BECAUSE IT ONLY EXISTS IN THE INTERFACE THIS CLASS IS A SHITTY COPY OF
  • 0
    @gitpull Really? I don't see an edit button. I'm on the desktop website.
Add Comment