this post was submitted on 09 May 2024
46 points (100.0% liked)

Linux

1259 readers
106 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

Tabs are 8 characters, and thus indentations are also 8 characters. There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.

I am in love with this awsome document; I love its guidelines, and coding conventions.

However, when Rust was introduced into the kernel, they butchered these beautiful guidelines, I know it's hard to look at such heretic actions, but you have to see this:

The default settings of rustfmt are used. This means the idiomatic Rust style is followed. For instance, 4 spaces are used for indentation rather than tabs.

How can this even relate to the ideology of the first document? I am deeply saddened by these new rules.

I know this is "The Rust experiment", but this must be fixed before it's too late! This has to reach someone.

A counter-argument might be:

The code should be formatted using rustfmt. In this way, a person contributing from time to time to the kernel does not need to learn and remember one more style guide. More importantly, reviewers and maintainers do not need to spend time pointing out style issues anymore, and thus less patch roundtrips may be needed to land a change.

And to that I say that rustfmt is configurable per project, and if it isn't, then it has to be. Doesn't something like .editorconfig exist?

Edit: I think I read enough comments to come up with a conclusion.

At first, forcing another language's code style upon another sounds backwards, but both styles are actually more similar than I originally though, the kernel's C style is just rustfmt’s default with:

  • 80 character line.
  • 8-space hard tabs.
  • Indentation limited to 3.
  • Short local-variable names.
  • Having function length scale negatively with complexity.

The part about switch statements doesn't apply as Rust replaced them with match.*

The part about function brackets on new lines doesn't apply because Rust does have nested functions.

The bad part about bracket-less if statements doesn't apply as Rust doesn't support such anti-features.

The part about editor cruft is probably solved in this day & age.

The rest are either forced by the borrow checker, made obsolete by the great type system, or are just C exclusive issues that are unique to C.

I left out some parts of the standard that I do not understand.

This all turned up to be an indentation and line-size argument. Embarrassing!

*: I experimented with not-indenting the arms of the root match expression, it's surprisingly very good for simple match expressions, and feels very much like a switch, though I am not confident in recommending to people. Example:

match x {
5 => foo(),
3 => bar(),
1 => match baz(x) {
	Ok(_) => foo2(),
	Err(e) => match maybe(e) {
		Ok(_) => bar2(),
		_ => panic!(),
		}
	}
_ => panic!(),
}

top 23 comments
sorted by: hot top controversial new old
[–] KindaABigDyl@programming.dev 36 points 6 months ago (1 children)

My biggest disagreement is this:

Do not unnecessarily use braces where a single statement will do.

Always put braces around if statements. It will bite you in the butt

[–] Doods@infosec.pub 8 points 6 months ago* (last edited 6 months ago)

This one in particular I am against. (it's not like it's possible in rust anyway)

[–] vzq@lemmy.blahaj.zone 30 points 6 months ago (2 children)

You are just stoking the flames of a meaningless holy war.

[–] Doods@infosec.pub 7 points 6 months ago* (last edited 6 months ago) (1 children)

The kernel had a consistent style before rust was even an idea! Who do you think has started this inconsistency? (Maybe not, what does someone like me know about the kernel anyway)

[–] vzq@lemmy.blahaj.zone 7 points 6 months ago (1 children)
[–] Doods@infosec.pub 2 points 6 months ago

Agreed, and upvoted.

[–] thingsiplay 5 points 6 months ago

To be fair, consistent styling across the entire project is not meaningless. But on the other side, I don't think this needs to be fixed here, because it is already consistent (within their language).

[–] hunger@programming.dev 27 points 6 months ago (1 children)

Rustfmt is not very configurable. That is a wonderful thing: People don't waste time on discussing different formatting options and every bit of rust code looks pretty identical.

[–] Doods@infosec.pub 13 points 6 months ago* (last edited 6 months ago) (3 children)

My emotions just stopped, so I can now think straight.

There are really only 2 changes that - in my eyes - should be made:

  • 8 space-long, hard tabs.
  • 80 character limit instead of 100.

I don't think a tool like rustfmt can affect most of the original guidelines, and it's generally compatible with the OG style by default.

Edit: I - surprisingly - never actually used rustfmt, so I will go now and test before I say something stupid.

Edit II: I just found this on their repo:

Rustfmt is designed to be very configurable.

Edit III: I tested rustfmt with:

hard_tabs = true

max_width = 80

It's great!

[–] acockworkorange@mander.xyz 21 points 6 months ago (1 children)

We are not stuck to DEC VT100 terminals anymore. It’s okay to have 100 columns of code. And wasting 10% of that space for each indentation? What are you smoking?

[–] Doods@infosec.pub 3 points 6 months ago* (last edited 6 months ago)

wasting 10% of that space for each indentation? What are you smoking?

As I said before, this standard is older than C itself, and the kernel's been using it for decades, I shouldn't have to explain it. Long tabs and short lines boost readability, and restricting indentation to 3 solves the problem. Read my reply to 1rre@discuss.tchncs.de for more context.

Also rustfmt didn't move the string in

println!("a very long string slice with a static lifetime"); to a new line even when it exceeded a 100 columns, I should seek a solution.

Note: The actual string I used was way longer than that.

[–] thingsiplay 9 points 6 months ago

8 space-long, hard tabs.

Hard disagree. 8 spaces is waste and 4 should be industry standard. Tabs should not be used for indentation, but spaces. On the other side, Tabs are configurable, so that's actually a plus point.

80 character limit instead of 100.

Why? 80 is an old standard with limitations that do not apply today anymore. We have wider screens and higher resolutions. While it makes sense to keep this to be consistent with previous code and language defaults for C, there is no reason to enforce this for the new adopted language, which already has a standard on its own.

And yes rustfmt can be configured and when I started with Rust I changed max_width to 80, just because I was used to it with Python. But there is no benefit doing this in Rust.

[–] eveninghere 1 points 6 months ago

I think they can just ban tabs. And do some automatic rejection via... what? Are kernel devs still using email attachment for this?

[–] thingsiplay 16 points 6 months ago* (last edited 6 months ago)

How can this even relate to the ideology of the first document? I am deeply saddened by these new rules.

The previous document was written in a time when only C was the language in the Kernel. Now that two different languages and eco systems exist, it makes lot of sense to not mix them up. The document with guidelines for C code was needed, because there was no uniform guide that every user used. But with Rust it is different. There exist rustfmt and practically every user is learning and writing code with it and every public documentation and library is using this tool; most of them with default values.

You don't mix C with Rust anyway, so why do you want force every Rust programmer and code formatted like C? I don't think this is an issue that needs to be fixed. Are you programming for the Linux Kernel? Do you do it in C and Rust? Otherwise, why do you think this is a problem?

Edit: Also the very first paragraph and second sentence in the linked document says this:

Coding style is very personal, and I won’t force my views on anybody, but this is what goes for anything that I have to be able to maintain, and I’d prefer it for most other things too.

But you seem to ignore that and force everyone to write code like this, declaring it to be an issue in the code (which it isn't). Do you not think that other Linux developers didn't think of this? Why is this an issue that has to be fixed and how do you explain this? Why does the C coding standard apply to Rust, when Rust has its own established one?

[–] ReversalHatchery 14 points 6 months ago (1 children)

Is this a joke?

Obviously, 8 wide tabs are too much. That's like defining Pi as 5.

[–] Doods@infosec.pub 4 points 6 months ago (1 children)

No, it is not, people have been using 8-space tabs even back when terminals were limited to 80 characters.

[–] iiGxC@slrpnk.net 12 points 6 months ago

Yeah, sometimes people make mistakes but we can change for the better.

Personally, I wish everyone would just use the tab character and configure how it gets displayed in their editor, instead of imposing spacing on everyone

[–] penquin@lemm.ee 7 points 6 months ago (1 children)

Finally, something is beating python in how ridiculous it is. Yay!

load more comments (1 replies)
[–] KRAW@linux.community 5 points 6 months ago

What happens when the defaults change? Is old code all of a sudden not following the style guidelines?

[–] Turbo@lemmy.ml 5 points 6 months ago (1 children)

Agreed not tabs

8 spaces seems excessive.

4 spaces or even 2 would be my preference.

Disclaimer: lots of programming but no rust experience. Though, I don't think that matters.

[–] adamnejm@programming.dev 4 points 6 months ago

First saying that tabs are a no go, then proceeding to deliberate on the correct amount of spaces...

Long live tabs!

[–] 0x0@programming.dev 4 points 6 months ago (1 children)

Silicon Valley vibes (the series).

Doesn't the kernel have a coding standard?

[–] Doods@infosec.pub 2 points 6 months ago* (last edited 6 months ago)

It has a long-lasting C coding standard, they call it the standard since it was the only language anyway. Then, they made a newly conceived Rust standards, which ignore everything the original standard stood for. (Note the strong language in the post's first quote, it's from the original standard)