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 this:
return path.substring(indexOfLastSeparator + 1);
Is it bad practice to modify an inline expression? Suggestions on how to reorganize to increase readability will be most welcome.
---- ---- Edit OK is updated after comments. Thanks everyone for the answer! I do not want to actually change the value of the variable at all, just the expression that it uses.
Another published one suggested that I could tear out this part of the expression, as shown in the second code snippet below. Better / worse / no difference? :) I'm starting to suspect that I'm too careful here.
return path.substring(indexOfLastSeparator + 1);
or
int indexOfFirstCharInFileName = indexOfLastSeparator + 1; return path.substring(indexOfFirstCharInFileName);
language-agnostic refactoring code-smell
willcodejavaforfood
source share