Question

I am coding a boggle solver for which I need to populate a dictionary. My approach is the do a static initialization something like:

private static final Set<String> dictionayKeys = DictionaryReader.populateDictionaryWords("/../dictionary.text);

To add more details, the boggle solver would only use dictionaryKeys to verify if a word is contained then dictionaryKeys eg dictionaryKeys.contains(currWord)

Now question - I have been reading up on lazy initialization, and wondering if this example would be be an ideal canditate for lazy init, given the file IO involved ?

Was it helpful?

Solution

Probably not in this situation. Wouldn't you ALWAYS need to populate the dictionary at some point? I would rather wait a little bit longer when starting the program so it can populate a dictionary, then have the program seemingly hang when I am playing the game while it populates it lazily.

I think lazy initialization is better used when it is an expensive operation AND the data isn't always required. For example, hibernate might lazily load some of the fields of an entity object that aren't related to the entity's key because not every use case of the entity will need that part of the data, only the ones that explicitly ask for it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top