Live data from Hacker News

Applying the Linus Torvalds “Good Taste” Coding Requirement

medium.com

31–40 of 302 posts

Re: Applying the Linus Torvalds “Good Taste” Coding Requirement

#31

I don't know why I'm even replying, this will get so much hate here, oh well... 1. Just because things aren't done "the way you would do them" doesn't mean they're "bad" or "wrong". 2. If you're not on a solo project, I've found writing correct but less "clever" code to help shorten ramp-up time for new devs and be more beneficial to future maintainability of the code-base and system. TL;DR; Swapping values by XOR'in…

Absolutely.

Further, I would consider Linus's one-liner 'indirect=...' to be 'clever' code.

If I were reviewing it, I would want it expanded into more obvious code as it requires too much cognitive overhead to parse.

Re: Applying the Linus Torvalds “Good Taste” Coding Requirement

#32

Not being used to C syntax, I took this as a typo at first: indirect = &(*indirect)->next; The position of the parentheses make it look like you're dereferencing only (* indirect). But on second look, you need the parens there so that it's (* indirect)->next as opposed to indirect->next. Then the & operates on the whole thing. I'd be tempted to wrap it in a second set of parens for clarity, but perhaps it's entirely…

To me, its 'clever' code.... i.e. it should be simplified.

This is not good code, it requires too much thought to realise what its doing.

Re: Applying the Linus Torvalds “Good Taste” Coding Requirement

#33

Not being used to C syntax, I took this as a typo at first: indirect = &(*indirect)->next; The position of the parentheses make it look like you're dereferencing only (* indirect). But on second look, you need the parens there so that it's (* indirect)->next as opposed to indirect->next. Then the & operates on the whole thing. I'd be tempted to wrap it in a second set of parens for clarity, but perhaps it's entirely…

I think the whole example comes down to Linus being really good at C. He wants code from people who are similarly fluent in the idioms and syntax of the language.

Re: Applying the Linus Torvalds “Good Taste” Coding Requirement

#34
Speaking of coding niceties, when I started branching out from bash into a 'real' language, Python, I couldn't find a switch statement. A colleague of mine said that Python didn't have one, and that switch statements were a 'code smell'. I didn't really understand that, and asked about those times that you genuinely could use a switch statement, and the answer was "just use a long if-else function". I can't remember his justifaction for it, so clearly it didn't stick

I still don't see the real pragmatic difference between a switch statement and a long if-else function - anyone feel like ELI5'ing it for me? Switch statements just seem more concise and readable to me...

Re: Applying the Linus Torvalds “Good Taste” Coding Requirement

#35

For my money, Linus's example of "good taste" gives up rather a lot of clarity to achieve succinctness. The original is simple and clear. His preferred version is shorter, but also harder to understand because of its use of a complicated indirection. And that's not good taste. It's just showing off. “Programs must be written for people to read, and only incidentally for machines to execute.” ― Harold Abelson, Structu…

I completely agree (tho you're getting downvoted by others).

Linus's code here is 'clever'... but not good, simple code.

Re: Applying the Linus Torvalds “Good Taste” Coding Requirement

#36

I dunno, the first example seems unsatisfying. The original has that ugly condition, the "good" version seems overly clever. And for all the talk of taste and aesthetics, both versions ignore an elephant in the room: defensively dealing with `entry` being absent from the list. Not that I've never abused addresses like this. But having written this multiple times, I currently prefer something like this: remove_list_en…

Another issue both versions have is that they will not tolerate head being NULL.

Don't be afraid of double pointers, although they may seem overly clever at first glance, they can be really helpful for situations like this. Linus's code is considerably shorter, and not particularly difficult to debug or to understand, assuming you understand double pointers.

Another case where double pointers can be really useful is in the interface for linked lists. I.e.

    remove_list_entry(node** head, node* entry) { [...] }
Defining the interface that way allows properly removing the last entry from the list. Workarounds like making a list struct and passing a pointer to that are just double pointers in disguise. Returning the new head is error prone, since you can easily forget to reassign it.

Double pointers for life!

Re: Applying the Linus Torvalds “Good Taste” Coding Requirement

#37
> To the best of my ability to discern, the crux of the “good taste” requirement is the elimination of edge cases, which tend to reveal themselves as conditional statements. The fewer conditions you test for, the better your code “tastes”.

looks at his Swift code and gulps

Re: Applying the Linus Torvalds “Good Taste” Coding Requirement

#38
Both versions suck. If "entry" is not found in the list, the code will run off the end of the list, either de-referencing zero and faulting or going off into junk, depending on how the list ends.

Writing low-level list manipulation more than once sucks. It leads to bugs. Such manipulation should be encapsulated. That's why we have containers in modern languages. The Linux kernel is still C, not C++, which leads to too much of this sort of thing.

Re: Applying the Linus Torvalds “Good Taste” Coding Requirement

#39
One anti-pattern I've seen a few times, across multiple codebase and companies, is the for i in 1...cases: switch(i)

To me, his first example fits this perfectly, except he is using ifs instead of a switch. Basically, if you are generating a big, trivial sequence and then filtering it down afterwards, think harder and probably there is a way to generate the sequence you are actually after directly.

Re: Applying the Linus Torvalds “Good Taste” Coding Requirement

#40

Earlier quoted context omitted.

My preferred whitespace code-style is to use tabs for indentation What does "I convert it back to 4 space tabs before I commit anything" mean, then? If you're putting \t tabs in your file, 4 space tabs vs 8 space tabs refers to the way your editor renders \t. What are you changing before committing, your editor settings? Edit: oops, just realized you're not the person I was responding to. The convert it back before I…

Sorry, I should have mentioned that I was not the GP. Even if you use spaces rather than tabs, the idea is the same. Do you mean something like the below? Note: \t represents tabbing (not necessarily the number of tabs, just spacing using tabs), and _ represents spaces. int f(int x, \t\t__int y) // one 4-width tab, pad with spaces { \t\t// do stuff; } int f(int x, \t\t\t\tint y) // one 8-width tab, whoops { \t\t\t\t/…

Even if you use spaces rather than tabs, the idea is the same

It's not, because there does not exist an editor that is smart enough to recognize that these 4 spaces are indentation tabs while these 4 spaces are spacing spaces. It's not impossible, but show me an editor that does it today (especially with C++) and I'll eat my hat :D

I agree with you that if you're using actual tabs for indentation it works fine.

Post reply on HN