I have an Entity Student with following properties - (name, joinedOn, birthday, age, batch, etc.) and a function fetchStudents(<params>). I want to fetch students based on multiple filters.

In my method I have two ways to pass filters.

  1. Pass all filters as params to the method
  2. Make a class StudentCriteria with filters as fields and then pass the object of this class

While working in java I always go with the second option but recently I'm working in php and I was advised to go with the first way. I am unable to figure out which way is better in maintaining the code, reusability and performance wise. Thanks.

  • it depends on how many params you need and how optional they are Dec 9, 2012 at 18:19
  • There could be around 5 to 12 params and all are optional.
    – Shwetanka
    Dec 9, 2012 at 18:24

3 Answers 3


I personally don't like having too many optional arguments if I can get away with it. I can find it quite hard to read and when you end up passing things like

getOption($option1, "",0,"")

where the method declaration is actually something like (of course real parameter names would be better named)

getOption($myoption, $textStr, $numberOf, %description = "", $name = "", $typeOf = 0)

It's hard to know from reading this what exactly the values are and also what you are setting or leaving out. Every time I want to call that method I have to be very careful to set the right parameters in the right place.

A possible alternative is to use something like the Builder pattern where you are building up the criteria you wish to add explicitly. For those parameters which are required you make part of the constructor. For those optional ones you have a method to add.

Something like:

class StudentEnrollmentCriteriaBuilder {
   function __construct($id) {
      // id is always required for this criteria

   function getCriteria() {
      // return the built up criteria

   function addNameFilter($name) {
      // add the name to the filter
      return $this;

   function addSexFilter($sex) {
      // add sex type as a filter

      return $this;

   // etc

I guess you could then use it like

$criteria = new StudentEnrollmentCriteriaBuilder ($id);

$query = $criteria.addNameFilter('bob').addSexFilter('M').getCriteria();

// do stuff

Each method would be responsible for adding the filter if valid. So I guess in the case of the name if it was an empty name you would not add it as a filter etc as it wasn't required, or throw an exception etc etc

If it came down to it, where there are many properties I prefer the object option. If some are required at least make them part of the constructor. If you at least made the optional ones method based you could add validity checking on the data at that point, or leave it up to the user of the object to do.

Just my 1 1/2 cents.

  • 1
    @Shwetanka thanks for the acceptance but as a personal suggestion you might want to wait at least a day to see if you get better answers before accepting. Just to give the question a bit of time to get out there and people a bit of time to digest what your asking and offer good (better) feedback...
    – dreza
    Dec 9, 2012 at 19:44

I tend to use optional parameters and have some function filters to only check against the parameters I pass to the function. It will avoid the hassle of having a StudentSearchCriteria class, while keeping function calls fairly short.


"Clean Code" says you have two have as few parameters as possible.

I use up to three parameters.

I recommend you use the StudentSearchCriteria class.

  • 4
    I don't see what option 3 offers over option 1. It's just wrapping them in a dictionary for the sake of reducing the number of parameters. And, in type-safe languages, you lose your compiler protection. In languages that offer auto-completion, you lose that. All because you've read that less arguments = cleaner code. I really don't think that's what Uncle Bob meant. At least I hope not.
    – pdr
    Dec 9, 2012 at 18:50
  • @pdr The dictionary idea is mine, Uncle Bob didn't mean it. Giving a second thought, you are right. I changed the answer. Dec 10, 2012 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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