Naming intefaces and classes

Discussion in 'Programming' started by robertlit, Apr 1, 2020.

  1. I have an interface let's call it Apple with some methods, then I want to implement this interface, and what object could implement Apple if not an apple? So then I would just go an create a class Apple, but that's not possible. What I would do is change the name of the class to IApple. What would you do?
    (I know that it does not really matter, but I want the code to be as clean as possible, so am asking your opinion)
    #1 robertlit, Apr 1, 2020
    Last edited: Apr 2, 2020
  2. Well, usually an interface describes a part of the object so that it is reusable. So for a class Apple, you could have an interface Edible with methods that relate to eating edible things.

    If you are using interfaces for an API, or as some kind of core module, so you can reuse code for multiple platforms, that isn't always the case, however. In that case I usually go with giving interfaces the normal name and the implementation that same name plus "Impl". I think giving the implementation an 'odd' name, rather than giving your interface an 'odd' name (i.e. IInterface) works better, since you'll often want to specify the interface as your parameter, return value, etc. and not your actual implementation, since your implementation will likely have more data than your interface (and to make your code more abstract, you want to have access to as little data as needed).
  3. You could always opt for AppleImpl, however I would choose something like Stef's choice of Edible.