Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8261601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:27:54+00:00 2026-06-08T03:27:54+00:00

Imagine you have this class class Ai1ec_Less_Parser_Controller { /** * @var Ai1ec_Read_Variables_Startegy */ private

  • 0

Imagine you have this class

class Ai1ec_Less_Parser_Controller {
    /**
     * @var Ai1ec_Read_Variables_Startegy
     */
    private $read_variable_strategy;
    /**
     * @var Ai1ec_Save_Variables_Strategy
     */
    private $write_variable_strategy;
    /**
     * @var Ai1ec_Less_Variables_Collection
     */
    private $less_variables_collection;
    /**
     * @var Ai1ec_Less_Parser
     */
    private $ai1ec_less_parser;
    /**
     * We set the private variables in the constructor. I feel that there are too many parameters. 
     * Should i use setter instead and throw an exception if something is not set?
     * 
     * @param Ai1ec_Read_Variables_Startegy $read_variable_strategy
     * @param Ai1ec_Save_Variables_Strategy $write_variable_strategy
     * @param Ai1ec_Less_Variables_Collection $less_variables_collection
     * @param Ai1ec_Less_Parser $ai1ec_less_parser
     */
    public function __construct( Ai1ec_Read_Variables_Startegy $read_variable_strategy,
                                 Ai1ec_Save_Variables_Strategy $write_variable_strategy,
                                 Ai1ec_Less_Variables_Collection $less_variables_collection,
                                 Ai1ec_Less_Parser $ai1ec_less_parser ) {

    }
}

I need those variables to be set and so i set them in the constructor ( but that look like too many parameters ). Another option would be to use setters to set them and then in a method throw an exception if one of the required variables is not set like this

public function do_something_with_parser_and_read_strategy() {
  if( $this->are_paser_and_read_strategy_set === false ) {
     throw new Exception( "You must set them!" );
  }
}  
private function are_paser_and_read_strategy_set () {
  return isset( $this->read_variable_strategy ) && isset( $this->ai1ec_less_parser );
} 

Do you think that one of the two methods is better?And why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:27:57+00:00Added an answer on June 8, 2026 at 3:27 am

    Class naming Controller somehow reflects MVC, or in general – any mechanism responsible for processing sequence.

    Data object classes tend to have many fields in any case – it is their responsibility.
    Regular object relying on many other objects could be possibly missing a point.

    There are four objects, as I see: read, save, parse and provide collection interface to something.
    Why shall one have different interfaces for reading and writing? Could this not be combined into one?
    Parser shall be a library on itself, thus there may be no reason to combine it anywhere, although it could possible use readers/writers for itself, and, in return, provide collection. Thus could it be possible that parser would take an argument of reader and return a collection object?

    That is more about specific case.
    In general – having many arguments to the method (or initializing many fields within an object by other objects of different domains) indicates some sort of design flaw.

    Kind of on-topic might be this article on Constructor Initialization – it advises to use in-constructor initialization. Just be sure to follow up to the point:

    What if there’s a lot of collaborators to provide in the constructor? A large list of construction parameters, like any large
    parameter list, is a CodeSmell.

    And as Ray has written – there is a possibility to initialize using setters, and there is article on that too. To the extent of my view – I think that Martin Fowler really summarizes these cases pretty well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have this: public class Animal { private String racaAnimal; private String corAnimal;
Imagine we have a class like this: class Testee { public: void Func() private:
Imagine I have this code: var myFunc1 = function(event) { alert(1); } var myFunc2
Imagine you have this model: class Category(models.Model): node_id = models.IntegerField(primary_key = True) type_id =
Im playing a little bit with heavy-client app. Imagine I have this model: class
Imagine I have this class namespace CommonLibrary { public class Report() { public DateTime
Let's imagine I have this class : public class FileData { public Weigth {
So, imagine I have this model: class Car has_one :engine end and the engine
Imagine I have a test like this: class MyUnitTest < Test::Unit::TestCase def test_first #
Imagine I have two class A and B defined like this : class A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.