Sunday, February 21, 2021

Knowing How Things Work

 There are some real benefits in knowing how things work.

Jumping into something totally blind sometimes is fine, but only if you are there to learn it, or to enjoy it or, or just to take in the experience. But it’s always amateur-hour, the results might work out okay, but it is more likely they will leave some room to be desired.


Having a shallow overview might be okay sometimes, however being able to get right down to the details and see what is happening underneath is far, far better.


Most things that are complex are somewhat opaque from the outside. Their internal complexity can be counter-intuitive and built up over long and fractious histories. You need to know most of the parts, but it also helps to see how it all evolved, so that the exceptions make sense and fit back into the bigger picture.


There is no such thing as too much depth. There might however be too many details for any one person to cope with, so for very large and complex things, it is often necessary to specialize. A generalist can lead a bunch of specialists, but only if they are trusted and their advice heeded.  Thus one needs to know their limits: how much they know, how much they don’t know, and all of the little things that they think they might know, but really don’t. They need to know if they need someone else who knows more.


If you know how things work, you can make changes to them that have a much higher degree of continuing to work or actually improving stuff. If you don’t know how things work, then any changes you make will effectively cause random side-effects that you constantly are reacting to. That is terribly inefficient, somewhat dangerous, and there is no way of knowing once you start this, whether you’ll ever get it finished in a manner that is acceptable. 


Having an opinion about how things ‘should’ work is not the same as knowing how they actually work. And worse, opinions based on misunderstandings are more likely to be destructive, than helpful.


If you know how things work, then you know all of the changes that have to be made to achieve a certain goal. You also know how long it will take to make those changes. If it’s long-running work and you set a plan, then that plan will work out, in so long as there have not been any unforeseeable events. If the plan fails, it fails because of the things you didn’t know. Thus being able to successfully plan is a good way to prove that you actually know how things work.


It takes a lot of time to know how things work. It takes a lot of reading, and ingesting what you have learned, and then getting out there and experiencing stuff before you can really see how all of the pieces fit together. Learning from experienced mentors is the fastest way to get gain understanding. Learning from courses or textbooks is better at providing an overall view. Somethings can be known discretely, and that knowledge is definitive. Somethings can only be known as a sort of intuitive feel for how they will react in underlying difficult circumstances. You might know all of the parts really well, but are still not be entirely sure how they will react to a multi-dimensional set of changes. Most things require some form of balance, which is why oversimplifications tend to throw things out of whack. They favor one aspect of the problem over the others. 


Full knowledge is obviously the fastest and most effective way forward, but when that spans multiple people the team dynamics became an actual part of the thing getting changed, they are no longer separatable. Partial knowledge can be okay if it is accepted and account for, but you still have to find out about the unknowns and the unknown unknowns as well. 


A professional is someone who knows how things work for a given industry and can successfully reach any ‘possible’ goals. But they also know which goals are not possible, and which ones are unlikely. They know how to avoid failure.

Sunday, February 14, 2021

Gyrations

It’s instructive to walk through code sometimes. It reveals a lot more than just scanning it at a high level.

One of the things you find is that the code might take some pretty odd paths through the logic, just to get back to some really simple data changes.


These gyrations are of course unnecessary. They usually exist because the programmer didn’t know how to go from A to B. Instead, they went skewing off to H, bounced over to M, and then came back to B.


Sometimes this is a result of a second programmer coming into the code, with the intent to ‘just fix’ the minimum necessary. 


Sometimes it is a result of the original programmer not fully understanding the work they are doing. For some coders, they have a discreet set of ‘instructions’ that produce certain types of output. They assemble their code from this set. If this set has gaps, their code also has gaps, so they fill them by jumping over to other areas that they have in their set, doing intermediately work, then eventually jumping back. That is, they know how to go from H to M, and they know how to go from A to H, and from M to B. This can be recursive and often quite intertwined, there can be some pretty crazy gyrations going on.


It’s far easier if you want to code A to B, to just code A to B, even if there aren’t any underlying libraries that help. Most of these transformations are pretty trivial, they involve basic data type changes or structural rearrangements. If it turns out that it is not possible for scheduling reasons to do the code yourself, then the best way to handle this is to craft a function to encapsulate A to B. In that function, you can throw H and M into the mix, that’s okay. Later, if another programmer sees this and knows how to be more straightforward, it’s pretty safe for them to just replace the H and M madness with better code. If it’s encapsulated, then the impact of this change is easy to figure out. 


Sometimes programmers don’t want to encapsulate A to B because they feel that it is either hard to read or slow in performance. Neither issue really applies. It’s easier to read code if the weirdness is pulled away from it. You get a bigger sense of what the logic is trying to accomplish. The performance costs of adding lots of functions have been effectively trivial for decades. There are certain types of high-performance code where it might matter, but chances are that is not the code you are writing right now, and even if it was, it’s better to also provide a slower, more stable, more debuggable version in the source as well.


The biggest problem with large systems is that any and all disorganization feeds into making it a tangled mass of spaghetti. This always starts with little stuff, and each problem itself does not seem significant, but as these problems stack on top of each other, the entire edifice grows shaky and difficult. In a sense, passing through H and M is entirely artificial. They weren’t really needed, they are wasting resources, and they are just confusing the code. The code needs to get B from A, and it needs to do that in the clearest and cleanest way possible. Even if you are rushed, encapsulating that in a little function doesn’t take more than a few minutes, and it will pay off immensely later. 


Fixing this is actually really easy. Just start going through the big functions, creating littler ones. Once you have little ones, revise them to fit into other places in the code. Sometimes you have to unwind it a little to get it cleaned up. So, if A->H->M->B is intertwined with A’->Q->X->N->C, you probably need to separate them first, before you can encapsulate them. Obviously, it was less work to get it right originally, but it’s not complicated work to sit there and unravel it either. It’s just slow and boring. You have to do it a little at a time, then check that the code still runs correctly, then continue on. It definitely is work worth doing, particularly if there are still big changes to the system that are coming. Ignoring it only makes it worse.

Thursday, February 11, 2021

Multi-tasking

When I first started coding I often struggled with remaining focused. While writing code, there is a lot of stuff going on in our heads that need to get put down into the computer. It turns out that code is best and the work is fastest if you can basically just type what you ‘see’ during prolonged stretches. Later you keep passing over it, again and again, editing it until it becomes clean and tidy.

As I grew in my abilities, I learned to focus for hours and hours at a time. In the heyday of programming, I had my own office, I could close the door and get emersed in the work. That was easily the best code I have ever written. It was very readable, the mechanics were discussed thoroughly and thought out clearly, the project had good strong conventions that we meticulously followed, and the code was consistent. Reuse was critical to the success of the project, otherwise, we would have ended up with way too much stuff. It was a very ‘technical’ development, a big distributed fault-tolerant cache that had high-performance specifications. Despite the complexity, we only ever had 1 known software bug in production. It was also my favorite job.

As Agile killed the profession, programming moved from being able being about deep focus to often just bouncing around throwing a lot of code fragments everywhere to see if we could get things working. Noisy open office spaces, lots of interruptions, stress, and drama, and maybe about 30% of the time necessary to get barely functional code.

It took some adjustments, but I gradually learned to work in a messy, loud, mutli-tasking environment. Part of the secret was learning to write a lot of stuff down. The core habit is to keep a ‘note’ of what you are going to do before you go and do it. If you are disrupted in the middle of working on something, you make a new note for the next task, jump to the new work and keep going. When you are finished, you go back over your notes and work your way back out of the pit. In rough environments, you might find yourself down 6 or 7 levels, really quickly.

Another great habit is time-boxing. That is, you try to allocate very specific times of the day to do specific work. Mornings are for big email replies, management issues, etc. After lunch, somedays are ‘coding days’, some are ‘meeting days’. Rather obviously, sticking all the meetings back to back helps keep you in ‘talking context’, rather than ‘coding mode’. It’s always been very different, at different companies, but trying to batch similar stuff together helps, thinking about it in terms of ‘focused work’ and ‘unfocused (chatty) work’ helps too.

Another thing to do is keep a separate list of boring tasks. When you are baked, work on stupid, boring, trivial stuff. When you have the energy, do the more cognitively demanding work. It does cause a bit of interference with scheduling though. When you are giving estimates, you have to give them in terms of the good days/hours. So, if most weeks consist of 2-3 days of insanity and drama, your capacity is really only 3-2 days of work, but it’s better to err on the lower side, so you are often down to 2. Thus 10 days of grueling, hard-ass coding are going to span 5 weeks unless you start clocking in overtime. If you keep clocking in overtime, eventually all of the days are ‘unfocused days’ and you’re down to 0, which gets messy with your employer (but at least the boring work is all done, isn’t it).

Sometimes I just cycle. Get four projects going at once, work on each until they hit a significant blocker, then move on to the next one. When doing this, the code itself can’t be rocket science, so it’s usually a little more brute forcey, domain-specific stuff. Then the blockers tend to be poor specifications, external decisions, ambiguities, etc. Most of them involve getting a conversation going with other people about some issue that needs to be addressed right away. So, the flow is to read back the items that need to get done (as per above), then work on them. Either close them off or jot down that you need to punt it into some discussion. When the little bits are all finished, flip over into ‘email mode’ and trigger all of the discussions. That project is now all jammed up, move on to the next project. Keep cycling between all of them, with the same flow.

Sometimes, shifting priorities will pop up that force a different order, but it’s best if you try to spend a bit of time first to close off the thing you are actively working on right now (by either finishing it or at least updating your notes). Working this way, keeps everything moving forward, and you keep a record of where you are and what didn’t get done yet. It doesn’t however produce great quality work.

My personal feeling is that spending the effort to get good or even great code is both faster and produces way better results for the users. We can rush through the work, at times they do only need crappy quality, but doing this all of the time gradually paints the entire effort into a corner. Technical debt is most often exponential, a little of it starts leaking in, and before you know it, getting anything done, at all, is a Herculean effort. Preventing that decline is equally, if not more important than getting out most features, but since that work is basically invisible to management and the users -- until it is too late -- people incorrectly ignore it. Still, the industry turned into this current direction, like a huge ship, so it’s not so easy to set it back on a good course. We have to deal with whatever madness that is paying the bills, so it is best to learn how to spew out lots of code, on many different tasks, all at once, even if you realize that people aren’t really getting what they need.