Ndifreke Ekott

Thoughts, stories, ideas and programming

07 Mar 2021

The future of the web will be statically typed

Opinions are mine and as a result of random walk in the pack with a cup of coffee thirty minutes later.

When learning to program, there was a time when the debate of statically typed vs dynamically typed languages was prevalent. Usually, folks who hated Java defaulted to other more dynamically typed languages like Python, PHP, and Javascript. One of the main reasons was how quickly you could get a program written and running.

Java 101 usually started with OOP concepts. You started with a Class, encapsulation and before you know it, you are trying to comprehend inheritance. I happened to have done Java before knowing PHP. Now switching from a Java way of thinking into a PHP mindset took a while but I finally did.

When you spend a lot of time writing in dynamically typed languages, if you haven’t had a reasonable experience in statically typed languages like Java or C#, you may not miss or realize the benefit of statically typed languages. In order to ensure the correctness of a method in a PHP before modern PHP (the version 7.0+), you had to write a couple of if statements to check the data type like:

if (is_int($inputValue)) { ...}

or

if ($inputValue instanceOf SomeClass) {}

Now, these may not be issues these days with the modern version of languages like PHP and JS that allows you to type-hint parameters to functions. But there is also the aspect of declaring a variable as a type and only be able to re-assign the only value of the same type. Take PHP for example, type hinting class members have been added to version 7.4 ( I think) and this has prevented you from re-assigning values to a property that isn’t of the same type.

class Person {
   public int $age; // type hinting here is a 7.4+
 }
 
$person = new Person();

$person->age = 10;

$person->age = "Name"; // this will throw a runtime error in 7.4

Referencing the quote from the PHP documentation

Type declarations can be added to function arguments, return values, and, as of PHP 7.4.0, class properties. They ensure that the value is of the specified type at call time, otherwise a TypeError is thrown.

https://www.php.net/manual/en/language.types.declarations.php

Before 7.4, you could assign pretty much any value to any variable and force to write defensible code. In statically typed languages, you know a variable is what it is and not have to worry about its type changing to a string type by surprise.

So I am rambling and you may be asking what is he about? I start with a question - What do you do when you have a day off during a pandemic with no place to travel to? You explore a programming framework or language 🤓.

I took a peek at ReactJ JS just to see what is happening there. Before now, I also explored the Flutter framework and built a little throwaway app. Flutter 2 has recently been released and currently supports building apps for the web. We also have Typescript which is a compile to JS language for building apps for the web. As a matter of fact, you can write React in Typescript while Angular defaults to Typescript.

During my time off, I went through the React tutorial and when it came to managing states, I peeked at the options out there and found all the Redux libraries out there. This was exciting except, I didn’t like the approach of checking for message types using switch statements. Now I may be biased because I have also explored functional programming languages like - Elm, F# and Scala.

Now you got me. Elm, F# and Scala all have a compile to JS toolchain. And if you delve into Functional Programming, you will admit that some things are modelled cleanly in ways you will appreciate when compare to your imperative languages. So I have caught the functional programming bug and recently wrote a mini todo list app in F# that allowed me to generate a React app but written in F#.

The roadmaps for most of the popular imperative languages are slowly including functional programming ideas. As they drive to more static typing, eventually our believed JS will adopt static typing and at this point, maybe my calling out the fact that the future of the web is static will be realised.

In conclusion, static typing will become commonplace whether you like it or not. We will get used to it after initial resistance to it, especially as businesses depend a lot of web technologies. Also, don’t spend your day off looking at new tools and languages, it messes you up but you learn something new anyways.