The stupid question is the question not asked | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
why_bird:
Disclaimer: (1) I've never written any OO perl code, (2) I've not reviewed your code thoroughly. I hate the writing in this rambly node, but not quite enough to abandon it. Any comments on improving the writing / organization / code examples in this node would be greatly welcomed! I noticed two OO mistakes: First, if you have a bunch of if/else/... statements in a chunk of OO code, it's a place to look at and think about factoring. (Specifically, in both get_option and set_option, you have a sequence of conditionals based on the type of the argument, that *begs* for a factorization.) Second (related to the first), it's not extensible. What do you do if you want to add a "date" type? Perhaps a "filename" type might be nice (so it can automatically check for the presence of a file or some such?).So I'd first factor out a new base class to represent a typed option ... say "option_type". Initially, I'd give it two member functions: set_value and get_value. Then you can subclass it based on the types you want to handle. Thus, you'd get "option_bool", "option_int", "option_num" and "option_str". Factoring out the set_value and get_value code would give you something like[1]: Then the code in your set_option and get_option subroutines would look more like[2]:
After this factorization, you'll find that there are other things that belong in your "option_type" classes: Obviously, there would be a default value for each class type, which you could set in the object constructor (allowing you to rid yourself a bit of code in add_option):
And, of course, you'd want the name, desc, valid etc. values you currently store for the options in there. You could add the is_valid method to each class, so your is_number and is_int methods would become member functions. Since you don't have is_XXX for bool and str, you could let the base class handle it (by assuming valid, for instance?). Then, you'd review your code (again) to find other things to factor out and make specific to the object type(s). You might have a value formatter for your print_options method, etc. Then, when you want to add a new type of option to the system, you could add it without having to touch so many bits of your getopt_dev package. If you guess correctly about what bits need to be factored into your option_type base class, you wouldn't have to touch the getopt_dev module at all. You could just add your new option type(s) in another module. Yark! I hate the way I ramble through this node. But I don't want to throw it away, and I know my code is screwed up. But I think the basic points I wanted to make come through, so here goes. Notes: [1] Here's where I first show my perl OO ignorance. I'm certain that my syntax here is screwed up, but I think this'll get the point across. [2] I'm just going to assume you have a hash of option_type variables called "options", as I don't know where you're storing them... ...roboticusIn reply to Re: A first attempt.. at OO perl
by roboticus
|
|