Applying the Linus Torvalds “Good Taste” Coding Requirement (article)
-
@TimeBandit said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
Fuck useless braces that waste vertical space
Apple agrees with you and created a bug in their SSL library to show how awful it is
To be fair, it's not so bad if you use a recent GCC and
-Wall -Werror
, because misleading indentation now produces a warning.
-
@Yamikuronue said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
Why is it better to modify than to throw out and redo?
Inherently? It isn't.
Besides, I can think of other reasons. This reduction in code complexity relies on implicit meaning and obfuscates the intention of the code. It also requires more expertise to maintain it.
DISCLAIMER: I'm keeping in mind instances of this modification scaled up to real life development, not trivial examples.
-
@xaade said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
This reduction in code complexity relies on implicit meaning and obfuscates the intention of the code.
Because it's in C. Similar tasks in a more verbose, easier to read language aren't nearly as obfuscated. I agree that you can go way too far, but there's definitely room for elegance.
-
@Yamikuronue said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
@xaade said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
This reduction in code complexity relies on implicit meaning and obfuscates the intention of the code.
Because it's in C. Similar tasks in a more verbose, easier to read language aren't nearly as obfuscated. I agree that you can go way too far, but there's definitely room for elegance.
In C? Not even C++?
Well, fuck, C is the programming language for the greybeards.
Moving along. Nothing to see here...
-
@xaade said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
In C? Not even C++?
Both statements were made before they started to modernize C++, so IDK whether his opinions have changed in the meantime. I very much doubt it, though. That guy is stubborn as hell.
-
@asdf said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
Both statements were made before they started to modernize C++, so IDK whether his opinions have changed in the meantime. I very much doubt it, though. That guy is stubborn as hell.
He started using it to code SubSurface
He's using QT, which makes C++ less awful ;)
-
@TimeBandit said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
He started using it to code SubSurface
What's next? C++ in the Linux kernel?
@TimeBandit said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
He's using QT, which makes C++ less awful
E_PARSE_ERROR
-
@asdf said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
Linus Torvalds on C++
That's a first-class rant. Entertaining, even if you don't agree with it.*
In other words: the choice of C is the only sane choice. I know Miles
Bader jokingly said "to piss you off", but it's actually true. I've come
to the conclusion that any programmer that would prefer the project to be
in C++ over C is likely a programmer that I really would prefer to piss
off, so that he doesn't come and screw up any project I'm involved with.If you want a VCS that is written in C++, go play with Monotone. Really.
They use a "real database". They use "nice object-oriented libraries".
They use "nice C++ abstractions". And quite frankly, as a result of all
these design decisions that sound so appealing to some CS people, the end
result is a horrible and unmaintainable mess.
But I'm sure you'd like it more than git.*The only sane choices are really Ada and Haskell.
-
@antiquarian said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
That's a first-class rant.
Sometimes I wonder whether Linus has a sock puppet on this forum.
-
@asdf said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
@xaade said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
In C? Not even C++?
Both statements were made before they started to modernize C++, so IDK whether his opinions have changed in the meantime. I very much doubt it, though. That guy is stubborn as hell.
Amazing, I actually agree with Linus on something!
-
@asdf said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
Yup. If you think this is dirty pointer magic, you've never seen truly dirty pointer magic. :P
I have written C++ code like this:
*reinterpret_cast<DWORD*>(this) = 0x7E1AF4;
There was a very good reason for it.
A friend and I decompiled a game, figured out lots of its internals, made these byte-perfect C++ replica headers, compiled a DLL with bugfixes and new features, and injected it into the game at startup. This voodoo was the best way to replicate an inlined constructor assigning a vtable pointer. The fun we had... the horrors we saw and wrote...
-
@asdf Sometimes I think Linus thinks the only good code is his own and that everyone else's is passable at best.
-
@fbmac said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
@kt_ I'm glad I work in a team where nobody complains about the presence or absence of braces, spaces vs tabs, or any other "coding style" bullshit.
Not agreeing on spaces vs. tabs invariably leads to huge diffs in source control that completely obscure what was actually changed just because everybody has to reformat bits and pieces so the colleagues' code won't look godawful.
Braces ... yeah, if they're optional, treat them as optional. I write mostly Perl these days anyway and there they ain't :)
-
@Yamikuronue said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
Why is it better to modify than to throw out and redo?
Because, as Joel Spolsky (I think?) said, what you call an ugly old code full of warts is likely to actually be a code that was tweaked and adjusted to handle all the weird edge-cases and bugs that you didn't think of initially. So by throwing it out you're condamning yourself to falling into these bugs again and adding the edge cases again. Sure, tests will help you do that more quickly and without breaking the rest (assuming you have perfects tests, which is very unlikely!), but you're still going to spend time on that, which you won't have to do if you just modify the code.
There are cases where the piling-on of edge cases actually shows that you took the wrong route from the start, and in that case a change in requirements might be a good opportunity to start from scratch, yes. And cases where the new requirement is so far from the initial one that rewriting the code is the only realistic option. But I would not put that as a rule.
-
@dkf said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
modern compilers transform code pretty thoroughly on the way from what you write to what they issue
...and modern CPUs transform it even more.
Although I would expect the ping-pong pointers version of the list walk to run marginally faster than the naively coded version, that's by no means a certainty; the register-to-register move that it saves might well end up being optimized away by a microcode generator.
Really the only way to find out is to time the walking of a long list on the CPU you care about; and if you actually do care that much about the performance of a linked-list walk, it really is time to look at whether or not the linked list is the best data structure for the job at hand (clue: probably not).
-
@remi said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
Because, as Joel Spolsky (I think?) said, what you call an ugly old code full of warts is likely to actually be a code that was tweaked and adjusted to handle all the weird edge-cases and bugs that you didn't think of initially.
It depends on whether they are necessary warts, or just working around weird features of the environment you're in. The former are just dealing with what the problem really is, but the latter are a tax on you for purposes.
-
@remi said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
assuming you have perfects tests, which is very unlikely!
I'm sorry, you're saying the previous programmer (you or whoever) was perfect enough to hit all the edge cases, had the time to write tests, and was stupid enough not to encapsulate the edge cases they solved for in the tests?
That's the whole fucking point of unit tests: if you have tests around the warts, you can experiment and feel confident you don't have to keep unmaintainable glob just because it might cover some edge cases you didn't think of.
I'm not saying you should always throw out the code, but the idea that you can't ever change the sacred lines laid down before you is bunk.
-
@flabdablet said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
Although I would expect the ping-pong pointers version of the list walk to run marginally faster than the naively coded version, that's by no means a certainty; the register-to-register move that it saves might well end up being optimized away by a microcode generator.
I think more significant would be whether the memory prediction is right. Shuffling stuff around in registers tends to be extremely fast (and jumps are pretty fast too if the branch prediction is right) but forcing the variable to be on the stack just so that you can avoid some trivial jumps could be quite expensive. There's a lot of bits and pieces that interact here, and the results could be quite non-trivial; I would not expect to know whether the code is faster or not by simple inspection. (I would however find that it's a damn awful piece of code for using double-pointers…)
This is exactly why measuring is better than guessing when it comes to performance tuning. ;)
-
@dkf said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
forcing the variable to be on the stack just so that you can avoid some trivial jumps could be quite expensive
True, but the compilation results clearly demonstrate that the indirect-pointer version doesn't do that.
-
@flabdablet under that compiler with that optimisation setting.
It's also well into the reaches of micro optimisation
-
@Yamikuronue said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
I'm sorry, you're saying the previous programmer (you or whoever) was perfect enough to hit all the edge cases, had the time to write tests, and was stupid enough not to encapsulate the edge cases they solved for in the tests?
No, of course ideally all these edge cases are in tests (again, ideally, because we all know that in reality if some code comes from old stuff and various places, things are never perfect). But when you rewrite the function (code, whatever) from scratch, you are still going to spend time fixing the code so that all those tests pass again. So OK, my quip at "perfect" tests was not really warranted here as hopefully the tests are good enough to cover all the cases that caused the warts in the initial code.
Still, having tests doesn't mean for me that rewriting is necessarily a better idea than adapting.
I'm not saying you should always throw out the code, but the idea that you can't ever change the sacred lines laid down before you is bunk.
Me neither. I'm just saying that, by default, my position is rather on the "incremental and minimal changes" rather than "rewrite everything in a way that looks nicer". But that's just a "default setting", there are many cases where rewriting is warranted.
-
@remi I think having cleaner code to maintain is valuable in and of itself. My default would be to clean it up unless there's a prohibitive number of "warts". After all, you got here by people being lazy and just throwing in another "if" statement. A proper refactoring where you try to figure out what assumptions were originally made that were poor can often be a godsend.
-
@Jaloopa said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
under that compiler with that optimisation setting
In 2016, gcc's optimization is probably the minimum one could reasonably expect to encounter. It's good, but most are now better.
-
@remi said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
But when you rewrite the function (code, whatever) from scratch, you are still going to spend time fixing the code so that all those tests pass again.
If you've got the tests, then you'll run into those cases again immediately, rather than when someone complains, reducing impact. Also you'll run into it before you merge, so you can back out if it doesn't work out.
-
@Yamikuronue said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
@remi I think having cleaner code to maintain is valuable in and of itself.
Yes, that's a good point. My fear is that while you think you are cleaning, it's very easy to overlook good reasons that lead to the warts that you want to remove.
figur[ing] out what assumptions were originally made that were poor
... can easily be wrong! I see that often in code that has been modified by several devs. Someone comes along, sees something that looks wrong, or weird, or purely ugly, and changes it, only to have another dev explaining immediately that there was a good reason for that edge case (doesn't matter whether this is seen in tests, code review, discussion between coders or anything, the result is a loss of time -- if not new bugs!).
Sure, this is usually a code smell that the design is wrong (or at least no longer adapted to the needs), and that code usually needs to be refactored. But it's all too easy to sweep in and erase carefully thought out (if badly executed) code, all because you think that you understood everything...
Again, my point is definitely not to never refactor or rewrite, but rather that refactoring/rewriting should not be your first port of call, but only one possible choice that you pick after careful consideration.
-
@remi said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
only to have another dev explaining immediately that there was a good reason for that edge case
If the dev has to explain, you've done it wrong. Write a test that explains, comment the code, and leave the explanation in the commit message. You can't be chasing down everyone who comes after you!
-
@PleegWat said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
@remi said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
But when you rewrite the function (code, whatever) from scratch, you are still going to spend time fixing the code so that all those tests pass again.
If you've got the tests, then you'll run into those cases again immediately, rather than when someone complains, reducing impact. Also you'll run into it before you merge, so you can back out if it doesn't work out.
Sure, but in both cases you have spent more time than just adding one more wart to the code.
Not saying this is not worth it in some cases, but I've seen too many cocky and over-confident devs (including me, let's be honest!) who think that rewriting will be quicker, only to end up spending much more time and effort than the quick-and-dirty...
It's a balance, and personnally I'd rather shift it towards conservativeness.
-
@remi I guess the thing is, I have a more devopsy philosophy: make safe systems where you know right away if you broke something, and then go nuts experimenting. Worst case, you can't make all the tests pass without making it just as ugly as it was, so you throw away your branch and go back to what you had in the repo. The time spent learning the code and why it is the way it is is just as valuable as time spent writing code you're going to keep: it saves you a lot of time when writing code later, over the lifetime of the product.
-
@Yamikuronue said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
@remi said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
only to have another dev explaining immediately that there was a good reason for that edge case
If the dev has to explain, you've done it wrong. Write a test that explains, comment the code, and leave the explanation in the commit message. You can't be chasing down everyone who comes after you!
Yeah, right. If you think that's how all code is always written, I've got a bridge to sell...
Again, that's the ideal world. Sometimes people do it that way. Sometimes what seems obvious to one dev when writing the commit message does not look obvious at all to the next one (I mean, look at how this thread started!). Sometimes one dev writes in a kind of borked English that he thought made sense but didn't. Sometimes someone is just lazy, just once, just this time but that's enough...
I don't know, I may be working in the wrong place (well, if I'm honest... anyway, that's a lot of other stories!), but it does happen regularly that someone doesn't fully understand some code and goes to ask the original dev if he remembers what that was about. And on the whole, I'd rather have that than everyone thinking they can change anything by themselves.
-
@Yamikuronue said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
The time spent learning the code and why it is the way it is is just as valuable as time spent writing code you're going to keep: it saves you a lot of time when writing code later, over the lifetime of the product.
Yup. It does make sense to timebox the attempted rewrite though.
-
@remi said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
Again, that's the ideal world
The thing is, anytime you say "it should be X way", you're talking about ideal conditions. And if your ideal conditions are, "I don't have tests, the guy left a year ago, and the commit messages are in Mandarin", then you need help.
-
@PleegWat Absolutly. I'm a perfectionist, I've had to learn the hard way when to let it go and just let the code be messy.
-
@Yamikuronue said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
The time spent learning the code and why it is the way it is is just as valuable as time spent writing code you're going to keep
Again, on the principle, I can't argue with that. But I would love to be able to really do that all and every time and tell that to my manager (and other people involved). It just doesn't always work that way.
-
@remi I have no doubt you're in a shitty IT situation. I am too (check out Yamirant in the lounge ;) ). But I firmly believe there's a better way and slowly, reluctantly, kicking and screaming, the industry is moving in the right direction.
-
@Yamikuronue said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
@remi I have no doubt you're in a shitty IT situation.
TBH, it could be worse. Not much more, but it could be. At least the non-IT aspects are pretty good (team, management, actual business is pretty interesting...). Plus I'm not a software engineer by training, so I probably don't mind that much letting some ugliness creep in (I keep my perfectionnism for the science that the code is supposed to do!).
OK, I thought it was pretty clear from my messages that I was trying to find an applicable balance between theory and reality, but it seems we've had a misunderstanding on that from the start. If we're talking ideals, then I agree with you, full stop.
I am too (check out Yamirant in the lounge ;) ). But I firmly believe there's a better way and slowly, reluctantly, kicking and screaming, the industry is moving in the right direction.
(I'm not in the Lounge, joined too recently and never bothered about that) I think that on the whole things are indeed getting better.
Hey, you want a slice of my own s to illustrate that? It's only since about 1 year that we have a common source code repository for all people working together, rather than... at least 2 CVS and 1 SVN repo? Unfortunately we couldn't push some guys hard enough and we ended up with SVN rather than git/Hg/... But still better than before, no?
Also, someone recently managed to add a field in the SVN commit box to enter the issue # so that now we can start to link (most) commits to an issue! And we have an automated build system which is not one guy running "make" from a crontab! It's definitely getting better...
(but you get an idea of where I'm coming from...)
-
@remi BTDT. When I started, we had JUST started using SVN. I built our build system singlehandedly :D pissed everyone off doing it, too, but they're okay with it now.
-
@dkf said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
@fbmac said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
but the indirect pointer version is pretty clear
It gives me the heebie-jeebies. :)
I tend to assume that if I write very plain code, the compiler will eventually become smart enough to do good things with that code. I don't assume that when it comes to smartass code, and I would be very hesitant about what the compiler generates without actually looking at it and measuring the performance.
Of course, I'm also not that fond of linked lists in the first place. There are good reasons to use them sometimes, but they've a tendency to have poor memory locality.
"I let the compiler worry about optimization. I don't use linked lists because they're not fully optimized."
-
@Yamikuronue I'm hoping things will improve before the next reorg or we buy another company. These things tend to throw back any effort with an alarming speed!
Although to be fair, these days and given the state of the industry I'm working in, it's more likely that we will be bought than the opposite... when a company has reached the stage where what's protecting it from being bought is that its debt is too large and all potential buyers are in the same situation, it's not good...
-
@remi said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
I may be working in the wrong place (well, if I'm honest... anyway, that's a lot of other stories!)
Lounge thread!
-
@remi said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
Unfortunately we couldn't push some guys hard enough and we ended up with SVN rather than git/Hg/
Did you get that mixed up somehow? You have the correct choice
-
@remi said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
(I'm not in the Lounge, joined too recently and never bothered about that)
Nowadays, you just have to request. No more weird gaming-the-system rules...
-
@Yamikuronue said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
The time spent learning the code and why it is the way it is is just as valuable as time spent writing code you're going to keep
You're not a manager, are you?
-
@LaoC said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
@fbmac said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
@kt_ I'm glad I work in a team where nobody complains about the presence or absence of braces, spaces vs tabs, or any other "coding style" bullshit.
Not agreeing on spaces vs. tabs invariably leads to huge diffs in source control that completely obscure what was actually changed just because everybody has to reformat bits and pieces so the colleagues' code won't look godawful.
Braces ... yeah, if they're optional, treat them as optional. I write mostly Perl these days anyway and there they ain't :)If you write Go, you should go fmt yourself.
-
Unless your code is measurably slower than the alternative, pick the one that's cleaner and easier to maintain.
-
@LaoC said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
Not agreeing on spaces vs. tabs invariably leads to huge diffs in source control that completely obscure what was actually changed
And that, ladies and gentlemen, is what
diff
's--ignore-space-change
was made for. And guess what? it works forgit-diff
, as well.You want better?
git merge -Xignore-space-change
Kids today. Don't know they're born, let alone their arse from their elbow.
-
@tufty said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
And that, ladies and gentlemen, is what diff's --ignore-space-change was made for.
Unless, of course, the file is Python, but in that case, mixing spaces and tabs in the first place is a Very Bad Idea™.
-
@ben_lubar said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):
If you write Go, you should go fmt yourself.
Go can go fmt itself.
-
FTFY:
@ben_lubar said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):If you write Go, you should
go fmt yourselfnot.Alternative FTFY:
@ben_lubar said in Applying the Linus Torvalds “Good Taste” Coding Requirement (article):If you write Go, you should go
fmtf--- yourself.
-
@LaoC You can configure accordingly depending on your source control system.
If you are using TFS before 2013 good luck on that.
-
@ben_lubar Go is just shit.