General code quality on first project

I think I’ve finally managed to start wrapping my head around Elm and I have the start of something that is not just a toy example (further than I ever got with Haskell!), so I would like to get some feedback on my general code quality to nip bad habits in the bud early on.

It is only a single file, just over 90 lines, including comments and blank lines: score.elm

I don’t want to pretend to be an Elm pro, and your code is still very simple. But I found it easy to follow.

Right now, it’s a bit over-complicated, e. g. you hard-coded only two players with no way to add more, but are still using an Array and have to deal with index out of bounds cases, even though you know they cannot occur. Or you used a record for storing a player’s money, even though that’s the only attribute and you could have just made a Player Int constructor. But I assume that you intend to add those more players and more fields later on, in which case everything is fine, because those “over-compilactions” are not too distracting anyway.

Looks ok.

I would suggest configuring your editor to run elm-format on save.

Also, Array.initialize 2 (always { money = 0 }) should be Array.repeat 2 { money = 0 }

I would have implemented updateArray like this:

updateArray : Int -> (a -> a) -> Array a -> Array a
updateArray index updateFunc array =
    case Array.get index array of
        Nothing ->
            array

        Just value ->
            Array.set index (updateFunc value) array

As a small warning, be careful that |> is tempting and, even if it is very useful, it can encourage needlessly complex code.

3 Likes

I somehow missed Array.repeat. That and using case for updateArray looks much better, thanks!

Oops, I forgot to mention that I intend to expand the number of players and the Player record.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.