swarm-game / swarm

Resource gathering + programming game
Other
838 stars 52 forks source link

Split large Haskell modules #707

Open xsebek opened 2 years ago

xsebek commented 2 years ago

Some of the Swarm Haskell modules have grown exceedingly large, which makes them hard to maintain.

Notably:

find src -name '*.hs' -exec wc -l {} \; | sort -h | tail -n 6
#  857 src/Swarm/Game/State.hs
#  897 src/Swarm/Language/Syntax.hs
#  942 src/Swarm/TUI/Controller.hs
#  999 src/Swarm/TUI/Model.hs
# 1130 src/Swarm/TUI/View.hs
# 2008 src/Swarm/Game/Step.hs

It also makes compilation slightly longer, since any change causes the recompilation of the entire file and its dependencies.

Proposed solution

The Step module is the largest one and the trickiest to split. But I think it could be reasonably split into three parts:

I think we could make room for the utility functions on robots and game state if we moved the records themselves into an internal module.

For example, if we moved the GameState record and its lenses:

module Swarm.Game.State.Internal (GameState, creativeMode, ...)
module Swarm.Game.State (module Swarm.Game.State.Internal, initGameState, scenarioToGameState, ...)
import Swarm.Game.State.Internal

This could of course be tweaked for documentation so that the Step module renders nicely.

byorgey commented 11 months ago

Update:

diophantus :: ~/projects/swarm » find src -name '*.hs' -exec wc -l {} \; | sort -h | tail -n 6
852 src/Swarm/Game/Step.hs
1126 src/Swarm/Language/Typecheck.hs
1208 src/Swarm/Language/Syntax.hs
1467 src/Swarm/TUI/View.hs
1503 src/Swarm/TUI/Controller.hs
1693 src/Swarm/Game/Step/Const.hs

We managed to split up Step, though Step.Const is still the longest module. Maybe the next place to focus some energy would be the TUI modules as well as syntax + typechecking.

byorgey commented 1 month ago

Update:

diophantus :: ~/projects/swarm » find src -name '*.hs' -exec wc -l {} \; | sort -h | tail -n 6
865 src/swarm-scenario/Swarm/Game/Entity.hs
873 src/swarm-engine/Swarm/Game/Step.hs
919 src/swarm-lang/Swarm/Language/Syntax/Constants.hs
1417 src/swarm-lang/Swarm/Language/Typecheck.hs
1484 src/swarm-tui/Swarm/TUI/View.hs
1779 src/swarm-engine/Swarm/Game/Step/Const.hs