UpdateGame function for Tetris I am writing a Tetris game while learning Haskell and I'm trying to make the following code more concise and easier to read. How can I improve it?

Note that the function is not finished yet (There is no check for complete lines, for instance); I currently want to improve its readability.

updateGame :: IORef InputsData -&gt; IORef GameState -&gt; IO()
updateGame keysIORef gameIORef = do
    game &lt;- readIORef gameIORef
    keys &lt;- readIORef keysIORef
    newTime &lt;- getCurrentTime
    elapsed &lt;- getMilliseconds $ diffUTCTime newTime (time game)

    -- Updating current time
    modifyIORef gameIORef (\g -&gt; g {time = newTime})
    game &lt;- readIORef gameIORef

    -- Updating keys cooldown
    modifyIORef gameIORef (\g -&gt; g {keyCooldowns = updateCooldowns (keyCooldowns game) elapsed})
    game &lt;- readIORef gameIORef

    -- Processing side movements
    when (not $ isOnCooldown (keyCooldowns game) (SpecialKey KeyRight)) $ do
        if right keys
          then when (canTetrominoMove (currentTetromino game) (Helpers.R) (tank game)) $ do
            modifyIORef gameIORef (\g -&gt; g {currentTetromino = (currentTetromino g) {x = x (currentTetromino g) + 1}})
            modifyIORef gameIORef (\g -&gt; g {keyCooldowns = addCooldown (keyCooldowns game) (SpecialKey KeyRight) 100})
          else if left keys
            then when (canTetrominoMove (currentTetromino game) (Helpers.L) (tank game)) $ do
              modifyIORef gameIORef (\g -&gt; g {currentTetromino = (currentTetromino g) {x = x (currentTetromino g) - 1}})
              modifyIORef gameIORef (\g -&gt; g {keyCooldowns = addCooldown (keyCooldowns game) (SpecialKey KeyRight) 100})
            else return ()
    game &lt;- readIORef gameIORef

    -- Processing tetromino rotations
    when (not $ isOnCooldown (keyCooldowns game) (Char 'z')) $ do
        if xKey keys
          then when (canTetrominoRotate (currentTetromino game) (Helpers.R) (tank game)) $ do
            modifyIORef gameIORef (\g -&gt; g {currentTetromino = rotateTetromino (currentTetromino g) Helpers.R})
            modifyIORef gameIORef (\g -&gt; g {keyCooldowns = addCooldown (keyCooldowns game) (Char 'z') 200})
          else if zKey keys
            then when (canTetrominoRotate (currentTetromino game) (Helpers.L) (tank game)) $ do
              modifyIORef gameIORef (\g -&gt; g {currentTetromino = rotateTetromino (currentTetromino g) Helpers.L})
              modifyIORef gameIORef (\g -&gt; g {keyCooldowns = addCooldown (keyCooldowns game) (Char 'z') 200})
            else return ()
    game &lt;- readIORef gameIORef

    -- Updating current tetromino falling time
    modifyIORef gameIORef (\g -&gt; g {tetrominoFallingTime = (tetrominoFallingTime g) - elapsed})
    game &lt;- readIORef gameIORef

    -- Making current tetromino fall &amp; restarting current tetromino falling time
    when (tetrominoFallingTime game &lt; 0) $ do
        modifyIORef gameIORef (\g -&gt; g {tetrominoFallingTime = (tetrominoFallingTime g) + getTetrominoFallingSpeed (difficultyLevel g)})
        if canTetrominoFall (currentTetromino game) (tank game)
            then do
                modifyIORef gameIORef (\g -&gt; g {currentTetromino = (currentTetromino g) {y = y (currentTetromino g) - 1}})
            else do
                modifyIORef gameIORef (\g -&gt; g {tank = addTetrominoToTank (currentTetromino game) (tank game)})
                let (currentTetromino', tetrominoBag', rndGen') = drawTetrominoFromBag (rndGen game) (tetrominoBag game)
                modifyIORef gameIORef (\g -&gt; g {currentTetromino = currentTetromino', rndGen = rndGen', tetrominoBag = tetrominoBag'})

    addTimerCallback 1 $ updateGame keysIORef gameIORef
    postRedisplay Nothing
You should separate IO actions from pure code. In your case the principal task of updateGame is to create the new game state given input data, and for this no IO is necessary. The fact that the state is stored and modified in an IORef.

It's not just the matter of style and code separation. Calling modifyIORef multiple times in the function means that you can easily get race conditions, if there is another thread working with the game state. It would get or modify an inconsistent state.

So my suggestion would be to split it into two parts - one a pure function, and another that does all the necessary IO:

updateGame :: UTCTime -&gt; InputsData -&gt; GameState -&gt; GameState
updateGame time input game = ...


updateGameIO :: IORef InputsData -&gt; IORef GameState -&gt; IO ()
updateGameIO modifyIORef gameIORef = step
  where
    step = do
      time &lt;- getCurrentTime

      keys &lt;- readIORef keysIORef
      modifyIORef gameIORef (updateGame time keys)

      addTimerCallback 1 step
      postRedisplay Nothing


This way, you ensure that the game state is updated atomically, and keep the main function pure.



Updating a field in a complex data type is a common task, and it can be greatly simplified using the lens library. Instead of

\g -&gt; g {keyCooldowns = updateCooldowns (keyCooldowns game) elapsed}


you let lens generate so-called lenses for your data type, so for example keyCooldowns won't be of type GameState -&gt; Something, but Lens' GameState Something, and then the function that modifies the field inside GameState would be

over keyCooldowns (flip updateCooldowns elapsed)


See A Little Lens Starter Tutorial