www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/02/18:32:51

Message-ID: <368EABD5.34206130@montana.com>
Date: Sat, 02 Jan 1999 16:29:25 -0700
From: bowman <bowman AT montana DOT com>
X-Mailer: Mozilla 4.5 [en] (Win95; I)
X-Accept-Language: en
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: The Endless Pursuit of Elegance
References: <76lv0j$m21$1 AT birch DOT prod DOT itd DOT earthlink DOT net>
Reply-To: djgpp AT delorie DOT com


Mike Moore wrote:
> 
>     I'm making a little CD player using the BCD library.  It accepts command
> line arguments, and I have this monstrous if-else loop that figures out what
> argument the user typed:

You might try a struct containing a pointer to a possible argument and a
pointer to the function that handles that choice. You can tweak the
functions so they will all have the same signature. Then build an array
of the structs, and initalize it. throw a null string or some other
marker at the end so you know when to take a default action.

It might look prettier, and having all the stuff in a table might make
it easier to add options at some later date, as you can just add the new
option string and write a handler for it. I doubt if it will be any more
efficient, but it will condense the code down to about three lines, and
the struct initialization will probably fit within the screen, rather
than having several screens of strcmps.

I doubt if there are enough options to worry about anything other than a
quick, dirty linear search.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019