archives

Principles or patterns in FP

I've been reading articles on this site on and off for some time now. I thought I might ask my question here (I wasn’t sure where else to post, please bear with me). I have been programming in Java for a long time and recently I started programming in Erlang. I started developing a product (service) in Erlang.

When I write code in Java, I try to adhere to principles of OOP to a great extent and I find that they actually help me. Like for instance, the Open/Closed principle makes code maintenance easy (among other things). With Open/Closed principle I write classes in such a way that there won't be a need to edit them after a few months. I think we can agree that it is risky editing a file when you re-visit after a few months. So for example, if my customer wants to calculate area of a new Shape, say, a Triangle, I can easily create a new type without modifying the existing code:

abstract class Shape {
    public abstract float area();
}

class Circle extends Shape {
    private float radius;
    public Circle(float r) {
        this.radius = r;
    }
    public float area() {
        return 3.14 * radius * radius;
    }
}

class Square extends Shape {
    // override area 
}

The same in Erlang would be like so, say in shapes.erl:

-module(shapes).
-export([area/1]).
area({circle, Radius}) -> 3.14 * Radius * Radius;
area({square, Side}) -> Side * Side.

To calculate the area of a Triangle, I would have to edit shapes.erl - which is risky because there is a chance I might mess up other functions. To be sure everything runs as expected, I'd have to execute unit tests on *all* functions in this module, increasing my testing time.

Like I mentioned, I am creating a product which will be used as a service. Anything that will help me roll out features or fixes sooner will greatly improve my productivity and keep in mind the costs too. My question - are there any such principles or patterns in FP that I can follow? I searched online but I wasn't satisfied with the results. With time, I will eventually learn some principles but I wanted to get some tips from the experts here. I apologize if this was discussed before, I will be happy with a link to a good article.

Thank you.