Is it worth creating a local variable if it's only needed once?



  • It is particularly difficult to understand whether a local variable should be created when it is used as a method. Like I do. ArrayList<Integer> and I challenge his method.

    set(int i, Object value);

    There you go.

    limit.set(
                        index,
                        limit.get(i)
                                - task.getUnSortListOfVariable().get(i)
                                        .get(maxTop.getFix()).getWeight());
    

    limit is mine. ArrayList<Integer>

    I refer to the second argument:

    limit.get(i)
                                - task.getUnSortListOfVariable().get(i)
                                        .get(maxTop.getFix()).getWeight()
    

    Should I write:

    int value = limit.get(i);
    value -= task.getUnSortListOfVariable().get(i)
                                        .get(maxTop.getFix()).getWeight();
    

    This value will no longer be used in the code.



  • Yeah, it's worth it if it increases the code reading. The best code is self-documented.

    Everything should be fine. If you create a variable on every chick, the code will blow up and the trees won't be seen in the woods.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2