News for hackers. Stuff that matters.
Re:It's probably time to stop recommending Clean Code (2020) by ericbarrett on Tuesday May 25, 2021 @10:57AM (#27277631)
Another thing Martin advocates for is not putting your name in comments, e.g. "Fixed a bug here; there could still be problematic interactions with subsystem foo -- ericb". He says, "Source control systems are very good at remembering who added what, when." (p. 68, 2009 edition)

Rubbish! Multiple times I've had to track down the original author of code that was auto-refactored, reformatted, changed locations, changed source control, etc. "git blame" and such are useless in these cases; it ends up being a game of Whodunit that involves hours of search, Slack pings, and what not. Just put your name in the comment, if it's documenting something substantial and is the result of your own research and struggle. And if you're in such a position, allow and encourage your colleagues to do this too.

Re:It's probably time to stop recommending Clean Code (2020) by jacquesm on Tuesday May 25, 2021 @11:06AM (#27277774)
Better put such a long explanation there that your name isn't needed any more. Because if it is your name that makes the difference chances are that you have left the company by the time someone comes across that comment and needs access to your brain.
Re:It's probably time to stop recommending Clean Code (2020) by ericbarrett on Tuesday May 25, 2021 @11:12AM (#27277877)
Sometimes what is interesting is that you have found that another engineer—whom you might not know in a large enough organization—has put time and thought into the code you are working on, and you can be a lot more efficient and less likely to break things if you can talk to that engineer first. It's not always the comment itself.
Re:It's probably time to stop recommending Clean Code (2020) by jacquesm on Tuesday May 25, 2021 @11:16AM (#27277927)
Sure, but in IT the rule of assumption should be that the code will outlive the coder. If being able to talk to other engineers is going to make the difference (instead of just being an optimization) then you already have problems.
Re:It's probably time to stop recommending Clean Code (2020) by ericbarrett on Tuesday May 25, 2021 @11:39AM (#27278265)
Talking to other engineers is necessary when your codebase, organization, and architecture is large enough that changes can have far-reaching effects. I would say, in fact, that it's the most important distinction between a junior and senior engineer.

Let me give you a real-world example. Facebook used to have a service called BCF‡; it was basically a "lookup" service for a given host, where you could do forward- or reverse-lookups of a hostname, or a class of hosts, and get information about their physical location, network properties, hardware configuration, and so forth.

This code was old. It had survived the transition from SVN to Git, and I'm sure it has in some form survived the transition from Git to Mercurial, though that was after my time. It had also been moved several times as no team formally owned the service. It was originally slapped together under extreme pressure by a few engineers, basically a "hackathon." Despite that, it was so useful that it had been adopted by pretty much every team that touched infrastructure, which at the time I became involved was ~300 engineers.

I was working on a service that made extensive use of the data in BCF. There was a problem with one of its Thrift RPCs which required a bugfix. This bug had plagued users of the service for several years, but because the code was so old and hoary—it didn't even have auto-generation of its Thrift bindings—nobody had bothered to fix it. Instead, every team who used this RPC had coded around it, or (worse still) skipped the binding and queried the backing database directly.

Well, I was determined to fix the bug. "git blame" showed a bot. No problem, let's go back before that commit...another bot. Before that, a human! Cool, let's reach out—no, turns out he'd done some code formatting on it. Before that—whoops, the beginning of the code history! OK, so check the old SVN repo. Five contributors over its history. I pinged each and every one who was still at the company—they hadn't written it. Finally got ahold of somebody who said, "Oh yeah, Samir‡ wrote that, ask him." I looked up and realized I could see the back of Samir's head, because he sat 10m away. It took two hours to get to that point, and 5 minutes to sort out what I needed from him without literally bringing down the site. I fixed the bug.

Every single one of those things, of course, defied the "rule of assumption." But every single one of those things was done under a specific kind of duress: keeping the company running and the features rolling. The real world is messy, and putting a little extra in your comments and leaving threads for future engineers is an enormously powerful lubricant.

‡ names changed to protect the guilty

Re:It's probably time to stop recommending Clean Code (2020) by jacquesm on Tuesday May 25, 2021 @11:50AM (#27278394)
I think you just bolstered my 'then you already have problems' argument ;)
Re:It's probably time to stop recommending Clean Code (2020) by ericbarrett on Tuesday May 25, 2021 @11:52AM (#27278429)
Every company has these problems, my friend. Like I said, good comments are lubrication against the problems that will arise in sufficiently large, churning code.
Re:It's probably time to stop recommending Clean Code (2020) by tacon on Tuesday May 25, 2021 @12:06PM (#27278586)
Yes, that is exactly the thesis of the classic paper "Programming as Theory Building"[0], that it is the theory in the human's head that we pay for and that the complete history of the code is often not enough to effectively modify a program.

[0]https://pages.cs.wisc.edu/~remzi/Naur.pdf

Re:It's probably time to stop recommending Clean Code (2020) by travisjungroth on Tuesday May 25, 2021 @10:38PM (#27285536)
Or have gotten busy since. I worked at Coinbase in 2019 and saw a comment at the top of a file saying that something should probably be changed. I git-blamed and saw it was written six years earlier by Brian Armstrong.
Re:It's probably time to stop recommending Clean Code (2020) by SassyGrapefruit on Tuesday May 25, 2021 @11:08AM (#27277809)
I think most of what martin says is rubbish, but this is not. I have never had `git blame` fail...ever. I know what user is responsible for every line of code. Doing this is contemporaneous. Its right up there with commenting out blocks of code so you don't lose them.
Re:It's probably time to stop recommending Clean Code (2020) by ericbarrett on Tuesday May 25, 2021 @11:11AM (#27277858)
I don't know what to say, this is a real problem I have encountered in actual production code multiple times. Any code that lives longer than your company's source control of choice, style of choice, or code structure of choice is vulnerable. Moreover, what's the harm? It's more information, not less.
Re:It's probably time to stop recommending Clean Code (2020) by SassyGrapefruit on Tuesday May 25, 2021 @11:24AM (#27278053)
>Moreover, what's the harm? It's more information, not less

If your code is outliving your source control system you got bigger problems than whatever is in your comments. I can confidently say that in 25 years in this industry this isn't something I've encountered. So probably sufficiently low enough probability to safely ignore.

>Moreover, what's the harm? It's more information, not less.

Too much information is every bit as bad as too little.

Re:It's probably time to stop recommending Clean Code (2020) by triceratops on Tuesday May 25, 2021 @11:26AM (#27278075)
> I can confidently say that in 25 years in this industry this isn't something I've encountered.

I've been in the industry less than half that time and it's happened to me. blame will tell you the last person/commit to touch that line of code. To find out when it was originally written, I may have to (in some cases) manually binary search the history of the file.

Re:It's probably time to stop recommending Clean Code (2020) by nemetroid on Tuesday May 25, 2021 @11:40AM (#27278276)
If you're a CLI user, you should check out tig, which is a terminal UI for git. In the blame view, you can select a line and press "," to go to the blame view for the parent of the commit blamed for that line.

This lets you quickly traverse through the history of a file. Another good tool for digging into the history is the -S flag to "git log", which finds commits that added or removed a certain string (there's -G that searches for a regex, too).

Re:It's probably time to stop recommending Clean Code (2020) by jimpudar on Tuesday May 25, 2021 @12:41PM (#27279081)
If you prefer a GUI tool, DeepGit[0] is a very nice tool that allows you to do some pretty amazing code archeology. I use this all the time for figuring out how legacy code evolved over time.

[0] https://www.syntevo.com/deepgit/

Re:It's probably time to stop recommending Clean Code (2020) by SassyGrapefruit on Tuesday May 25, 2021 @11:37AM (#27278238)
why? Just go to the commit hash in the blame and run blame again.

All in all it has a negligible impact on the readability of the code. It's mostly aesthetic for me. It's ugly and only solves far fetched problems.

Do you scratch your name and SSN into the side of your car? What if your title blows away in the wind on the same day that city hall burns down destroying all ownership records?

Re:It's probably time to stop recommending Clean Code (2020) by richardxia on Tuesday May 25, 2021 @11:49AM (#27278390)
Was going to post the exact same thing. I make use of this repeated git blame method all the time, and for everyone who is just learning this for the first time, you'll actually want to write `git blame <commit>~` to go back one commit from the commit hash in the blame, because otherwise you'll still get the same results on the line you're looking at.

Also, if you're using GitHub, their Blame view also a button that looks like a stack of rectangles between the commit information and the code. Clicking that will essentially do the same thing command-line git operation above.

Re:It's probably time to stop recommending Clean Code (2020) by erik_seaberg on Tuesday May 25, 2021 @06:53PM (#27283813)
git blame --ignore-rev helps with filtering out meaningless changes in commits.
Re:It's probably time to stop recommending Clean Code (2020) by triceratops on Tuesday May 25, 2021 @11:41AM (#27278281)
Good idea. TIL.

(I'm embarrassed I didn't think of it before)

Re:It's probably time to stop recommending Clean Code (2020) by NortySpock on Tuesday May 25, 2021 @12:30PM (#27278923)
> If your code is outliving your source control system you got bigger problems than whatever is in your comments.

You've never been at a company that finds it needs to upgrade from $old_version_control_sytem to $newer_version_control_system?

Because I've never seen that done as a big-bang rollout, it's always been "oh that code lives in the new system now, and we only kept 6 months of history"

Even just having an architect manually move folders around in the version control system has broken the history.

Re:It's probably time to stop recommending Clean Code (2020) by SassyGrapefruit on Tuesday May 25, 2021 @12:54PM (#27279252)
>You've never been at a company that finds it needs to upgrade from $old_version_control_sytem to $newer_version_control_system?

I've done this maybe a dozen times. Its always been big bang and I've never lost any history I didn't choose to lose. In fact I just did this last week for a 10 year old project that moved from SVN to GIT.

If you work somewhere where someone tells you this isn't possible consider finding a new job or alternatively become their new source control lord with your new found powers. Moving things between SCM systems is about the easiest thing you can do. Its all sequentially structured and verifiable. The tools just work everytime.

Re:It's probably time to stop recommending Clean Code (2020) by triceratops on Tuesday May 25, 2021 @11:24AM (#27278061)
Code never gets moved or refactored by someone other than the original author?
Re:It's probably time to stop recommending Clean Code (2020) by kapp_in_life on Tuesday May 25, 2021 @11:38AM (#27278246)
So then you checkout at the refactor commit and look through the blame to continue searching. If you have to repeat this more than a few times then the person has probably left the company or hasn't touched the code in years so its better to understand it yourself before modifying.
Re:It's probably time to stop recommending Clean Code (2020) by triceratops on Tuesday May 25, 2021 @11:41AM (#27278286)
I'm embarrassed I didn't think of that before.
Re:It's probably time to stop recommending Clean Code (2020) by kapp_in_life on Thursday May 27, 2021 @02:26PM (#27306363)
No worries, after typing it out I definitely feel like there should be an easier way to say "show me the stack of commits that touched this line(s) of code", and I'm sure some git wizard has a fancy one liner that could more easily do that.
Re:It's probably time to stop recommending Clean Code (2020) by SassyGrapefruit on Tuesday May 25, 2021 @11:43AM (#27278302)
If it gets moved then the blame will tell me who moved it, It will also tell me what the hash was before it was moved. That hash will have all the original information. Same for the refactor case.
Re:It's probably time to stop recommending Clean Code (2020) by gher-shyu3i on Tuesday May 25, 2021 @11:30AM (#27278136)
The parent's comment holds when reformatting, especially in languages with suspect formatting practices like golang, where visibility rules are dictated by the case of the first letter (wat?) or how it attempts to align groups of constants or struct fields depending on the length of the longest field name. Ends up in completely unnecessary changes that divert away from the main diff.
Re:It's probably time to stop recommending Clean Code (2020) by nemetroid on Tuesday May 25, 2021 @11:22AM (#27278026)
It does require some rigor in your version control practices, but I'm happily "git blame"-ing twenty year old code every now and then.
Re:It's probably time to stop recommending Clean Code (2020) by ziml77 on Tuesday May 25, 2021 @11:04AM (#27277749)
What is even the downside of adding a few extra characters to the end of a comment to show who wrote it?

And has Martin ever even worked on large, non-greenfield projects? That's the only way I could see anyone professing such idealism.

Re:It's probably time to stop recommending Clean Code (2020) by jen20 on Tuesday May 25, 2021 @11:48AM (#27278361)
> What is even the downside of adding a few extra characters to the end of a comment to show who wrote it?

You're right - in fact we should do this for every line of code, so that we know of whom to ask questions!

    func main() { // jen20
        fmt.Println("Hello World") // ziml77
    } // jen20
What's the downside of adding a few extra characters!?

Of course, this view is already available to people: `git blame` - and it's the same for comments, so there is no need.

The exception is "notes to future self" during the development of a feature (to be removed before review), in which case the most useful place for them to appear is at the _start_ of the comment with a marker:

    // TODO(jen20): also implement function X for type Y
Then they are easy to find...
Re:It's probably time to stop recommending Clean Code (2020) by ericbarrett on Tuesday May 25, 2021 @12:01PM (#27278536)
What you've shown is code clutter and reductio ad absurdum to what I wrote in the top-level comment. I am speaking of architectural comments, bug-fixes, and especially in service architecture where unusual and catastrophic interactions might happen (or have happened) with code that's not under your control.
Re:It's probably time to stop recommending Clean Code (2020) by max46 on Tuesday May 25, 2021 @12:08PM (#27278621)
>You're right - in fact we should do this for every line of code, so that we know of whom to ask questions!

if you want to live in this kind of absolutism, I would rather have the name on every line than no comments at all.

Re:It's probably time to stop recommending Clean Code (2020) by jen20 on Tuesday May 25, 2021 @01:32PM (#27279758)
Good news then: git blame already does this, and modern editors show it all the time (via copying Code Lens features from Visual Studio).

Install the appropriate extension for GitHub and you can have it there too, with no extra effort or maintenance burden of duplicative metadata.

Re:It's probably time to stop recommending Clean Code (2020) by Cthulhu_ on Tuesday May 25, 2021 @11:37AM (#27278242)
I think your comment is controversial, for a number of reasons. One, I think nobody should own code. Code should be obvious, tested, documented and reviewed (bringing the number of people involved to at least two), the story behind it should be either in the git comments or referenced to e.g. a task management system. Code ownership just creates islands.

I mean by all means assign a "domain expert" to a PART of your code, but no individual segment of code should belong to anyone.

Second: There's something to be said about avoiding churn. Everybody loves refactoring and rewriting code, present company included, but it muddles the version control waters. I've seen a few github projects where the guidelines stated not to create PRs for minor refactorings, because they create churn and version control noise.

Anyway, that's all "ideal world" thinking, I know in practice it doesn't work like that.

Re:It's probably time to stop recommending Clean Code (2020) by Chris2048 on Tuesday May 25, 2021 @12:45PM (#27279134)
Maybe not exclusive ownership, but there are always going to be those more familiar with a section of code that others.

It's not really efficient to insist everyone know the codebase equally, especially with larger codebases.

Re:It's probably time to stop recommending Clean Code (2020) by bjohnson225 on Tuesday May 25, 2021 @01:06PM (#27279405)
I never found people adding a name useful.

Either the code is recent (in which case 'git blame' works better since someone changing a few characters may or may not decide to add their name to the file) or it's old and the author has either left the company or has forgotten practically everything about the code.

Re:It's probably time to stop recommending Clean Code (2020) by watwut on Tuesday May 25, 2021 @11:24AM (#27278062)
If you have to track down the author, then it is already bad. The code should not hope that the author never finds a new job.
Re:It's probably time to stop recommending Clean Code (2020) by cloverich on Tuesday May 25, 2021 @01:42PM (#27279865)
But sometimes it is bad, and not fixable within the author's control. I occasionally leave author notes, as a shortcut. If I'm no longer here, yeah you gotta figure it all out the hard way. But if I am, I can probably save you a week, maybe a month. And obviously if its something you can succintly describe, you'd just leave a comment. This is the domain of "Based on being here a few years on a few teams, and three services between this one, a few migrations etc etc". Some business problems have a lot of baggage that aren't easily documented or described, its the hard thing about professional development especially in a changing business. There's also cases where I _didnt'_ author the code, but did purposefully not change something that looks like it should be changed. In those cases, without my name comment, git blame wouldn't point you to me. YMMV.
Re:It's probably time to stop recommending Clean Code (2020) by throw1234651234 on Tuesday May 25, 2021 @11:35AM (#27278220)
A 1000 times this. We never use git blame - who cares? The code should be self-explanatory, and if it's not, the author doesn't remember why they did it 5 years down the line either.
Re:It's probably time to stop recommending Clean Code (2020) by erik_seaberg on Tuesday May 25, 2021 @07:02PM (#27283881)
I rarely want the author name. What I want is the commit message explaining the changes, and I want to see what the code used to do.

Code without history is nearly unsupportable without reverse engineering it all.