in reply to Re^2: Ordering of parameters - Re^9: Converting Unicode
in thread Converting Unicode
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Ordering of parameters - Re^9: Converting Unicode
by NERDVANA (Priest) on Dec 27, 2023 at 06:55 UTC | |
I see objects as tool to organize data and related functions, and not much more. I don't really subscribe to the "greater theory of objects" that languages like Java force on people; maybe that's what you're referring to. I think pretty much any code is going to run into a pattern that could be helped along by some structure, though. Your code provides a good example, actually. You seem to have a form that allows up to 5 instances of 9 search parameters (plus a yes/no 10th parameter for each instance after the first), multiplying out to 49 variables. You then end up repeating a lot of your code, once for each set of 9 parameters. Consider if you group those into hashrefs, for each set of search parameters:
Now, the caller has to do a little more work to build that array of hashrefs to call this function. But, you were probably handling form-submission parameters by name anyway, so you can probably build them in a loop. The savings come when you start to use those:
The code here only got moderately shorter in number of keystrokes, but to my eyes it got a lot less error-prone, and easier to maintain. (and being perl, of course you have the option to code-golf this down to fewer characters...)
but use whichever one you think reads easier for you or a future maintainer. Your function composeRegex uses 8 of these 10 hashref keys. Instead of spelling out all 8 arguments to the function like
You could just pass the whole "search" hashref to the composeRegex function:
And, just like that, "composeRegex" has become a function that operates on "search parameter hashrefs" which is basically a few tiny steps away from being a method that operates on "search parameter objects". I didn't actually use any of perl's object-oriented features, yet, either. But that's the concept. You have a group of data (the "search parameters") and you have functions that need some or all of the search parameters, and those functions would become the methods of your search parameter object. | [reply] [d/l] [select] |