4 ms·
I agree with everything he has to say but there's an important caveat missing: In his example, the code he starts with is trivial to understand. I can read thro
by abainbridge 8y ago
I agree with everything he has to say but there's an important caveat missing: In his example, the code he starts with is trivial to understand. I can read through it linearly. I can see how the variables are used to achieve the layout. The functions draw_title() and draw_big_text_button() have obvious meanings, partly because I can see x and y coords being passed in.
Comparatively, the code he ends up with is hard to understand. If I was new to the code, I'd be wondering what layout.window_title(title) does. I'd have to lookup the definitions of Panel_Layout data structure and window_title() function. Instead of reading linearly, I now have a tree traversal to do. That is harder.
However, he's not wrong. The changes he made allow more things to be added to the UI panel without the code becoming an incomprehensible mess.
Let's call the original code the linear one. I think the reason his refactoring is good is because the linear function would otherwise have passed a threshold where it was too complex to fit into the working memory of the programmer. The tree traversal exercise he is forcing the next programmer to do serves a purpose. Each function and data structure he introduced is small enough to fit in the programmer's working memory. After looking up those definitions, their (simple) behaviour should be able to be parked in the programmer's medium term memory. Once that investment is made, use of working memory is reduced, freeing up the possibility of adding more stuff to the UI panel function without it passing the threshold.
There's a trade-off. In some ways he made the code worse. But it was necessary. We should always be asking ourselves if the abstractions we are introducing are necessary. Understanding the constraints of programmer's brains is vital to being able to make that judgement.
- tjoff 8y agoMy thought exactly. At one point I even thought he might conclude with that despite a very semantically compressed solution he reverted to the original solution but with a minor twist and some lessons learned - would have been a lovely twist :) In this case it probably was very worth while since it was so commonly used. These kind of simplifications are often riddled with trade offs that you get better at identifying and making informed decisions about as you mature and gain experience. I do agree with pretty much everything written, though I do feel that it is hard to avoid writing bad code in the beginning (but we can certainly improve on it!).
- fouc 8y agoI think that functions should be small & minimal to no side effects & trustworthy. That way you can see something like "layout.window_title(title)" and not need to go read that code because it's not necessary - unless you were specifically making a change in that area. Even if it can make debugging slightly trickier with the extra tree traversals, it also makes debugging easier at the same time because the logic is more compartmentalized and you don't have to read nearly as much code to solve the issue.