I have a method that gets the index to use in my own markdown system.
Indices are passed to the method and it has to return the lowest greater than -1 (not found) until now we only had negrita
, cursiva
and normal
the method was relatively simple (although it seems ugly to me with desire):
private int getValidIndexToUse(int indexOfBold, int indexOfItalics) {
if (indexOfBold > -1 && indexOfItalics == -1)
return indexOfBold;
else if (indexOfItalics > -1 && indexOfBold == -1)
return indexOfItalics;
else
return indexOfBold > -1 && indexOfBold < indexOfItalics ? indexOfBold : indexOfItalics;
}
Now you have to add cursiva+negrita
and underline and with this system the method becomes crazy.
Does anyone have any better ideas than nesting infinite if's?
The general problem of finding the smallest number in a set of numbers, provided that it is greater than some other number, could be further simplified with a variable-length parameter:
Or with Java 8, using a stream of ints:
Well, posting the question I have seen very clearly how to do it without complicating my life, very similar to if we wanted to get the same result by iterating through an array or list:
NOTE: it is not checked if they are all equal to -1 because the method call is inside a
while (containsAny(stringToCheck, TAGS))
, but if this check is not carried out, it is a point to check.What I have realized is that there was an error in approaching the problem and excessive complication in the old method, added to the fact that at certain moments you get confused, very complicated methods like the one in the question come out.
I hope my temporary blindness will help someone in the future.