Errors, Exceptions, and whatnot

Functional programming and domain modeling are some of my favorite subjects when it comes to software development. In the last few years, I’ve been combining them to design and write code that is easy to understand and one of the aspects that can be tricky is how to communicate errors and failures.

The way I like to think about error handling is based on Type Driven Design although without most of its power and formalism when applied to common OO languages.

The thoughtful use of types can make a design more transparent and improve correctness at the same time. - Scott Wlaschin

As an example, I want to define one function that saves one payment identifier object.

Naive(typeless) contract

void Save(PaymentIdentifier value);

Analyzing the type signature the only thing I can tell is that I must give an instance of PaymentIdentifier.

Cristal clear right? well, not really.

The first thing I would question before calling this function is if this function call fails and which errors must be caught. This information could be somewhere else in comments, tests or more usually I would need to dive into the function and its dependencies to be sure that my invocation is safe.

Result type

Result Save(PaymentIdentifier value);

Now we have one return for this function invocation, let’s check a dummy implementation for this result type.

    public class Result<T> where T : class
    {
        public Error Error { get; }
        public bool Successful { get; }
        public T Value { get; }

        private Result(T value, bool successful = true)
        {
            Value = value;
            Successful = successful;
        }
        private Result(Error errorCode)
        {
            Error = errorCode;
            Successful = false;
        }
        public static Result<T> Success(T value) => new Result<T>(value);

        public static Result<T> Failure(Error error) => new Result<T>(error);
        public static Result<T> Failure(ErrorCode errorCode, [CallerMemberName] string callerName = "", [CallerFilePath] string filePath = null,
            [CallerLineNumber] int lineNumber = 0) => new Result<T>(new Error(errorCode, callerName: callerName, filePath: filePath, lineNumber: lineNumber));

    }

The important bits of the previous code is the representation of error and success. **the type system is informing result checking is required.

Advantages

  • Clear identification of functions that can fail
  • Control over failures that can be expected(IO, side effects)
  • The Error can carry more information, therefore, improve logging and troubleshooting

What about the exceptions, they are designed for it, right?

Yes, but they are supposed to happen sometimes and not be part of my design. When exceptions are the selected mechanism for error one needs to know every invoked function in the stack in order to catch it properly. What it smells like?

By keeping data private and providing public well-defined service methods the role of the object becomes clear to other objects. This increases usability. Other objects are aware of the format to send messages into the object providing the public service. This is essentially a contract between the two objects. The invoker is agreeing to send the message in the specific format, including passing any of the required parameter information. The invoked object is agreeing to process the message and if necessary, return a value in the pre-specified format.

Yes, lack of encapsulation! The function has a clear definition *only * the happy path.

Distributed software failures are part of the flow, they should be considered in the design

Failures will happen and it must be taken into account. The use of Result-like types helps to think about possible/common failures and also provides a typed way to take decisions when they happen.

When writing this post I discovered that Rust has a native support for Result type and encourage its use. F# always had Option types but now has the Result type for better semantics.

How to give it a try?

If your language doesn’t support option type you can implement a simple result type or look for packages. In C#, for example, there is the https://github.com/louthy/language-ext.

Brazillian software engineers and mutt complex

Introduction

I’ve been working in the Netherlands for six months now. It is my first experience working in Europe and although I’ve set minimal expectations mainly due to the fact that I was working for a great company, considering that I was not leaving for any of today’s dream companies I just expected a similar experience. In this post and want to share my experience and point some misbeliefs.

The mutt complex

By “mutt complex” I understand the inferiority in which the Brazilian put yourself voluntarily in the face of the world. This in all sectors and especially in football. — Nelson Rodrigues

My experience here is too limited yet, I just know some companies and most of them in the Netherlands even so, I want to say that about software development and in this definition I include methods, process, languages and skilled people Brazil is surely not behind some developed countries.

This this comparison is based on medium-sized companies in both countries and most of them based on big cities like Rio de Janeiro and Amsterdam.

Question and my answers

I will try to give my answers to common questions that I raised before moving and that I see as common questions for more people.

So you mean that in Europe with their better education system they also struggle to find good developers?

Short answer: Yes.
Long answer: I’m not comparing the number of colleges, quality of them or how hard can be in Brazil to have a proper education, but if you look directly to open vacancies and hiring process it is the same as in Brasil, what I mean? Usually, if you want to hire good software engineers you need to convince them to leave their current companies and offer better conditions.

Ok, but once you moved to a European company you are rich and you are waiting for your Tesla.

Short answer: Definitely no.
Long answer: I still don’t know any normal software engineer here that became rich coding. The salary is pretty good, but at least compared with cities like Rio de Janeiro e São Paulo it is not that different. It is not 2x more money here than in Brasil.

Ok, but at least they are far in front of Brazil when the subject is technology, development process and quality

Short answer: No.
Long answer: They are learning the same trending programming languages, following the same process and doing the same mistakes and etc. For example what I do here is basically design, build and maintain APIs deployed to the cloud(GCloud) using containers, rest and so on.

Ahh, but in Europe, companies give you the best hardware and everything you need. Money is not a problem

Short answer: again, no.
Long answer: Similarly to Brazil, there are companies which give you the dream setup and there are companies offering standard environments.

So, you mean working in Brazil IT companies is almost the same as in European companies?

Short answer: Yes
Long answer: Technically it is, there is the huge difference of culture and behaviors that for sure influence and modify the process but I can say the struggles and challenges are pretty much the same.

Ok, so if it is similar why you decided to leave your country?

Short answer: It is about personal experience and soft skills.
Long answer: I moved to live the experience and take conclusions with my own experience. My reasons are much more related to personal development and soft skills than to technical ones. Today I work in a real international environment and I learn every day is to work with different ways of thinking and learning different approaches.

Hum.. I’m not sure yet. can you give examples of problems?

The list could be really huge but some simple examples are:

  1. We are down, there is something wrong in production.
  2. Our rollback takes too long
  3. We are not testing the most important scenarios
Conclusion

I definitely believe the experience to live and work abroad is incredible, I do recommend it to my friends but on the other hand, we must stop this mutt complex. There are great engineers, companies, and projects in Brazil and they are not in any way worse than here.

Ok, got it. Now, could you share some of these cool companies that you believe exist in Brazil?

In the last years I worked for VTEX but I heard good stories from a lot of others, you can send me PM to ask about them.

Soft skills engineering

Introduction

I’ve had the opportunity to work in good companies and with skilled people but in spite of that, it is still normal to meet great technically developed professionals who lack soft skills.

I consider it plausible when I see interns/junior developers who still need to developed skills like empathy, attitude or leadership. But, on the order hand, I’m terrified when I see Seniors, principals, architects or any other related job title that clearly have not developed these abilities(yet).

In our industry engineers can take strategic positions where communication, planning, and big-picture thinking were learned being a good tech and rarely a person who inspires the best in others.

Considering that one senior software engineer can earn this title with only 5 years of experience it is easy to see that it is not enough time to develop tech and soft skills not only about the time required to learn it but in fact to the time experiencing situations that are also part of the learning process.

Problem

When people who are in a strategic position don’t have well developed soft abilities it is really hard to identify, build or develop the company culture for a lot of reasons that you already know(or can easily find about). I consider some skills like empathy, listening, and adaptability fundamental to achieve one cental agile manifesto item

Individuals and interactions over processes and tools.

To adopt agile or to make any other methodology successful, communication must be a first-class concern and be part of company culture.

The definition of success in the previous line is not only about being agile but in fact, it can be related to any definition the company may have for example:

  • Create amazing libraries
  • Increase the revenue in xxx %
  • Help people with their struggles

To achieve any of these statements soft skills cannot be neglected.

What you can do

If you experience one situation that could be avoided by soft skill then you need to express your concern, first trying to softly remember company culture statements or promote conversations about it. If you plan to be a better developer(human being) you definitely need to concentrate some time to some of the abilities that can make you achieve great results while working within a team.

I’ve read a few books about the subject and some of my favorites are:

With Love.

How did you start using F#?

This post is my answer to this question that was raised in the F# repository. The idea is basically to share how I started my first F# project and my strategies to convince people to give it a try.

Given

I started reading about functional programming and F# some years before I actually used it professionally. I can’t remember exactly which was my first contact but one that I remember was the book 7 languages in 7 weeks, after some tests and exercises as was able to write some functional code, although this code was actually more imperative than functional it still counts in the beginning. Three years ago, my friend and colleague(by that time) that is a F# expert, proposed our team to give it a try and read about F# and apply it in some small tasks like build scripts and very simple/small projects. After a few weeks and some small projects, we were able to give the first step towards the FP in real code.

When

Once the team was convinced that F# could be a great tool we started converting tests from our production(wannabe) code to F# and soon we realized that code in F# or C# would take around the same amount of time, so it was time to convince Managers about our decision.

It can be far more complex to have an OK from the company that in fact convince your colleagues about FP.

In our case we were lucky, there were around five languages been used by that time and the company culture of small team being responsible for all the stages of their products definitely allowed me to have fun building production F# code

Then

F# became a first-class citizen programming language in our team and it opened new possibilities to other people to learn and apply it. Months later I moved to another team, so I took the same strategy and started spreading F# through test projects until the moment that my colleagues were eager to give it a try, so did we.

I left this company around 8 months ago but as far as know developers who were part of this team are still building services with F# and evolving their understanding of some of the many FP techniques that have been added to most of the popular languages.

Today

I am currently a software engineer at Travix international, There are some other Devs here who also like FP and so we are applying it in small projects and leading initiatives like talks, coding dojo, and discussion.

It is still a really small initiative but It how things started, at least when it concerns to spread a whole new development style.

Hints to help you convince your colleagues

  • Some of the most popular languages are copying/implementing FP features.
  • Most of time that developers spend to put software in production is not coding and I guess that every professional who put software in production in the last few years have this feeling
  • Share your FP knowledge in sessions and discussions.
  • Start with a small project and give it some time patiently to learn core concepts, even if a few tries your team decide that FP is not worth it is easy to rewrite this code.

This post shows my personal experience and how I manage to learn, experiment and spread FP love, all time invested learning its concepts are really helpful even when I’m coding in C# and JavaScript.

There and Back Again

It is time to blog again! After a short pause of 8 years, I want to share some experience, ideas and have discussion mainly about software developments topics.