Monday, May 1, 2017

Lessons in Abstraction: What FP Can Teach OOP

Abstraction is one of the greatest visionary tools ever invented by human beings to imagine, decipher, and depict the world. – Jerry Saltz

I wish to approach truth as closely as is possible, and therefore I abstract everything until I arrive at the fundamental quality of objects. ― Piet Mondrian

One of the most advocated "best practices" in programming is the DRY principle: Don't Repeat Yourself. Many techniques can be used to enforce this principle: encapsulation, parameterization, inversion of control, and many more. One of these techniques is abstraction and one main difference between functional programming (FP) and object-oriented programming (OOP) is the way abstraction is applied. A common practice in OOP is to limit abstraction to the strict useful minimum for the problem at hand. In OOP, premature abstraction is often considered a fault, much as premature optimization.

In FP, on the other hand, abstraction is generally pushed as far as possible. Every problem is broken into a series of the simplest possible functions, which are then composed to build the problem solution. Identifying these abstractions is generally the most important part of problem resolution. In fact, FP programmers often spend more time trying to find what problem they should solve than solving them. And of course, it generally appears that these functions are the same from one problem to the next. Only the way they are composed is different. This is the reason why abstraction is one of the most valued techniques used by FP programmers.

In this article, we will compare how OOP and FP would handle abstraction in a specific simple problem: computing the sum of integers between 1 and an arbitrary value n. The problem is so simple to solve using imperative programming that it seems there is nothing interesting to learn from it. Here is how it could be done in imperative Java:

static int sum(int n) {
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        sum += i;
    }
    return sum;
}

It seems that there is no simpler and more efficient way to do it. However, for an OOP programmer as well as for an FP programmer, the interesting part of the problem is still to come. But each of these programmer will probably go a very different route. We could also notice (and demonstrate!) that the sum of the n first integers is equals to (n * (n + 1) / 2). As you will see, this would be going the other way, towards the least possible abstraction.

Abstracting the Computation in Object Oriented Programming

Both FP and OOP programmers will immediately remark one possible abstraction, which is the operation that is applied to construct the result. Here, we are adding values to construct the result. What if we were asked to return the product instead of the sum?

In OOP, there are two obvious way to abstract the computation: using the strategy pattern, or inversion of control (IoC).

Using the Strategy Pattern

The Java way to abstract a computation in a form that can be handed to a program is to create an object containing the methods performing the computation. (If you're lucky and there is just one method, lambda expressions can make this simpler but the approach remains the same.) For this to be an abstraction, we have to make the abstract part into an interface and the concrete part into an implementation:

static int compute(int n, Computer computer) {
    int result = computer.identity();
    for (int i = 1; i <= n; i++) {
        result = computer.compute(result, i);
    }
    return result;
}

interface Computer {

    int compute(int a, int b);

    int identity();

}

static class Adder implements Computer {

    @Override
    public int compute(int a, int b) {
        return a + b;
    }

    @Override
    public int identity() {
        return 0;
    }

}

static class Multiplier implements Computer {

    @Override
    public int compute(int a, int b) {
        return a * b;
    }

    @Override
    public int identity() {
        return 1;
    }

}

public static void main(String... args) {
    System.out.println(compute(5, new Adder()));
    System.out.println(compute(5, new Multiplier()));
}

Here, we have abstracted the computation and are now able to reuse the program with a different computation strategy, such as the multiplication. Note that we put the identity element, used as the start value for the computation, into the various Computer implementations, since it will be different for different types of computations (0 for addition, 1 for multiplication).

Frankly, it does not seem like a great improvement, and many programmers would not see the interest of decoupling the computation from the iteration. Furthermore, this approach does not take into account that we have in fact two nested possible abstractions: The operation itself (addition or multiplication) and the identity concept of a specific value that is paired with an operation and does not change the value to witch it is associated through this operation. The value 0 is the identity for addition, and 1 is the identity for multiplication. Many other operations have an identity value. The empty string is the identity for string concatenation, and the empty list is the identity for list concatenation.

We could of course use the Strategy pattern again for the identity, but this would create a really messy solution, and most programmers would probably think that using an interface with two methods is enough abstraction. As you will later see, FP programmers do not think this way.

Using Inversion of Control (IoC)

Another more object oriented technique for abstracting the computation is called inversion of control. The strategy pattern solution uses composition to solve the problem whereas inversion of control is based upon inheritance.

With this technique, we construct an abstract class that performs the loop, calling an abstract method to get the start value and another one to do the computation:

static abstract class Computer {

    public final int compute(int n) {
        int result = identity();
        for (int i = 1; i <= n; i++) {
            result = doCompute(result, i);
        }
        return result;
    }

    abstract int doCompute(int a, int b);

    abstract int identity();

}

static class Adder extends Computer {

    @Override
    public int doCompute(int a, int b) {
        return a + b;
    }

    @Override
    int identity() {
        return 0;
    }
}

static class Multiplier extends Computer {

    @Override
    public int doCompute(int a, int b) {
        return a * b;
    }

    @Override
    int identity() {
        return 1;
    }
}

public static void main(String... args) {
    Computer adder = new Adder();
    System.out.println(adder.compute(5));
    Computer multiplier = new Multiplier();
    System.out.println(multiplier.compute(5));
}

This approach is probably the most object oriented one, and it might seem much cleaner than the strategy pattern because it seemingly also abstracts the iteration into the Computer class. (By the way, this specific use of IoC has been given a specific name: the template method pattern.) But in fact, we did not abstract the iteration! We just encapsulated the whole program in the computer class. Let's now see how a functional programmer would handle the problem.

Abstracting in Functional Programming

Continue reading %Lessons in Abstraction: What FP Can Teach OOP%


by Pierre-Yves Saumont via SitePoint

How to Write Shell Scripts with JavaScript

This week I had to upgrade a client's website to use SSL. This wasn't a difficult task in itself — installing the certificate was just the click of a button — yet once I had made the switch, I was left with a lot of mixed content warnings. Part of fixing these meant that I had to go through the theme directory (it was a WordPress site) and identify all of the files in which assets were being included via HTTP.

Previously, I would have used a small Ruby script to automate this. Ruby was the first programming language I learned and is ideally suited to such tasks. However, we recently published an article on using Node to create a command-line interface. This article served to remind me that JavaScript has long since grown beyond the browser and can (amongst many other things) be used to great effect for desktop scripting.

In the rest of this post, I'll explain how to use JavaScript to recursively iterate over the files in a directory and to identify any occurrences of a specified string. I'll also offer a gentle introduction to writing shell scripts in JavaScript and put you on the road to writing your own.

Set Up

The only prerequisite here is Node.js. If you don't have this installed already, you can head over to their website and download one of the binaries. Alternatively, you can use a version manager such as nvm. We've got a tutorial on that here.

Getting Started

So where to begin? The first thing we need to do is iterate over all of the files in the theme directory. Luckily Node's native File System module comes with a readdir method we can use for that. It takes the directory path and a callback function as parameters. The callback gets two arguments (err and entries) where entries is an array of the names of the entries in the directory excluding . and .. — the current directory and the parent directory, respectively.

const fs = require('fs');

function buildTree(startPath) {
  fs.readdir(startPath, (err, entries) => {
    console.log(entries);
  });
}

buildTree('/home/jim/Desktop/theme');

If you're following along with this, save the above in a file named search_and_replace.js and run it from the command line using node search_and_replace.js. You'll also need to adjust the path to whichever directory you are using.

Adding Recursion

So far so good! The above script logs the directory's top level entries to the console, but my theme folder contained subdirectories which also had files that needed processing. That means that we need to iterate over the array of entries and have the function call itself for any directories it encounters.

To do this, we first need to work out if we are dealing with a directory. Luckily the File System module has a method for that, too: lstatSync. This returns an fs.Stats object, which itself has an isDirectory method. This method returns true or false accordingly.

Continue reading %How to Write Shell Scripts with JavaScript%


by James Hibbard via SitePoint

Netil Radio

Minimal One Pager with a random color and icon pattern header on load for London-based radio station, Netil Radio.

by Rob Hope via One Page Love

Shrug

Shrug

Slick little Landing Page with a beautifully simple pricing table for Shrug, a simple project management tool.

by Rob Hope via One Page Love

Building Your Startup: Using Routes for Schedule With Me

How to Set Up a Facebook Messenger Ad

Do you want to boost revenue for your next launch or promotion? Have you thought about using Facebook Messenger ads to engage prospects? Messenger ads provide a personal experience for customers and prospects. In this article, you’ll discover how to create a Facebook Messenger ad. What Are Messenger Ads? A Messenger ad is a Facebook [...]

This post How to Set Up a Facebook Messenger Ad first appeared on .
- Your Guide to the Social Media Jungle


by Tammy Cannon via

adidas Originals NMD

Resn and adidas dish up an urban noir dripping with light phantisma to showcase the 2017 NMD_ products and looks.
by via Awwwards - Sites of the day