Code Age vs. Time to Recall
notoriousbfg.com
notoriousbfg.com
This is a big red flag. As everyone has been saying since decades, comments are for the why, not the what.
Also, recall should be upper bound to the time it takes to find the relevant commit message. Having to actually recall from my brain why something was done is a terrible way to work: presumably at most two people in the original group know why, the people may not even be there anymore, and recall is far from perfect.
// Step 1: Prepare the widget
... 10 lines of code ...
// Step 2: Fnagle the widget
... 10 lines of code ...
Some (Martin Fowler in Refactoring) would respond to this by putting each of those 10-line code blocks into their own functions call step_1_prepare_widget() etc., but many times (not always) this ends up making the code harder to follow.On the flip side, smaller methods do not encourage code duplication and help set up unit testing for those who desire it. There are also arguments to be made depending on language and coding style (e.g. encouraging guarding, early returns and avoiding bracket use and indentation in old imperative-style Java).
If the refactored mini-function captures something very discrete, like popping an item off of a queue, then of course it's a big win. You'll often ask yourself what the hell the original developer was thinking spilling all these details into an unrelated context.
But if that chunk of code only really makes sense in the context of that function, then moving it into a separate function might not do anything except force future readers to flick back and forth between between the various helper functions and the top-level coordinating function. Have you honestly never had that experience? If so, maybe you're lucky enough to have never worked on a code base containing that mistake.
The only true rule is never to blindly follow rules! Feel free to bear good practices in mind, but always make a new, independent assessment for each situation where you might apply it. For the rule about refactoring into smaller functions, think about whether future readers are indeed going to be able to (confidently) avoid looking at the implementation of those smaller functions, because their meaning is so clear from their signature, or if you're just adding extra navigation and mental work. Sometimes it will go one way, maybe even mostly it will go that way, but sometimes not.
I've been surprised quite a few times in the past year that what I thought was "relatively new" code actually ended up being 5+ years old when I went back and looked at the commit history.
Just me getting old I guess.
It's stable? It's fast? It works? No bugs have been reported on it for years?
Regardless of how I 'feel' about that piece of code, I objectively should _not_ touch it.
I have not seen this use of the word "default" before. Does it mean "not touch"?
i've noticed that even after 3 weeks, code that i wrote deep in the flow zone will feel completely foreign, so it's more like a linear curve up and then it plateaus. however, i feel like at my current trend, by the time i'm 50 code i write the day before will feel completely foreign.
i used to watch this trend when i worked with programmers older than me, but i never realized just how badly it would hit me.
GOTO 2019 • Prioritizing Technical Debt as if Time and Money Matters • Adam Tornhill https://www.youtube.com/watch?v=fl4aZ2KXBsQ
For example, Go, not hard to recall, due to how rigid the language is with gofmt etc.
On the other hand, Perl, forget even trying to understand what drug you were on that day when you wrote this.
Then there is the extinct, like opening an old app made in cakePHP or backbone...
That's why I'm a proponent of ESLint, Gofmt, etc.
Pain today, pleasure later, hopefully.
>> One common developer bias is that old code is bad. You've never heard anyone say the phrase "legacy code" without contempt in their tone >>
As for the 'code age' point, I'd think it is more related to recency of the requirements, when was that problem domain last worked on and understood.
So maybe the issue points to clarity of requirements capture, and perhaps the coding/design corresponding to the requirements.
Another commenter referred to automated testing, which hits on having test cases to match the requirements. With that level of functional documentation, the code is likely to be easier to understand.
I cannot overstate how much better it was to work with.
There were internal attitudes that prevented me from pushing for tests. Oh and the fact that the company had not used them in the past. But I regret not pushing hard for testing.