Earlier quoted context omitted.
Conditionals are so simple it’s basically the first thing anyone learns. Anyone can understand the first approach. The latter requires knowledge that is specific to lower level languages,concepts that are certainly not taught universally. The latter approach is definitely not the easiest to understand. It is, however, what Linus considers «good taste in coding.»
> Conditionals are so simple it’s basically the first thing anyone learns. That's neither here, nor there though. The simplicity of a concept does not directly translate to how hard it makes the code (assembly is simpler in the sense of having fewer and simpler constructs than e.g. Python but much much harder to code in). Conditionals, and the exponential increase in state space they bring, are still one of the bigge…
Linus Torvalds' good taste argument for linked lists, explained
281–290 of 339 posts
Re: Linus Torvalds' good taste argument for linked lists, explained
#282Earlier quoted context omitted.
Hacker news has code blocks. Have the block indented by for spaces. this line has 4 spaces in front of it. What Hacker news could use instead are quote blocks. Now people often use the code blocks as quote blocks.
HN codeblocks are just two spaces indented. Any extra spaces indent further. This has no superfluous indentations.
Re: Linus Torvalds' good taste argument for linked lists, explained
#283Earlier quoted context omitted.
Imo every piece of code (for reasonable definitions of "piece") is supposed to check its own preconditions and not rely on the caller to check them.
This isn't possible in many cases - consider the simple C library function strlen, one of who's preconditions is that it must only be called on a zero-terminated string. There is no way to write code in strlen to check that this is true.
Re: Linus Torvalds' good taste argument for linked lists, explained
#284Earlier quoted context omitted.
Are you arguing that we should leave known crash bugs in the code?
What you are espousing is known as “defensive programming” which is very much a bad thing (tm).
Re: Linus Torvalds' good taste argument for linked lists, explained
#285Back in the 80's, I learned Pascal, and learned about its dynamically allocated records, then I went on to learn C, and got used to its pointers and arrays. Then I went back to Pascal, and designed a program in my head with some dynamically allocated linked list data structures, and another data structure that had a member that pointed to the head of the linked list. Then I started typing in the Pascal code, and hit…
Re: Linus Torvalds' good taste argument for linked lists, explained
#286Earlier quoted context omitted.
This is nice in theory but a bad idea in practice (as a blanket statement, I am all for checking preconditions in general). An easy example is binary search, which I think is a reasonable "piece" of code by your definition. One should never check its preconditions _inside_ the binary search function (that the list of elements being searched is partitioned by the search predicate). Checking the precondition is O(n) wh…
You're right, but checking for null is not terribly expensive compared to iterating over a linked list.
From a black box, zero-knowledge, perspective it’s maybe worth remembering that code built this way successfully runs mission critical systems all over the world every day. Thoughtful people would have switched to other (slower, more pedantic) systems if doing things differently was a real overall life improvement. People have had the choice, there are plenty of OS’s out there... some far more formal/pedantic. Linux wasn’t the safe choice back in the day, it was just better by several important metrics consistently over time.
Perhaps these insanely experienced kernel devs know what they are doing.
Re: Linus Torvalds' good taste argument for linked lists, explained
#287Earlier quoted context omitted.
Are you arguing that we should leave known crash bugs in the code?
I'm arguing that we should use asserts or similar defensive coding mechanisms to ensure that preconditions are met instead of merrily continuing to run our program with cascading error effects.
Re: Linus Torvalds' good taste argument for linked lists, explained
#288Re: Linus Torvalds' good taste argument for linked lists, explained
#289Back in the 80's, I learned Pascal, and learned about its dynamically allocated records, then I went on to learn C, and got used to its pointers and arrays. Then I went back to Pascal, and designed a program in my head with some dynamically allocated linked list data structures, and another data structure that had a member that pointed to the head of the linked list. Then I started typing in the Pascal code, and hit…
Most programming languages don't have an "address of" operator. That's because it's a potentially unsafe operation. I think your usage of Pascal sounds more like trying to operate a motorcycle like a bicycle.
So Linus's elegant linked list solution is possible in C, but not Pascal.
I don't get your metaphor that Pascal is a motorcycle and C is a bicycle. Just the opposite.
Re: Linus Torvalds' good taste argument for linked lists, explained
#290Earlier quoted context omitted.
That code is not a general purpose library function; it assumes the element exists. If you call that method and the element doesn't exist, presumably something has gone wrong already. Adding a NULL pointer check is not going to fix it. You just silently ignore the error. You'll prevent the crash, but there's no mechanism for handling the error.
And instead of adding a check and crashing visibly here you wait even longer until your program misbehaves due to the error? The longer the program runs after something went wrong, the harder it is to debug, and the more likely it becomes that you get an exploitable bug.