Monday, June 6, 2016

OpenBSD's Dirty Little Secret

OpenBSD is often advertised by its developers as being a highly secure operating system. One of their goals is to be "the #1 most secure operating system".

The home page proudly states:
 Only two remote holes in the default install, in a heck of a long time!
That sounds fantastic! But is OpenBSD really all that secure in practice? The statement on the front page needs to be read carefully.
Only two remote holes in a default install.
When you consider that nearly every network service is turned off by default, it's not really that impressive. The OpenBSD team also does not consider denial of service attacks to be a security exploit; only a reliability issue. The event that lead to the second remote hole in a default install was denied as a problem until proof of concept code showing arbitrary code execution was provided.

Most security vulnerabilities are going to be from services not part of a default install. When vulnerabilities are found, it's important to have a patch available. How does OpenBSD handle this? First, you have to consider that there are two ways software is typically added to a default OpenBSD system. Ports and packages. According to the FAQ:
In general, you are highly advised to use packages over building an application from ports.
Okay, so the OpenBSD team themselves are telling people to use packages. But are they kept secure? Nope.
When serious bugs or security flaws are discovered in third party software, they are fixed in the -stable branch of the ports tree. Note that binary packages for -release and -stable are not updated.
There is a solution to this problem if you are willing to trust a non-official source. M:Tier provides binary updates for the latest OpenBSD release.

Security is about understanding risk. If you think "I'm using OpenBSD, therefore I'm secure", then you don't understand the risks. OpenBSD has been an innovator in many areas. I give them credit for this. But as a user, you need to carefully understand what OpenBSD means when they talk about security and what it actually provides.

Wednesday, March 23, 2016

Micro-Classes are an Anti-Pattern


A micro-class, micro-method, or micro-function is a unit of code that accomplishes such a small amount of work that it can be said to have no useful functionality on its own. Instead, the work it's meant to perform is repeatedly delegated to other micro-units and obscures what task is really being performed.

It is important to note that abusing micro-classes is NOT the same as merely breaking down a problem into smaller parts (a good programming practice). Instead, the use of micro-classes takes that idea to such an extreme that even the simplest of tasks are divided and spread out so thinly that it becomes difficult to follow the logic of the program. Think of it like a book being broken down into chapters and sub-chapters versus a choose your own adventure book where you constantly have to flip pages. Reading code that makes heavy use of micro-classes is like having to flip to a different page for every sentence.

Because I primarily work as a Java developer, I will be using the term micro-class. However, my arguments apply equally to micro-functions for those not using OOP.

Micro-classes are a bad idea. We all know that God objects are an anti-pattern. I think the industry has been very successful in getting that point across. I even meet a lot of beginner developers who are familiar with the harm caused by really long methods that try to do everything. However, I think that now things are going too far in the other direction. There are now many software developers who go to such extremes to break things up into smaller units that the code base as a greater whole becomes incomprehensible.

Why Micro-Classes are Harmful


1. Micro-classes harm architecture.

When creating an architecture for your application, you think about what packages and layers you need. If you divide your program into these microscopic units, you will eventually find yourself in a situation where a micro-class doesn't have a logical place to live. You either stick them into some "util" package or, if you are really creative, you spin it off into an entirely separate project and you have your application code masquerading as a "library", despite not containing anything useful outside of the one application that will ever import it. Hard coded assumptions about the calling application are often embedded in this faux library. Additionally, the project is now composed of so many of these little utilities classes, that it becomes difficult to find the places where the real work happens.

2. Micro-classes harm API familiarity.

Code is easier to read when it uses APIs that programmers are already familiar with. In applications that abuse micro-classes, the common APIs are hidden at the lower layers and often unneccessarily wrapped. This creates a code base where the business logic is composed entirely from an unfamiliar API which only exists in that one application. This makes code harder to read because one must learn and understand this new and unfamiliar API in order to understand what the code is doing.

3. Micro-classes make change more difficult.

The typical argument in favor of micro-classes is that they enable change. I think there are flaws with this argument. The first flaw is that most systems are not well designed and only allow for particular kinds of changes. As soon as a requirement change occurs that was not anticipated, then the change is not any easier to make. Secondly, even in situations where micro-classes do enable the kind of change one would expect, the micro-class style of programming requires very advanced knowledge of the system in order to successfully make the change. In theory, one might be able to only change a couple lines of code. In reality, most programmers outside of the original developer won't understand the system well enough to do it. Instead, they struggle through it and end up with ugly hacks to accommodate the requirements change. I put forward the argument that while a more cohesive system requires a greater number of source lines to be modified, they are often contained in a fewer number of places and the change is actually easier to make.

4. Code which relies on micro-classes has a high level of indirection.

When you read code composed of micro-classes, it is difficult to understand and explain to others. In an application composed of micro-classes the steps needed to accomplish simple tasks are spread out across the system. I've worked on programs where it was not uncommon for me to end up with 20+ tabs open in my editor as I navigated through the web of files to see how a relatively simple piece of functionality worked.

Signs of a Micro-Class


How can you tell the difference between a micro-class and a proper class which neatly sub-divides a larger problem? Sometimes the distinction isn't always clear. I would even give some leniency and say that a few micro-classes in an application are acceptable. Their overuse is what causes major maintenance problems.

1. Micro-classes are often used only once. If you find a class which is never used by more than one caller, it may be a micro-class.

2. Micro-classes tend to reinvent the wheel. They will act as nothing more than wrapper classes which do little but provide a slightly different interface for doing the same functionality as another class.

3. Micro-classes tend to abuse design patterns. While design patterns aren't necessarily bad, one should have a rational for using them. Code that abuses micro-classes often does so in order to implement a design pattern for a relatively simple piece of functionality where the design pattern was overkill and not needed.

4. Micro-classes tend to be poorly named or invent new vocabulary. Because the code gets spread so thin, developers must think of new names for all the additional classes and functions. The names are often poor at describing the purpose of the class.

Conclusion


I'm not arguing in favor of god objects. I think there is a healthy middle ground between 2000 line methods and 500 methods of 4 lines each. At some point, developers need to learn when to stop sub-diving a problem and perform the task they want to accomplish inline. I also don't believe this is a violation of the Single Responsibility Principle (SRP). It's just taking the idea of what constitutes a "single responsibility" and viewing it a higher level.