Rewriting to Haskell–Linting

riccardoodone

Riccardo Odone

Posted on April 20, 2020

Rewriting to Haskell–Linting

You can keep reading here or jump to my blog to get the full experience, including the wonderful pink, blue and white palette.


This is part of a series:


I bet most of us are not surrounded by as many haskellers as we would wish. That means we need to treasure every ounce of feedback we can get. Turns out a linter is a great way of squeezing some more.

We recently added HLint to Stream. What follows are some of the changes we've made thanks to its suggestions.

Unused LANGUAGE pragma:

 {-# LANGUAGE DataKinds #-}
-{-# LANGUAGE OverloadedStrings #-}
 {-# LANGUAGE TypeOperators #-}
Enter fullscreen mode Exit fullscreen mode

Use infix:

-toChannels channel = intersect activeChannels [channel]
+toChannels channel = activeChannels `intersect` [channel]
Enter fullscreen mode Exit fullscreen mode

Redundant bracket:

-  toField (Disabled) = toField ("NULL" :: Text)
+  toField Disabled = toField ("NULL" :: Text)
Enter fullscreen mode Exit fullscreen mode

Redundant $:

-  toField NoQuery = toField $ ("%" :: Text)
+  toField NoQuery = toField ("%" :: Text)
Enter fullscreen mode Exit fullscreen mode

Redundant do:

-findPosts connection searchQuery searchQuantity searchComments searchChannels searchLastId = do
+findPosts connection searchQuery searchQuantity searchComments searchChannels searchLastId =
   query connection (sqlQuery searchLastId) (searchQuery, searchQuery, searchComments, searchChannels, searchQuantity)
Enter fullscreen mode Exit fullscreen mode

Eta reduce:

-  random g = randomR (minBound, maxBound) g
+  random = randomR (minBound, maxBound)
Enter fullscreen mode Exit fullscreen mode
-server configuration connection = getSearchResults configuration connection
+server = getSearchResults
Enter fullscreen mode Exit fullscreen mode

Use <$> (instead of >>= pure):

-  updatedAt <- randomUTCTime >>= pure . utcToLocalTime (read "UTC")
+  updatedAt <- utcToLocalTime (read "UTC") <$> randomUTCTime
Enter fullscreen mode Exit fullscreen mode
-  createdAt <- maybe randomUTCTime pure mCreatedAt >>= pure . utcToLocalTime (read "UTC")
+  createdAt <- utcToLocalTime (read "UTC") <$> maybe randomUTCTime pure mCreatedAt
Enter fullscreen mode Exit fullscreen mode

Hints provided by HLint should be taken as such. In our case, we took what felt right and left the rest out. This is why we do not run HLint in CI. However, there are many ways of integrating it to get the best out without the pain of broken builds. Be sure to take a look at the readme cause there's a lot of additional goodness (e.g. automatically applying hints).


Get the latest content via email from me personally. Reply with your thoughts. Let's learn from each other. Subscribe to my PinkLetter!

💖 💪 🙅 🚩
riccardoodone
Riccardo Odone

Posted on April 20, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Rewriting to Haskell–Errors
functional Rewriting to Haskell–Errors

May 11, 2020

Rewriting to Haskell–Linting
functional Rewriting to Haskell–Linting

April 20, 2020

Rewriting to Haskell–Testing
functional Rewriting to Haskell–Testing

April 13, 2020