# Keep your interfaces simple

DevFeed: [Keep your interfaces simple](<https://devfeed.tech/articles/keep-your-interfaces-simple-25892.md>)

Original publisher: [Read original article](<https://proandroiddev.com/keep-your-interfaces-simple-e025d515e3b9?source=rss-1331e67af4e1------2>)

Author: Danny Preussler

Published: 2020-07-16T07:56:26Z

Content type: tutorial

Language: en

Sources: [Stories by Danny Preussler on Medium](<https://devfeed.tech/sources/stories-by-danny-preussler-on-medium.md>)

Topics: [interfaces](<https://devfeed.tech/topics/interfaces.md>), [API](<https://devfeed.tech/topics/api.md>), [feature flags](<https://devfeed.tech/topics/feature-flags.md>), [Mocking](<https://devfeed.tech/topics/mocking.md>), [Code](<https://devfeed.tech/topics/code.md>), [implementation](<https://devfeed.tech/topics/implementation.md>), [Java](<https://devfeed.tech/topics/java.md>), [Extension](<https://devfeed.tech/topics/extension.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [api](<https://devfeed.tech/tags/api.md>), [code](<https://devfeed.tech/tags/code.md>), [extension](<https://devfeed.tech/tags/extension.md>), [feature-flags](<https://devfeed.tech/tags/feature-flags.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [interfaces](<https://devfeed.tech/tags/interfaces.md>), [java](<https://devfeed.tech/tags/java.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [mocking](<https://devfeed.tech/tags/mocking.md>), [tests](<https://devfeed.tech/tags/tests.md>)

## AI overview

This article explains how implicit connections between interface methods can create hidden contracts that every implementation and mock must honor. It uses Java List methods, the equals/hashCode contract, and feature flags to argue for simpler interfaces, extension functions, and stub implementations where appropriate.

## Source excerpt

Avoiding implicit connections and learn how extension functions can help you writing good APIshttps://unsplash.com/photos/xxeAftHHq6E Writing your classes with a good API is hard but important. As the writer is trying to make it easy for the user, we sometimes tend to repeat ourselves by adding convenient methods. Think about the List interface in Java. To check if there are no elements in the list we could check list.getLength() == 0 or we simply ask for isEmpty(). The 2nd one reads much better. But it also adds a duplication and implicit connection between the two: If the list is empty, it can't contain any elements! This must be respected by every implementer of the interface! We can easily think of many other methods with implicit dependencies. Think about how hashCode and equals have a connection. This is stated in the Javadocs: Note that it is generally necessary to override the hashCode method whenever equals is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes. This is a pitfall that can be difficult to avoid and can lead to issues elsewhere that are difficult to track down and fix. Nowadays we have tools to validate this contract, or even better, to generate the implementation. Another example: feature flags A lot of developers work with features flags. These enable us to release continuously without the need for long-lived feature branches. Let's say we have an interface like this: interface AppFeatures { fun isEnabled(feature: Feature): Boolean } When using this I realized I often write code like: if (!isEnabled(Feature.SomeFeature)) "not is enabled" does not read nicely though. But I want the reader to understand my code without thinking too much. Therefore my initial thought was to add another method to the interface fun isDisabled(feature: Feature): Boolean But I realized, doing this might break a lot of tests that simply mock the interface: val appFeatures = mock<AppF