File length in Elm

Curious question for people: do you find your file lengths in Elm to be significantly different than in other languages (that is, more lines of code or fewer lines code)? I’ve seen a few talks where there is talk about how it’s easier to deal with much larger files in Elm, but I find for myself that I still prefer keeping files pretty small. Curious what others’ experiences have been. Have you found that you tend to write longer files in Elm? About the same? Smaller?

For me it’s about the same in that I don’t worry about file size until the file reaches something like 2 kLOC and code seldom reaches that point. Most files gravitate somewhere around 500 LOC with a few outliers that are around 1.5 kLOC and few others at the other end of the spectrum around 50 LOC.

If you have a custom type, operations on that custom type and it is easier to think about that custom type in isolation, then by all means, extract it into its own module. Do this when you have identified such an opportunity. If you artificially try to split the code into modules you risk creating problems for yourself.

3 Likes

I would suggest that you look at the problem this way: Is this module containing its responsability?

Lines of code in a file don’t mean a thing, I have couple of files that are 1k+ but also I have files where I just defined a type and some operations on it, mainly toString, encoder and decoder.

Usually the question is more am I importing kitchen sink to make this work in this module when it may just fit better somewhere else. Cultural question if you ask me. :slight_smile:

1 Like

Use code folding or an outline in your editor.

Initially I found that with long files I would get lost fairly quickly as the file got larger, and I tended to end up with a bunch of small files. I found that a few comments really helped in seeing the different sections in the file, and as a result I ended up consolidating everything back into one file for now. It’s about 1kLOC right now.

1 Like

IntelliJ’s Structure View (thanks to @klazuka) is really nice for long files. I always have it open to the right of the editor. It gives you a clickable list of all top-level functions and types.

image

2 Likes

Elm files and function can be longer than in other languages, but some tips help me to find my way.

  • I always use the same structure when relevant (by example model / msg / update / view)
  • I use alphabetical order for case branches, record properties, etc. Not only it makes them easier to find, but it reduces the number of conflict when merging the code.

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