I’m always trying to write software that is as elegant and readable as possible. This often means identifying areas of confusion in the code and improving it. This also often means picking between styles or ideas that are all equally crappy.
Today’s dilemma concerns snap lines. Snap lines are the lines that go from an element to the other elements used for snapping. If you don’t know what snapping is, it’s when you drag something in software. When it’s close to another thing, it moves on its own to line up with that other thing; it snaps into position to be perfectly aligned. My software draws a dotted line to show which elements are used for snapping horizontally and vertically.
I have a function in the mechanism Document class called “move,” which accepts a set of elements to move. The set is almost always the element selected in the mechanism. Inside the move function is a test to see if any element is close to lining up with another not-selected element. Once any snapping is finished, the move function returns zero or more lines that indicate any snap that happened. The problem is that returning an array of lines from the move function doesn’t make much sense. It’s not nonsense; it’s just not contextual because the move function moves things, and lines don’t appear related to that operation.
One solution is to name the set of lines being returned. I could do that by returning a tuple, a group of values with names for each value. But I don’t have any other value to return! A tuple cannot have a single value (which is annoying since a struct or class can). Do I create a struct with a single value so I can give a name to the set of lines? That’s not elegant. Do I return more than one value in a tuple by adding a true/false value that indicates if anything moved (even though it’s unnecessary)? That’s also not elegant. Do I create a parameter to pass in an empty array of lines and then fill it as needed? That’s not elegant, either. None of the solutions are pretty, elegant, self-explanatory, etc.
One of the things I’m enjoying about porting the Linkage program to the Mac is making the code better than in the Windows version. But it’s tough because some things I did “wrong” in the Windows Linkage code is still going to be “wrong” in the Mac version.
So, what option did I pick? I now return two values from the move function in a tuple. The first value is the line array named “snapLines”. The second value is a dummy value, a “void” that gets ignored. This is somewhat ugly, but it lets me name the snapLines array so the code that calls the move function is more self-explanatory.