I think "competitive" (i.e. solving algorithmic challenges for fun) coding really gives you some insight into how to write code that's short and to the point. The user with the most reputation on LeetCode, for example, consistently posts solutions that are surprisingly short, efficient, and readable. https://discuss.leetcode.com/user/stefanpochmann (some random examples) https://discuss.leetcode.com/topic/18731/7-lin…
Applying the Linus Torvalds “Good Taste” Coding Requirement
61–70 of 302 posts
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#62I 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.
People will and are arguing that "it is kernel code, it should be complicated" but I really think this is backwards. Because it is low level and debugging is really cognitively intense and difficult at this level the code should be even clearer and easier to understand.
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#63"A lot of conditionals arise from fuzzy thinking about the problem. In servo-control theory, a lot of people think that the algorithm for the servo ought to be different when the distance is great than when it is close. Far away, you’re in slew mode; closer to the target you’re in decelerate mode; very close you’re in hunt mode. You have to test how far you are to know which algorithm to apply."
"I’ve worked out a non-linear servo-control algorithm that will handle full range. This approach eliminates the glitches at the transitioning points between one mode and the other. It eliminates the logic necessary to decide which algorithm to use. It eliminates your having to empirically determine the transition points. And of course, you have a much simpler program with one algorithm instead of three."
"Instead of trying to get rid of conditionals, you’re best to question the underlying theory that led to the conditionals."
That's part of a chapter of the book called Minimizing Control Structures. Forth guys are crazy about taste, and if I've learned anything from reading their stuff, it's that chasing tasteful programming to its end gets very hard.
OP is right on the money. The hard thing is that it is a creative process, and takes a real understanding of the problem you're solving to do it. Worst of all, aside from the feeling of solving a puzzle well, the benefits only begin appearing much later. I'm glad the kernel team takes it seriously.
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#64I 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…
> both versions ignore an elephant in the room: defensively dealing with `entry` being absent from the list. Indeed, both versions really have a poor taste. Both are code snippets which can only work under very restrictive assumptions. The main argument `head` is implied and the `entry` to be removed must be present. Such piece of code has to be duplicated and tuned for every list and use case. I even wonder why Linu…
Sure, you need to be massively defensive if you're writing an API or boundary, but within an owned codebase, if you're eliminating edge-cases, you should follow them all the way up.
tl;dr if you're calling remove_list_entry, only do so if you have an entry.
Or course, I'm not Linus, so I have no idea of his thought process.
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#65The author doesn't mention that the grid being initalized is square, and simply describes it as grid[rows][cols]. However, all his solutions are only correct for a square grid. One solution for arbitrary dimensions could be for (i = 0; i
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#66These are great examples, and they hint to, but do not mention, the big counterpoint: development time. In his own examples, the author admitted that though the code was ugly, it worked. He then spent extra time reworking the existing code to make it, well, prettier. "If I had more time, I would have written a shorter letter." -- Voltaire The problem is that, in many (most?) professional settings, the developer is un…
This demonstrates the general move in the marketplace from companies developing software, to instead hacking out quick product for fast money. The startup mentality has made this worse, and continues to do so. Good software requires thought and time to get right. Unfortunately, those people buying software are often willing to pay for low-quality hacks these days, in the name of a quick fix. It's just another commodi…
I can attest that there never were "good ole days" of software development. There never was "take all the time you want, do it right". There were always schedules, commitments, and budgets.
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#67Speaking 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…
Switch statements have fall-through behavior that you need `break` to avoid (in most languages); sometimes this is what you want, but only rarely. One trick I've seen people do in Python is to use a dict; define lambdas for each case, and then key the dict on your value that you would switch on, and invoke the lambda (or use existing functions). def fake_switch(self, thing): { 'foo': self.do_foo, 'bar': self.do_bar,…
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#68Earlier quoted context omitted.
Re: #2 - in Linux case, double-pointer list removal succinctly illustrates the minimum C proficiency needed for tinkering with kernel code. That is, not every project has "shortening ramp-up time" as a priority. In quite a few cases non-trivial code doubles as filter for less skilled devs.
Projects should be accessible to new developers regardless of their complexity level. Even operating systems. Otherwise they will simply stagnate and die. Also, "less skilled devs" are just people that are still learning. We've all been there.
Projects should be accessible to new developers with appropriate skill set, which is inherently defined by the projects' complexity.
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#69I 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…
Re: #2 - in Linux case, double-pointer list removal succinctly illustrates the minimum C proficiency needed for tinkering with kernel code. That is, not every project has "shortening ramp-up time" as a priority. In quite a few cases non-trivial code doubles as filter for less skilled devs.
An OS kernel is going to be a very complex piece of code (ignoring microkernels) because what it does is complex. But that is all the more reason to keep the code as straightforward as possible. (There are still going to be times where code is not straightforward due to inherent complexity of the problem, or performance considerations, or portability, ...)
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#70The author doesn't mention that the grid being initalized is square, and simply describes it as grid[rows][cols]. However, all his solutions are only correct for a square grid. One solution for arbitrary dimensions could be for (i = 0; i
Replace your second for statement with
for (i = 1; i
and you're golden.