Live data from Hacker News

Good code is like a love letter to the next developer who will maintain it

addyosmani.com

131–140 of 274 posts

Re: Good code is like a love letter to the next developer who will maintain it

#131
post #70

Earlier quoted context omitted.

> there is no such thing as good code definitely not true. this is good code: https://github.com/torvalds/linux/blob/master/fs/file.c this is good code: https://github.com/golang/go/blob/master/src/strconv/atoc.go its formatted, commented, direct. the fact that you struggle with writing time tested code does not mean that everyone does.

By linking to those examples you confirmed the point you were trying to refute. From my personal perspective this is mediocre code at best. It’s written in an unsafe language. Littered with macros, which in C are not hygienic and are land mines in waiting. Constants defined with lowercase and not actually marked as const. Double underscores everywhere, which are the bad alternative to namespaces seen in weak language…

While I don't think this code is the best, these complaints are like disliking Shakespeare because his writing is archaic. Yeah, no shit.

Good writing is relative to the norms and expectations of the audience, which are other kernel developers in this case.

    It’s written in an unsafe language.
So is every other mainstream kernel.

    Double underscores everywhere, which are the bad alternative to namespaces seen in weak languages like C.
It's the designated way to avoid symbol conflicts for system software. Cultural norms.

    Constants defined with lowercase and not actually marked as const.
There aren't any constants in fs/file.c though? Do you mean the sysctls? Those are modifiable at runtime.

    WTF does “BITBIT_NR(nr)” do!?
This is genuinely obscure without background knowledge. fs/file.c maintains a bitmap of bitmaps for performance optimization reasons, hence "bitbit". "nr" has been a standard abbreviation in this part of the kernel for decades.

    Abbreviations everywhere: ofdr, nfdt, fds, fs, etc…
Terse identifiers are just a cultural norm in kernel code. old file descriptor table, new file descriptor table, file descriptors, filesystem, etc...

   Then there is a long-winded explanation of how they pack bits into an array of longs. Okay, why not make this a reusable module of code? Because C is a weak language, or because the Linux kernel is spaghetti with a dozen implementations of bit maps?
Because this code is actually very tricky, performance sensitive, and basically unique in the kernel. The kernel has many things that could be de-duplicated, but I'm confident someone's tried refactoring this and failed for some reason or another, probably performance.

    Sorry… did I just see a data race just casually commented as “okay because it is faster if it’s horrifically unsafe?
You're seeing one of the many design tradeoffs that are made to get good performance in the real world. The VFS code this file is part of is one of the most performance-critical components in the kernel and gets involved with all the other filesystem operations that happen, which on a unix system is essentially everything. The code (and cache footprint) are smaller if the safety burden is pushed off to other people here, which is more important than maintaining an ideal interface.

This is some of the most battle-tested code in the world. It's fine if you don't want to modify it, but it's solid code that people have literally bet their lives on given the mildly terrifying use of Linux in safety-critical systems.

Re: Good code is like a love letter to the next developer who will maintain it

#132

After almost 20 years of writing the best code I can muster as much as I can, I’ve come to understand that most people won’t ever really appreciate it for the effort you put in. Many won’t even notice; they’re too preoccupied with their own lives to open their eyes to what is there. Nor do they give a damn about mastery. But, that doesn’t mean it’s a waste. I don’t think I’d do much differently in retrospect, except…

Usually the situation is someone has meticulously crafted some perfect system which can be extended and reused in many coherent ways. And then that person leaves the company and no one else truly understands how it works or was meant to be used so it gets a series of hacks and patches applied to it which violate the original design which is known by no one at the company. For most projects this is not that big of a d…

For anyone interested in a discussion about the concept of systems not being understood by newcomers, I can recommend Peter Naur's Programming as Theory Building: https://pages.cs.wisc.edu/~remzi/Naur.pdf

Re: Good code is like a love letter to the next developer who will maintain it

#133
post #102

Earlier quoted context omitted.

By linking to those examples you confirmed the point you were trying to refute. From my personal perspective this is mediocre code at best. It’s written in an unsafe language. Littered with macros, which in C are not hygienic and are land mines in waiting. Constants defined with lowercase and not actually marked as const. Double underscores everywhere, which are the bad alternative to namespaces seen in weak language…

The issue so very many people fall into is assuming some foreign-looking codebase is awful. What people typically mean to say is something along the lines of "I haven't a clue what this does, haven't enough experience with the language to understand it, or how people use it". Go look at code written in "safe" languages like Rust and tell me you understand what it's doing any better than well written C code. You have…

I wish people see this more. It's so frustrating to see people pass judgement without even understanding the whole context. Maybe it's due to the lack of documentation sometimes. But many times it's just people not reading them.

Re: Good code is like a love letter to the next developer who will maintain it

#134
Write good code. Professional code. Simpler and easier to understand code. That’s it…

Some Leetcode jock will come in one day and rewrite it anyway, or a new CTO will crash the joint holding a bathroom sink and make your code an orphan. Or the company will just disappear.

The code that i write like my own is, well, my own. Because that’s the code I come back to years later and I maintain, and always will. Everything else is ephemeral.

Re: Good code is like a love letter to the next developer who will maintain it

#136

Earlier quoted context omitted.

My personal experience is that the majority of developers I ever encounter at work (as opposed to within my social network) are usually slow because they're bad engineers who don't understand problems quickly, don't have the knowledge or experience to see solutions quickly, and in general don't think deeply quickly. It's not hard to 3x performance without sacrifices when the baseline is mediocre at best. "Fast must b…

While this is true, if a job ad is telling me that that's _their_ experience of their average hire, it's probably not somewhere I want to work!

I agree (unless they also pay you three times as much, in which case fair enough).

Re: Good code is like a love letter to the next developer who will maintain it

#137

As someone who has gone back and read my old code as well as a lot of others old code, there is no such thing as good code. IMHO, the problem is one of cultural context which is often not shared between generations of coders. Languages and best practices can change so violently that best-practices one decade are often anti-patterns in the next. As a codebase outlives its best-practices, do you stick with them and ext…

Interestingly, this is exactly what my upcoming talk at DConf is about:

https://dconf.org/2023/index.html#walterb

I've got the talk about half-written. I'm concentrating on things that are timeless about writing understandable code.

Although the examples will be in D, and will use D features, the concepts are transferable to many languages.

It will be livestreamed for those unable to attend.

Re: Good code is like a love letter to the next developer who will maintain it

#138
post #50

Meh. I've seen teams and projects bogged down by "good clean code" rules and nit picking code reviewers. These folks, typically "staff" engineers, over-police the repos and care more about clean code than delivery and execution. I'm waiting for the day where AI/co-pilots can enforce team and industry best-practices, style, maintainability, testability, etc before the code is even committed. Call it "uber-linting" and…

Can't you already do that with pre-commit hooks? We use those at work to check a lot of stuff like that before a commit ever goes through.

Re: Good code is like a love letter to the next developer who will maintain it

#139
post #109

Earlier quoted context omitted.

Short names actually help with readability when they are commonly used. It's a lot quicker to go over the code when you don't need to read as much, similar to how one would write something like x+y=z in math instead of putting descriptive name for each variable in each step or using things "1€/kg" instead of "one euro per kilogram" (or let alone including kilogram's definition from SI standard).

Despite this, mathematical proofs still use a bunch of plain words to define things. In a programming context, if one has an “user” from the database, “user” is usually a better name than “u”.

It really is a context and frequency thing. For example:

    users.map(u => u.lastName)
Nobody is going to have any questions about what 'u' is here. Do that in a codebase that has a 100% consistent and very frequently used User type, and it starts to feel quite reasonable to just use 'u'. It's as familiar as Apple π.

Re: Good code is like a love letter to the next developer who will maintain it

#140

Earlier quoted context omitted.

Usually the situation is someone has meticulously crafted some perfect system which can be extended and reused in many coherent ways. And then that person leaves the company and no one else truly understands how it works or was meant to be used so it gets a series of hacks and patches applied to it which violate the original design which is known by no one at the company. For most projects this is not that big of a d…

And this is why Go lang exists... The trade off is screw brilliant engineering just make it simple so other people can build on it easily. Not saying it's my favorite, but it has it's merits long term.

A semantically weak language does not reduce the inherent complexity of a system, as problem complexity is constant.

What Go does is encode solution complexity implicitly throughout a code base. This is the same effect JavaScript often has.

Post reply on HN