Functional Programming: a Brief, Pragmatic Overview

Functional Programming: a Brief, Pragmatic Overview

Functional Programming (FP) is a programming paradigm, like imperative (procedural) programming or object-oriented programming (OOP).
Some people oppose these paradigms, but actually they are not exclusive of each other: we do imperative programming inside OOP’s methods, and we can mix OOP and FP: that’s even a trend in modern languages (Scala, Ceylon, …) and in older ones (JavaScript, Java 8…).

Why functional programming?

FP exists for years, but it was rather confined to niche languages. It started to infuse in the mainstream coding practices via the above mentioned languages or libraries like Guava (Java) or Underscore / Lodash (JavaScript).
By promoting stateless functions and immutable data, it proved to be very efficient and easy to control in concurrent programming, leading to efficient processing of big data.
It is also easy to test. Its rather declarative style is also easy to read.
And reactive functional programming (FRP) is a powerful paradigm to process asynchronously big streams of data as well as user input.

Given all these qualities, why isn’t more popular?

Very popular languages like C++ or Java are based on OOP, which is easy to grasp: we can relate objects to the real world. So there were a generalization of OOP thinking.
On the other hand, FP comes from lambda calculus, a “formal system in mathematical logic”. Ie. it has strong mathematical roots, thus some of its concepts can be quite abstract, and it shows when most people explain its concepts. Combined with strong typing, based on type theory, like in Haskell, we quickly end with abstract concepts with specialized jargon. Plus Haskell, one of the most popular FP languages, has a syntax not intuitive for the uninitiated.

Fortunately, as said, some people extracted some core ideas from FP, and exposed them in easier to understand languages and libraries (see above). This helped in spreading these practices in mainstream programming.

Read more

Microsoft Visual Studio Code review

Microsoft Visual Studio Code review

Integrated Development Environment oriented toward the Web technologies

https://code.visualstudio.com/

Visual Studio Code

Quick review

I installed Visual Studio Code (v. 0.10.3 on Windows 7 with 8 GB of memory).
I opened it, it starts reasonably fast.
It has a dark UI, which I don’t like, but I quickly found where to change this to the default light theme. Good point than several themes are bundled by default: no need to hunt for them.

Surprisingly, drag’n’drop of code isn’t enabled.
Apparently, it doesn’t even exist!
http://visualstudio.uservoice.com/forums/293070-visual-studio-code/suggestions/7763958-drag-n-drop-selections

Same for column (rectangular) selections!
https://visualstudio.uservoice.com/forums/293070-visual-studio-code/suggestions/7761618-implement-column-mode-selection-editing

These are features I use extensively in all editors / IDEs I use, I feel cripled if I don’t have them…

End of test… See you later.

How to test if a variable or property is defined in JavaScript

How to test if a variable or property is defined in JavaScript

The problem

There are several use cases for testing if a variable or property is defined. We will examine them before examining the ways to check the state.

  • Check if a global variable exists.
    Of course, in our code, we carefully avoid to create global variables… We use var each time we declare a variable, and 'use strict'; guards against forgetting it. Of course, we have 'use strict'; everywhere, and an ESLint, JSHint or similar tool configuration to enforce it.
    But still, we can have global variables, at least when we load ‘old-school’ (not AMD / CommonJS enabled) libraries: jQuery’s $ and Underscore / Lodash’s _ are famous examples.
    In generic code, we might want to check if these variables are defined, and if not, to provide an alternative implementation.
    Reminder: in a browser, these global variables are attached to the window object.
    In the general case, we have two scenarii: the variable is not declared at all, or it is declared but no value has been assigned to it yet.
    In the first case, if we try to use it, we will have an error (ReferenceError) complaining about a non-existing variable.
    In the second case, the variable has the value undefined.

  • Check if a local variable exists.
    Actually, I will ignore this one: it is the same case than the first one, and a simple look at the code around is better than coding a check! It is here only for exhausting the cases…

  • Check if a property is defined on an object.
    That’s the most common case. It can be used against global objects (eg. to see if a browser object has one of the latest features), against library objects (to handle old versions or optional features), against function parameters (optional properties on a parameter), etc.

  • Check if a function parameter is defined.
    A function expecting a number of parameters can be called with only part of them, or even none.
    In this case, the missing parameters (at the end of the list) are declared but have the value undefined.

Read more

JetBrains WebStorm review

JetBrains WebStorm review

https://www.jetbrains.com/webstorm/

WebStorm

For Java coding, I elected Eclipse as my main IDE. I tried NetBeans, and appreciated it, but at the time, it was Java-centric so I went back to Eclipse instead, more polyglot. Now, NetBeans is more versatile, but now I know Eclipse well, with its quirks, warts and all, so I remain there.
When I had to code JavaScript (with AngularJS, how original!) at work, for a new project, I naturally tried to use Eclipse, with an Angular plugin. It wasn’t so bad, but still a bit frustrating, with no renaming of local variables, loosing the capability to drag’n’drop code in CSS or HTML editors (a long-standing bug…), and some other frustrations.
A colleague advised to try WebStorm.
At first, I was a bit reluctant. First, it is a payware, something I usually avoid. Of course, there is chance my company finally pays for the software, if it was a real productivity tool. But then, that’s something that I would use at work, and couldn’t use at home (I have zero software budget…).
Secondly, I tried IntelliJ IDEA some years ago, and wasn’t convinced. Perhaps it was less mature than now, or perhaps I didn’t try hard enough, but I went back to my Eclipse quite quickly.

Read more