Did another pass on the CreateMotif
function:
I’m satisfied it’s ready for testing, which will reveal all sorts of nonsense, but this function should be 80% there.
It is a very loooong function though. Long functions are a code smell.
A code smell isn’t necessarily a bug, but it is an area of code that might contain- or lead to- bugs. They are patterns that make your programmer spidey senses tingle, and you should always trust your spidey senses.
Usually the problem with long functions is that it indicates you are overloading the responsibility of the function- violating the precept that functions should be responsible for only one thing.
So it is worth looking at why this function is so long, and determine if it is overloaded, and can be shortened.
The first thing this function is doing it defining a bunch of data it will need later on.
- A Map of musical notes and their “tension”- desire to resolve back to the key of the scale.
- Data related to the rhythm of the music.
Right off the bat, lines 33-97 can probably be extracted into some kind of stateful object (Just so happens, this function consumes a ComposerState
object.
The same with 107-110 - These are “magic numbers” that affect the output of the function. These should also be extracted.
The rest of the function I’m fine with, as its doing the actual work that the function needs to do.
So am I going to do any of this work?
Hell no!
At least, not yet.
I want to test and get this in a working state, commit the whole thing to main, then do a refactor pass.
Working code > clean code.