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?