4

I would like to know what is a more appropriate way to code in Java. Is it generally better to pass entire objects in the method's parameters or just using the fields from the class?

  1. Using the field:
public class Dictionary {
    ArrayList<Definition> list;

    public Dictionary() {
        this.list = new ArrayList<Definition>();
    }

    public void newEntry(String key, String value) {
        this.list.add(new Definition(key, value)); 
    }
}
public class SampleTests {
    @Test
    public void Tests()
    {
        Dictionary d = new Dictionary();

        d.newEntry("Apple", "A fruit");
    }
}
  1. Passing an object:
public class Dictionary {

    public Dictionary() {
        //this.list = new ArrayList<Definition>();
    }

    public void newEntry(String key, String value, ArrayList<Definition> list) {
        list.add(new Definition(key, value)); //I'm not using field from this class!
    }
}
public class SampleTests {
    @Test
    public void Tests()
    {
        Dictionary d = new Dictionary();
        ArrayList<Definition> list = new ArrayList<>();

        d.newEntry("Apple", "A fruit", list);
    }
}

I've checked this related question, but the guys' answers do not help me much

5

One of the principles of object-oriented programming is encapsulation. It says that one should hide the internal structure of the object and prevent direct access to it.

In your case this means that those who use your class should not "know" whether your class Dictionary uses ArrayList or Map or something other to store elements.

That's why option 1 is better.

One of benefits of such approach is that you can change the implementation of the class Dictionary whenever you wish, e.g. you may decide to use a tree structure instead of list, and all who use your class will not need to modify their code.

1
  • Thank you for the thorough answer
    – max
    Mar 5 at 6:15
5

Option 1, without question.

The whole point of your Dictionary class is to encapsulate the list and any other information you need to make your dictionary work. You already have access to the class's fields, so passing them as parameters is pointless.

Your Option 2 exposes the internal details of your class, defeating its purpose.

4
  • nice. I have a C background, that is why OOP seems hard sometimes
    – max
    Mar 5 at 6:14
  • 1
    I must disagree — option 2 does not expose any implementation details of Dictionary class as it has none. Option 2 is just a glorified container for a stand-alone procedure to add “entry” to the list. Both list and entry details are provided external to the procedure and only “implementation detail” there is lives inside the addEntry method that itself could easily be moved to another class without any code inside the method breaking. Mar 11 at 19:27
  • @RolandTepp: I would argue that Option 2 doesn't provide any value or benefit over using the ArrayList directly. At least option 1 includes the encapsulation that will be needed if it's intended to be expanded into a fully functional Dictionary. Mar 11 at 19:29
  • @RobertHarvey you misunderstand me. I was not really arguing against option 1. I was simply pointing out that with option 2 we can not talk about encapsualtion at all as there is no state to encapsulate. Mar 18 at 20:21
0

In real life, very often you don’t just add an entry to a list, but it has consequences. Like an application could be displaying the entries of a list, and adding something needs to trigger a whole sequence of events. If you tell the object to add an item, it can take care of everything. If you insert an item yourself into the list, that can have negative side effects. And if not know, then next year.

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.