Most mainstream programming languages are pretty bad at initializating and populating structured data. Let's say you want a Java list containing "a" and "b".
First attempt :
a.add("1");
a.add("2");
Pretty bad signal-to-noise ratio.
Second attempt :
Slightly more readable, but allocates a useless intermediate array.
Third attempt :
Much more readable, but doesn't work if you want a set rather than a list : there's no
Arrays.asSet().Here's the OptimJ version :
HashSet<String> s = { "a", "b" };
But nausea quickly kicks in when you try to instanciate imbricated collection. A cooking recipe may be seen as a list (ordered) of sets of tasks (unordered because they can be done in parallel).
First attempt :
HashSet<String> set1 = new HashSet<String>();
set1.add("Mince the meet");
set1.add("Mash the potatoes");
irishStew.add(set1);
HashSet<String> set2 = new HashSet<String>();
set2.add("Cook in oven");
irishStew.add(set2);
Wow! This is called write-only code. Think about having to maintain pages of similar code written by an summer intern two years ago.
Second attempt :
The
asList doesn't work because it requires expressions, and the code that builds our sets is a sequence of statements, not an expression.Here's the OptimJ version :
{ "Mince the meet", "Mash the potatoes" },
{ "Cook in oven"}
};
Do you feel more confident now taking this code maintenance assignment ?
