https://kotlinlang.org logo
Title
c

Colton Idle

07/12/2022, 5:11 AM
This might come off as a bit crazy. But I inherited a project that has a mapper file that has like a 3,000 line when statement. There's only maybe about 300 when conditions, and each "mapper" is roughly 10 lines of code. My problem is that the IDE is suppppppper slow when this file is open. I can barely do anything. As a temporary measure I want to just split this file in half, and then half again. Which leads me to my question. Is there an easy/clever way to split a when statement across multiple files or do I have to refactor this, condition by condition. This wouldn't necessarily be a problem if the IDE could actually keep up with refactor tools. I just get the rainbow ball on macOS and have to wait like 3 minutes after every refactor. There has to be a better way. lol
😱 1
f

Francesc

07/12/2022, 5:55 AM
You can create a function for half the conditions (cut from original) and call that in an
else
in the original
when
, then repeat. Is that what you tried?
e

ephemient

07/12/2022, 6:03 AM
the second function may error due to non-exhaustive
when
, and even if it doesn't, having two places to look to see if a condition is handled seems less than ideal
I would suggest keeping the
when
in one piece, but limiting each case to do little more than call a function. then you can arrange those functions as you like
👍 3
k

Klitos Kyriacou

07/12/2022, 8:26 AM
I wonder if building and populating a MutableMap (mapping from the key to a function reference) and doing 300 `put`s would make the IDE faster. Still, it sounds bad if you have to compromise the clarity of your code in order to make the IDE faster.
m

Matteo Mirk

07/12/2022, 10:54 AM
If you get your IDE functioning again and consider refactoring that monster, I read this piece a few days ago which suits your case well: https://dzone.com/articles/refactor-switch-to-a-one-liner