2

The question sounds retrograde but I have about 50 variables being passed to a class. Basically an array of data I call 'filters' that is first passed to a prepFiltersMethod($filters). This prep filters method does some things sets some more filters needed in other methods for queries. It returns a larger subset of data of $filters after getting some additional things etc..

EDIT: This is HOW to control data type of question, I tried to clean up the question a bit, in short I'm asking how to handle a large input array into a class. if Junk data is passed along into it or deficient data, should we try to manage it. What I'm asking is if you have a big array coming into a class, would you take the extra step to write it into a new array to ensure it is the RIGHT data etc. is used for control purposes ? Weed out keys that shouldn't end up in it etc.? Say, take $filters array, pick out the keys we wanted and write it into a new array, and then use that ? In short I'm wondering if it would be helpful to use a builder to do this or how other people would handle. If we do nothing it works fine but may be a risk later down the road?

OVERVIEW:

  • Say we have a multi dimensional array called $filters.
  • $filters is being passed to a class (contains the details of a user request).
  • The $filters class goes through a prepare method that adds a few more items to this $filters array.
  • Now that the filters are prepared and checked, it will be used to build query and return results from another method. $filters is passed to getInventory method that looks at the filters and builds out a query and returns data from the DB.

QUESTION:

As it stands, $filters array is being passed around growing and shrinking as needed. Anything can be passed to the class with no gatekeeping, Should this be rectified? In other words, the method that first prepares the $filters array, should it copy the variables into a new array (e.g. build a new array) ?

Someone passes in :

$filters['x']
$filters['y']
$filters['z']
... ... ...

fine enough those are all needed/usable, but what if they pass in

$filters['x']
$filters['a']
$filters['b']
... ... ...

where 'a' and 'b' are not needed ?

Do we care about this ? should I employ something that ensures that the input data is more complete?

1) builder pattern? 2) create a new array and re-write them ? 3) Not worry about, let the other methods ignore data it doesn't need? 4) A standard class that can get and set 5) A global array in the class $this->filters (hate this idea) 6) Other ideas ?

  • 1
    Going with a builder object or Domain Specific Language (a buzz word you can look up) is probably the best route. What is the cost of refactoring this code? – Greg Burghardt Jul 9 '18 at 2:10
  • @DocBrown I'm sorry. Ignore FiltersStruct, what I'm asking is if you have a big array coming into a class, would you take the extra step to write it into a new array to ensure it is the RIGHT data etc. is used for control purposes ? Weed out keys that shouldn't end up in it etc.? Say, take $filters array, pick out the keys we wanted and write it into a new array, and then use that ? In short I'm wondering if it would be helpful to use a builder to do this or how other people would handle. If we do nothing it works fine but may be a risk later down the road. – Mike Q Jul 9 '18 at 17:02
3

If I got your right, you have a bunch of unstructured data to be passed around, and since you now see this will cause issues, you try to give it some local structure just for calling one method. Of course, this approach may not be worth the effort.

I think the only sensible solution to this is to add more structure to your data not just for prepFilters. Make your $filters a properly structured DTO or class, maybe hierarchical structured, right from the start. An array or dictionary is not the best data structure for passing around ~50 different values of different type and purpose around, dissecting it into a handful of small classes can make your code much more maintainable.

The attributes need proper names, and you should get an error by the compiler or at least from the run time system when you introduce a typo into an attribute name and try to access a non-existing attribute.

That way, your data structures stay extensible, and you don't have to think about in which order the data comes in or will be returned. It will become much simpler to pass specific parts of the data around in your program, and prepFilters will probably not be the only function which will benefit from this.

  • 1
    I guess this is PHP which is one of those languages where the term "array" is used for both classic arrays (with integer based indices, like C arrays for example) and dictionaries (with some object as a key). Sometimes dictionaries are also called associative arrays. – null Jul 9 '18 at 18:34
  • 2
    Doc's proposal to split up the ~50 values into meaningful sub-groups is excellent, +1 Whether the subgroup is an Object, or just another map ("associative array") is an open topic, depends what makes sense. If you think that sub-groups makes a lot of sense and could be used elsewhere in the code, yes, make it an object. Otherwise a simple map is fine. – user949300 Jul 9 '18 at 19:34
  • I appreciate all of the comments. Thank you very much! – Mike Q Jul 10 '18 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.