#285 Code Review of a Random Words Service

Companies:
Aston
Themes:
SpringSpring CoreCode Review

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

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in