#285 Code Review of a Random Words Service
Optimize the code and perform a code review:
import org.json.JSONArray;
import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class CodeReview {
private static final int wordCount = 10;
private static ArrayList<String> stringList;
public static ArrayList<String> getWords() {
stringList = new ArrayList<>();
String response = requestRandomWords();
log.debug("get words: " + response);
for (int i = 0; i < wordCount; i++) {
try {
stringList.add(new JSONArray(response).getJSONObject(i).getString("word"));
} catch (Exception e) {
e.printStackTrace();
}
}
return stringList;
}
private static String requestRandomWords() {
// go to some resource and return JSON
return "some json";
}
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor