by imoverclocked · 3y ago · 0 comments
As a codebase outlives its best-practices, do you stick with them and extend with those same anti-patterns or do you implement new things with a different mindset than the rest of the code?
-- If you break with tradition then you are making it harder to understand the codebase as a whole.
eg: why do we have a mix of procedural/OO/functional/... methodologies with spaghetti code at the core?
-- If you refactor everything then you are definitely breaking something.
eg: you have a 10M+ line java codebase with transformations bringing you into the 2020s from coders who originally knew C and Java 1.4.
-- If you keep with tradition then are you writing the best code possible?
eg: would you willingly use goto of some variant in new code if you jumped into a Cobol 74 codebase?
I think what the article really wants to talk about is "clear" code or "understandable" code and not "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.
...
Every programmer occasionally, when nobody’s home, turns off the lights, pours a glass of scotch, puts on some light German electronica, and opens up a file on their computer. It’s a different file for every programmer. Sometimes they wrote it, sometimes they found it and knew they had to save it. They read over the lines, and weep at their beauty, then the tears turn bitter as they remember the rest of the files and the inevitable collapse of all that is good and true in the world.
This file is Good Code. It has sensible and consistent names for functions and variables. It’s concise. It doesn’t do anything obviously stupid. It has never had to live in the wild, or answer to a sales team. It does exactly one, mundane, specific thing, and it does it well. It was written by a single person, and never touched by another. It reads like poetry written by someone over thirty.
...
[ https://www.stilldrinking.org/programming-sucks ]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 languages like C.
WTF does “BITBIT_NR(nr)” do!? Does it “bit” the “bits”? What is NR? Is it a “number”? Then why not use “n” and drop it from the macro name?
Abbreviations everywhere: ofdr, nfdt, fds, fs, 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?
\*
* Note that this can drive nr *below* what we had passed if sysctl_nr_open
* had been set lower between the check in expand_files() and here. Deal
* with that in caller, it's cheaper that way.
Huuuurk.Sorry… did I just see a data race just casually commented as “okay because it is faster if it’s horrifically unsafe?
Yes? No? Maybe?
Do you even know?
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 to know the language to understand what is sane or not, what the conventions are, what the common issues are, etc.
Same with all the Functional stuff out there. To a OO person, it looks bat poo insane. But if you learn FP, it's natural.
Some of your complaints are valid from the perspective of someone who doesn't do kernel work. For the people who do work on the kernel, the macros, abbreviations, etc are common place and well understood, and a complete non-issue.
“ the problem is one of cultural context which is often not shared between generations of coders. “
There is no such thing as absolutely good code. It’s context dependent and context tends to be ephemeral. The Linux kernel is in some ways an exception, but also appreciated by a pretty small group. The rant against the kernel code is meant to be illustrative - if you don’t know the conventions, abbreviations, if you’re not operating in the context of an early 21st century OS, where memory is handled manually, none of this looks particularly great.
Code is cultural and cultures are niche and ever changing. Particularly these days. The same technology that enabled the fast rise of, say, Ruby, or Go, or Rust makes your code and my code depreciate quite quickly.
That kernel C code still looks awful to me.
I've seen beautiful code in every language including C.
> ...did I just see a data race...
You did see a data race.
That race is pretty clearly unavoidable, as it would be triggered by a sysadmin setting the value of a particular sysctl knob "low enough" in between the check mentioned in another function in the chain of function calls that this function is part of being run and the commented code being run.
(You can tell that this function part of a chain of function calls because that comment refers to a check in a function that is not called by the function that contains the comment.)
> ...okay because it is faster if it’s horrifically unsafe...
I mean, here's the call site of the "objectionable" function and the site at which the issue gets detected: <https://github.com/torvalds/linux/blob/master/fs/file.c#L174...>. At both ends, there's a comment that says "Hey, it's faster to do this check here, rather than over there.".
It's clear that the folks who worked on this code were concerned about the performance, and had discovered that relying on callers to discover if that race had resulted in them getting a smaller allocation than they wanted was needed.
Many people will say that an induction stove is better in safety, convenience and many more. But a gas stove still has its own place.
Just because the kernel code doesn't use the latest "best practices" doesn't mean it's bad. It could be due to many reasons from performance, compatibility, etc. Software is so easy to update anyway compared to other things that I'd bet there's more "good" software than "good" anything else.
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.
But... his writing is archaic. Not just quaintly archaic, like a novel from the 1800s, but literally requiring translation archaic.
Worse still, the vast, vast majority of people "teaching" Shakespeare pronounce it wrong, which means most of the humour is lost: https://www.youtube.com/watch?v=YiblRSqhL04
The jokes don't work any more!
The rhymes sound wrong!
The whole thing is a farce. Theatre. We all pretend it is great English, whe in fact it isn't even English any more.
It's taught because our teachers were taught it. Those teachers... and on.
It's like the idiots still formatting cloud SSD virtual disks as-if they are physical RAID controllers with spinning rust.
It's like the Hungarian notation identifier style people copied from the NT kernel code, even though the NT kernel people realised it was a mistake and moved on.
The Linux kernel code would be crazy bad if it wasn't for tens of thousands of people beating on it until it has become merely mediocre.
It will never be perfect, it'll never even be "good" code. It has too much inertia, too much history for that to ever happen.
it seems you didn't bother to read the entire comment, otherwise you would've seen I linked to TWO different programming languages.
Finally a sane person <3
Honestly, I never understood why so many developers abbreviate function names or variables. Is it so much pain to type abc<tab> for auto completion?
Why not simply name fd file_descriptor?
Short names are a mental barrier and a level of abstraction that can easily be avoided.
Do you have a source on what I have managed to write or is this just guessing and calling it "fact?"
But do you think there is bad code? If so, I'll give you the official definition of good code: code that isn't bad code.
* Modern, where the language's features and idioms reflect the best way we know to write good, clean code today.
* Compatible, where code written in the language years ago continues to run the same as when first written.
* Simple, where the language has relatively few concepts that compose in clean ways and where there are few ways to accomplish the same goal.
But you only get to pick two.
If it's the latter, I need more info please!
good code solves the problem at hand. good code is as simple as possible. good code is testable and tested. good code is minimal. good code is performant. good code is maintainable. good code is understandable.
yes it matters somewhat that you have some consistency of the way you code. that's a secondary thing
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.
1) your code will be written once but read a thousand times
2) if you want to make your mark with style and convention start an OSS project of your own, otherwise make your mark through the simplicity and elegance of your logic, not the way your code contrasts from the code base.
Something could be written using Java 1.4 idioms with good organization and factoring, or using the latest functional hoohas with everything all mashed together. Or the inverse, of course.
If the structure of the code matches the structure of the problem, the implementation style is a much smaller deal.
nor is there such a thing as a good love letter