Less Encapsulation With Object-Oriented Programming

A look at the apparent contradiction of following object-oriented principles leading to tight coupling.

type Writer interface {
Write(p []byte) (n int, err error)
}
Chmod(mode FileMode) error
Chown(uid, gid int) error
Fd() uintptr
Name() string
Stat() (FileInfo, error)
...
Stat() (FileInfo, error)
Chown(uid, gid int) error
engine := "RD-180"
thrust := 3830
fmt.Fprintf(file, "%s has thrust %0.1f\n", engine, thrust)
RD-180 has thrust 3830.0
file.Fprintf("%s has thrust %0.1f\n", engine, thrust)
  1. You got potentially 10 new methods which could be modifying the internal state of your file object. You need to keep track of all of them.
  2. If you decide to modify the internal representation of the File type, then there are 10 extra methods which have to be reviewed to consider impact of this change.

The Java Non-Solution

Java has tried to solve this problem by factoring out a lot of this shared functionality into separate objects. The problem as we saw in my previous article is that this simply adds a lot of complexity. You end up creating wrappers around wrappers around wrappers. This in turn creates complex state management: Which object should call close?

scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text())
}

The Unix Pipeline

One of the reasons Unix commands such as ls and cat can be so easily combined in so many ways is because the interface between them is very minimalist. It is just text and it tends to be line oriented. The tools typically process text on a line by line basis. That is an interface which is quite easy for any tool to match.

Universal Abstractions

There is some of the same elegance in LISP in that everything is a list and you have this wide variety of tools to manipulate lists. Object-oriented thinking dispensed with this kind of idea of some broad universal abstractions which could be utilized in a wide variety of domains.

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store