-2

There is a HashMap instance. The only thing I will do with the instance is to call values() method described in the Map interface HashMap implements.

So I won't use any other possibilities interfaces provide.

Should I than declare the variable type of HashMap according to YAGNI and KISS or should I code to the interface and declare the type as Map?

Example of coding to the interface:

Map<Integer, String> langMap = new HashMap<>();

Example of class variable:

HashMap<Integer, String> langMap = new HashMap<>();
danielorn
  • 5,254
  • 1
  • 18
  • 31
Tom Smykowski
  • 25,487
  • 54
  • 159
  • 236
  • Thank you for the link but it contains information about what is programming to an interface. While my question is about if in this example i should follow this rule or YAGNI and KISS since interfaces are not used here – Tom Smykowski Jan 24 '21 at 19:13
  • The second answer in that link answers, IMHO, quite well *why* you should program to an interface. – Federico klez Culloca Jan 24 '21 at 19:15
  • I have red it and didn't find the answer. It proves programming to interface is good per se. And I agree with it. But it does not answer if I should program to interface when it is not (yet) needed. – Tom Smykowski Jan 24 '21 at 19:18
  • I don't understand why that doesn't answer your question. The point is: you should program to an interface unless you have a good reason *not* to. I don't see any good reason not to, in your example. – Federico klez Culloca Jan 24 '21 at 19:19
  • Do you think YAGNI and KISS are not good reason to not use an interface in this example? – Tom Smykowski Jan 24 '21 at 19:22
  • @TomaszSmykowski if you ain't gonna need methods specific to `HashMap`, use the interface. If you restrict yourself to methods on the `Map` interface, it's simpler. – Andy Turner Jan 24 '21 at 19:27
  • 2
    @TomaszSmykowski the problem, I think, is that you think of it as something that is not the default way to code. But if you think of it as the default way to code, *not* programming this way would be a violation of KISS or YAGNI. – Federico klez Culloca Jan 24 '21 at 19:30
  • Ok. I understand now with the your comment and the answer provided. Thanks – Tom Smykowski Jan 24 '21 at 19:36

1 Answers1

3

Whether to program against the interface or the concrete implementation should be determined by whether you need to access methods specific to HashMap that is not part of the interface. Using the Map interface is like saying "I need functionality as specified by the Map interface, but does not really care how it is implemented." Using the HashMap class directly is like saying "I need the functionality in Map and it needs to be implemented as a HashMap".

Programming towards the interface is not violating neither KISS nor YAGNI, one could actually argue the other way around

  • It is simpler (KISS) to program against a generic interface than a specific implementation.
  • If a specific implementation is not needed, you should use the generic interface (YAGNI)
danielorn
  • 5,254
  • 1
  • 18
  • 31