Skip to content

Instantly share code, notes, and snippets.

@isaacsanders
Created January 16, 2013 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacsanders/4544971 to your computer and use it in GitHub Desktop.
Save isaacsanders/4544971 to your computer and use it in GitHub Desktop.
Any thoughts on making this even more awful?
public static int search(Integer[] arriArrayToBeSearched, Integer iItemToBeFoundInArray){
int iIndexOfItemToBeFoundInArrayInArrayToBeSearched = -1;
int iLengthOfArrayToBeSearched = 0;
int iTemporaryVariable = arriArrayToBeSearched.length;
while (iTemporaryVariable > 0) {
iLengthOfArrayToBeSearched += 1;
iTemporaryVariable -= 1;
}
for (int indexInArrayToBeSearched = 0; indexInArrayToBeSearched < iLengthOfArrayToBeSearched; indexInArrayToBeSearched += 1) {
Integer iMemberOfArrayToBeSearchedAtCurrentIndex = arriArrayToBeSearched[indexInArrayToBeSearched];
boolean bMemberOfArrayToBeSearchedAtCurrentIndexIsEqualToTheItemToBeFoundInArray = iMemberOfArrayToBeSearchedAtCurrentIndex.equals(iItemToBeFoundInArray);
if (bMemberOfArrayToBeSearchedAtCurrentIndexIsEqualToTheItemToBeFoundInArray) {
iIndexOfItemToBeFoundInArrayInArrayToBeSearched = indexInArrayToBeSearched;
}
}
return iIndexOfItemToBeFoundInArrayInArrayToBeSearched;
}
@garyjohnson
Copy link

You could replace those numbers with constants, such as line 3:
int iLengthOfArrayToBeSearched = INITIAL_LENGTH_OF_ARRAY_TO_BE_SEARCHED;

@eraserhd
Copy link

You can move indexInArrayToBeSearched out of the for(), Make a int iNextIndexTobeSearched outside the loop, assign it at the top of the loop to indexToBeSearched + 1, and in the last clause of the for(;;), set indexInArrayToBeSearched = nextIndexToBeSearched.

@eraserhd
Copy link

Also, you can special case with if (arriArrayToBeSearched.length == 0) { iIndexOfitemToBeFoundInArrayInArrayToBeSearched = -1; } else { ... the rest of algorithm ... }

@eraserhd
Copy link

Or you could put

if (iItemToBeFoundInArray + arriArrayToBeSearched.length == iItemToBeFoundInArray)
   return -1;

At the beginning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment