Tuesday, May 3, 2016

When to use Inheritance

Inheritance is useful in cases where the subclasses benefit from inheriting both implementation and definitions of the superclass, and the system as a whole is simplified (not complicated) by it.

One of the most common usages of Inheritance is for user interface controls. RichTextBox for example contains all the properties, internal fields and methods as a simple TextBox. In addition, it exposes some extra properties which enable richer editing capabilities. Using inheritance in this case makes more sense than any other practice. It reduces code size and code complexity.

Miss-usages of inheritance often result in a tightly coupled and overly complicated code. There are some good practices that help elevating this problem. The use of composition and interfaces instead of inheritance is strongly preferred, and when inheritance is used it’s recommended to keep inheritance graph as shallow as possible to avoid the Yo-yo problem (where the programmer has to keep flipping between classes in order to follow the control flow of the program).

Unfortunately, there’s no simple way to avoid these miss-usages other than through a strict code review process.

Java, C# and other high level languages eliminated the worst ‘feature’ of inheritance: Multiple inheritance. The most notorious problem associated with this feature is the "diamond problem”, which arises when two classes B and C inherit from A, and class D inherits from both B and C.

​In addition, the Yo-yo problem is made much worse when Multiple inheritance is used.

Sunday, April 24, 2016

Windows: 32-bit, 64-bit, 128-bit?

The switch from 16 to 32 bit made perfect sense since the memory requirement of many applications was already above the limits of 16 bit address space (2^16 = 64 KB), and the extra memory space (2^32 = 4GB) was aligned with the physical memory range of most mainstream computers.

In contrast, the switch from 32 to 64 bit has been proven beneficial to far fewer applications. The benefits of the wider registers are limited to applications that require heavy math computations such as encryption software and the like. And while the larger address space (up to 2^64 = 16 Exabyte!) is useful for applications that benefit from more than 3/4GB (XBOX One for example uses 8GB), it also come at the expense of data locality (the processor cache didn't grow with the address space) and scalability .

Since with 64 bit we are already hitting the limits of practical processors, switching to 128 bit and introducing yet another WoW (Windows on Windows) compatibility layer (on top of the existing WoW64 sub system) makes even less sense, especially for the desktop.

Note that a decade after 64-bit computing arrived to the desktop, extra large applications like Outlook and Visual Studio still fit, and are better off in 32 bit space.

The Visual Studio team has been having the same conversations around taking the leap to 64 bit space in the past 10 years, and the jury is still out. Outlook supports a 64 version, but only to allow 64 bit extensions, not for the benefits of the extra RAM. The Edge browser 64 bit version is installed by default on 64 bit hardware only for security reasons, at its core it’s optimized for data locality and making very economical use of memory (Edge’s main process is 1/4 the size of IE’s main process and 1/2 the size of Chrome’s main process) .

The fact of the matter is that when it comes to space, less is more. Applications that use less memory space (without increased use of virtual memory / swapping to disk) run faster and scale better (physical RAM in mainstream computers/tablets ranges somewhere between 1-24 GB) .

Perhaps a truly groundbreaking innovations in hardware, power consumption and (followed by) computing will allow to use that space to process the entire World Wide Web data, from a single computer running Windows...If that would be the case, I bet that the 128 bit bus will be used to access a component completely different from a silicon memory chip.

If disk is the new tape, memory is the new disk, this 128 bit capable beast would be the new memory.