# Do we prefer cleaner code or faster solution if we cannot achieve both with one solution? (280ms java solution)

• In this post, Stefan's solutions are very concise. However, I found that the following solution only runs 280ms on OJ, which beats all other java solutions so far. It is fast because it only checks word1.equals(word2) once at the beginning. I wonder in real practice, do we prefer cleaner code or faster solution if we cannot achieve both with one solution? I hope people who have more industrial experience could give some adivce.

`````` public int shortestWordDistance(String[] words, String word1, String word2) {
int p1=-1; int p2=-1; int min = Integer.MAX_VALUE;

if(word1.equals(word2)){
for(int i=0;i<words.length;i++){
if(word1.equals(words[i])){
if(p1==-1){  p1=i; }
else{  min=Math.min(min, i-p1); p1=i; }
}
}
}else{
for(int i=0;i<words.length;i++){
if(word1.equals(words[i])){ p1=i; }
if(word2.equals(words[i])){ p2=i; }
if(p1!=-1 && p2!=-1){
min=Math.min(min, Math.abs(p1-p2));
}
}
}
return min;
}
``````

• If the time complexity is in the same order like O(n). We choose clean code.

(However actually I think your code looks better. The code in the if-else block are not duplicated at all. For some one-pass solution, if you put too many code in the 'one pass', that only makes code unreadable.)

• 'clean' code doesn't mean 'short' code. Yet I've always seen someone misunderstand what 'clean' means.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.