I just wrote this method:
private String getNameOfFileFrom(String path)
{
int indexOfLastSeparator = path.lastIndexOf('/');
if (indexOfLastSeparator > -1)
{
return path.substring(indexOfLastSeparator + 1);
}
else
{
return path;
}
}
The line that bothers me is:
return path.substring(indexOfLastSeparator + 1);
Is that a bad practice to modify the expression inline like that? Suggestions on how to refactor to increase readability would be most welcome.
----Edit---- OK update after comments. Thanks everyone for answering :) I am not looking to actually change the the value of the variable at all just the expression it is used it.
Another posted suggested I could break out that part of the expression as in the second code snippet below. Better/worse/no difference? :) I am starting to suspect I am being overly cautios here.
return path.substring(indexOfLastSeparator + 1);
or
int indexOfFirstCharInFileName = indexOfLastSeparator + 1;
return path.substring(indexOfFirstCharInFileName);