/ DEPENDENCY INJECTION, LANGUAGE, OBJECT-ORIENTED PROGRAMMING, SEMANTICS

Semantics: state or dependency, not both

The more I program, the easier it gets. However, the more questions arise regarding programming.

This week, my thinking was about Object-Oriented Programming. I’ve been told that OOP is about encapsulating state and behavior is a single isolated unit.

In languages I know, state translates into attributes and behavior into methods.

public class Cat {

    private Color color;

    public void mew() { ... }
}

That said, unit testing requires the ability to test in isolation: this has been enabled by Dependency Injection (whether manual or framework-based). Dependencies are also translated into attributes, though most (if not all) never change through the instance lifecycle.

public class MechanicalCat {

    private final RepairmentManager repairmentMgr;

    public MechanicalCat(RepairmentManager repairmentMgr) {
        this.repairmentMgr = repairmentMgr;
    }
}

Now, we have both state and dependencies handled as attributes. For Java, it makes sense because Dependency Injection became mainstream much later than the language. More modern JVM languages introduced the concept of val, which makes an attribute immutable. Here’s a Kotlin port of the above code:

class Cat(var color:Color) {

    fun void mew() { ... }
}

class MechanicalCat(val repairmentMgr:RepairmentManager)

However, this semantics is only about mutability/immutability. Java already had the same semnantics with a different syntax - the final keyword.

I was wondering if we could have different semantics for state and dependencies? Any language creator reading this blog and thinking this might be a good idea to separate between them?

Nicolas Fränkel

Nicolas Fränkel

Nicolas Fränkel is a Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Currently working for Hazelcast. Also double as a teacher in universities and higher education schools, a trainer and triples as a book author.

Read More