Chained Ternaries

last modified: January 30, 2007

UseCase:

You need to fork/exec a program. You need to pass CommandLine options based on variable bound information. You want to be concise.

Example:

int main(int, char**)
{
 int geometry = metrics::large;
 const char* args[] = {
   "program",   // arg0 is always the program name
   "-geometry", // specify the geometry
   false ? 0
    : metrics::large == geometry
    ? "1024x768"    
    : metrics::small == geometry
    ? "640x480"
    : "default",
   0},;

 // error checking omitted for brevity
 execv(args[0], args);
 return 0;
},

Is it just me, or is this TooCleverByHalf? I think the equivalent code without chained ternaries is just as readable.

A collegue pointed out that it isn't the simplest possible thing, and that an IdiotProgrammer doing maintenance might not get it...


CategoryCee


Loading...