HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Avoiding use of an initialized variable that will either be changed or ignored

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
variableinitializedignoredeitherwillthatuseavoidingchanged

Problem

Follow on from this question

This is a follow on question from one I asked a few days ago. I am making (or rather have made) a recursive method that takes a list of ArrayList objects as its parameters. There can be any number of them, but let's pretend there are five. They are numbered from 1 to 5, so the Array contains: [1 2 3 4 5].

The Array is then passed through a recursive method which returns only the numbers at odd indices in the Array, i.e. [2 4] (as they are at indices 1 and 3).

This is the code for the recursive method:

public static ArrayList oddList (ArrayList tList) {

    ArrayList oddList = ListMethods.deepClone(tList);
    int tempStorage = -1;

    if (oddList.size() = 0)
            oddList.add(tempStorage);                               
    }                                                               
    return oddList;                                                 
}


There are two things I am curious about:

  • Firstly, is there an alternative to that if statement at the very end, to check that tempStorage >= 0.



  • Secondly, and probably more importantly, should I care? In other words, is what I've done there a cheap, easy fix, or is that common coding practice? It just seems strange having an initialised variable that will always be either changed or ignored.



Any feedback would be fantastic, thanks (and to the guys who posted on my previous post, I'm still reviewing all the information - not allowed to use most of it in this assignment, but thanks anyway!)

Solution

I already explained in my other post, not to use this tempStoragevariable in this way. At least (compared to the other version) you do not change the meaning of it in this version.

If you rename it to something like removedItemFromEvenIndex it could be fine. If and really if the requirement is that all values inside the lists are >= 0.

If all values are allowed, I would suggest to stick to the solution with the boolean I suggested in the other post. Because if all values are allowed, You can not find any "check" value to see if you removed or something or not.

From where comes this problem? You try to do 2 things together with the tempStorage:

  • Transport the removed value



  • Status flag if you removed something or not



You can not address both things with the same Integer, because you already need everything from your Integer for the first part.

Rest is the same as for the other question.

To handle this one, same approach as other question:

What we want to do here? Add every second element to the new list, starting with the second.

So a simplified description could be:

function oddList(list)
  if list is empty or has only 1 element
    return empty list
  return new list(second element of list, oddList(all elements from the third to the end))


translate to algorithm in Java:

public static List oddList(final List list) {
    if (list.size() ();

    final List newList = new ArrayList<>(Arrays.asList(list.get(1)));
    newList.addAll(oddList(list.subList(2, list.size())));
    return newList;
}


If we take into account the other question, this could be a clever way (you should always try to reuse existing and working solutions):

public static List oddList(final List list) {
    final List newList = new ArrayList<>(list); //we create a copy, otherwise list is modified for the caller
    newList.remove(0); //all indices shifted one to the left
    return even(newList);
}

Code Snippets

function oddList(list)
  if list is empty or has only 1 element
    return empty list
  return new list(second element of list, oddList(all elements from the third to the end))
public static List<Integer> oddList(final List<Integer> list) {
    if (list.size() <= 1)
        return new ArrayList<>();

    final List<Integer> newList = new ArrayList<>(Arrays.asList(list.get(1)));
    newList.addAll(oddList(list.subList(2, list.size())));
    return newList;
}
public static List<Integer> oddList(final List<Integer> list) {
    final List<Integer> newList = new ArrayList<>(list); //we create a copy, otherwise list is modified for the caller
    newList.remove(0); //all indices shifted one to the left
    return even(newList);
}

Context

StackExchange Code Review Q#22770, answer score: 3

Revisions (0)

No revisions yet.