Written on May 28, 2016
I'm writing a book about building and deploying web applications with Haskell and Yesod. Want to know when it's released? Click here.
Rant Mode: Engaged.
The word “expressive” is often bandied about when describing the features of a language. In the same way that every new CSS framework that hits the front page of Hacker News is “light-weight”, “modular”, and “modern”, everyone’s favourite language will be at some point described by them as expressive.
Whether or not a language is expressive is a relative measure. To be expressive is to effectively convey thought or feeling. There are some languages that are so expressive that you immediately understand the author’s intent without needing to know the syntax. Take the following lines of Ruby, for example:
# "How long ago?" 10.years.ago # "I expect my list of car brands to include the best one" expect(car_brands).to include "Porsche"
I feel confident offering up those lines of Ruby for debate because they are indeed idiomatic. I want to also argue that Haskell is an expressive language (and it most certainly is), but I concede I would be cherry-picking from the nice parts and there is indeed some syntax to learn first.
export const ADD_TODO = 'ADD_TODO' export const DELETE_TODO = 'DELETE_TODO' export const EDIT_TODO = 'EDIT_TODO' export const COMPLETE_TODO = 'COMPLETE_TODO' export const COMPLETE_ALL = 'COMPLETE_ALL' export const CLEAR_COMPLETED = 'CLEAR_COMPLETED'
What thought, feeling, or idea is the preceding snippet of code trying to convey? We can clearly see that we have six different things here, that in a better language might be expressed as a list of six things.
All of the noise is a result of having to work around failings of the language. We’re telling the language how to do its job, whereas we should only be telling the language what we want. How might that work in a more expressive language? Well, take Haskell, for example:
data Action = AddTodo | DeleteTodo | EditTodo | CompleteTodo | CompleteAll | ClearCompleted
Here we can see that the language allows us to boil down our code to just the ideas we’re trying to convey. It also doesn’t hurt that GHC will check that all our action names match up, but that’s a different topic.
Why does Redux have the crazy
export const… convention anyway? Am I the only one who is uncomfortable with this? Turns out, I am not. The justifications offered up essentially come down to “all action names in one place”, and “please protect me from typos”. If the problem really is one of organisation and you want all your action names in one place, then why not an array of strings?
If the use of
const because although the intent its supposed to convey is “this value shall not change”, it actually has nothing to do with immutability. Although
const will prevent a developer from assigning some other value, the original value can still be mutated.