Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: More parameters vs more methods

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    403
    Thanks
    0
    Thanked 72 Times in 59 Posts

    Default More parameters vs more methods

    Hi there.

    I have a theoretical question about good coding practice.

    Lets assume I have some kind of GUI component class. This component has a lot of functionality and I would like it to have many attachment points for various listeners.
    Now, should I rather do this:
    	public void add_some_listener(Listener l);
    	public void add_someOther_listener(Listener l);
    	public void add_yetAnother_listener(Listener l);
    (please ignore the lack of camel-casing; underscores are just used for easier reading)

    or would this be more user-friendly:
    	public void addListener(ListenerType type, Listener l);
     
    	public static enum ListenerType {
    		SOME_LISTENER,
    		SOME_OTHER_LISTENER,
    		YET_ANOTHER_LISTENER;
    	}

    Then, in comparison, the user could would use it like this:
    	public void initComponent() {
    		/*
    		 * First approach
    		 */
    		add_some_listener(someListener);
    		add_someOther_listener(someListener);
    		add_yetAnother_listener(someListener);
     
    		/*
    		 * Second approach
    		 */
    		addListener(ListenerType.SOME_LISTENER, someListener);
    		addListener(ListenerType.SOME_OTHER_LISTENER, someListener);
    		addListener(ListenerType.YET_ANOTHER_LISTENER, someListener);
    	}

    What do you think is more "clean" and easier to use?

    I mean, the second approach would cut down the number of methods; the documentation would be shorter, easier to read, easier to memorize.
    On the other hand the enum adds some more code to all of that; you have to simply write more stuff. There is also the problem about passing "null" as an argument.

    What should I do? Is this simply a matter of taste or are there any factual benefits to either of those solutions?

    Thank you very much.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,181
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: More parameters vs more methods

    IMHO, the first approach is much better. For an example of the second case, take a look at the Calendar class. But like most things, this is going to depend entirely on your context.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Methods with parameters and return values help
    By andrag3k in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 15th, 2013, 09:37 PM
  2. Scope and parameters
    By irishkid in forum Java Theory & Questions
    Replies: 1
    Last Post: December 8th, 2012, 08:52 AM
  3. Interface parameters?
    By markjava in forum Java Theory & Questions
    Replies: 3
    Last Post: November 18th, 2012, 07:21 AM
  4. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  5. Help using parameters
    By white97 in forum Object Oriented Programming
    Replies: 6
    Last Post: August 9th, 2011, 07:28 PM