i was wondering if there was a cleaner shorter way.
The use of subroutines/functions to group related commands/behavior is the cleanest way to write code. It is not necessarily the shortest, but short code tends to be obscure code, and obscure code tends to be hard to maintain. Lots of head-scratching, wondering "why did I (they) do that?".