this post was submitted on 09 May 2024
458 points (92.4% liked)

Programmer Humor

32495 readers
679 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 25 points 6 months ago* (last edited 6 months ago) (22 children)

While I totally agree with that philosophy, it heavily depends on the language.

For Rust, my philosophy is more like this:

  • Impl + fn body don't count, as well as async blocks if they span the whole function
  • do not nest more than one if statement. You probably better using guard clauses or matches
  • do not put loops into an if statement.
  • do not nest loops unless clearly shown to be (X, Y) indexing
  • method chaining is free
  • do not nest closures, unless the nested closure doesn't have a {} block
  • do not use mod unless it's test for the current module. No I don't want to Star Wars scroll your 1000 line file. Split it.
[–] [email protected] 5 points 6 months ago (1 children)

I don't know enough Rust to understand by what you mean by the last one. My understanding was that mod name was just declaring the module that this file depends on. Could you explain what I should do instead? Since your other statements I totally agree with, I should probably agree with the last one.

[–] [email protected] 8 points 6 months ago (1 children)

mod name declares that the module should be compiled and reachable as a submodule of the current module. This assumes that you have a file or directory of the name in the right place. This is what you should do.

You can also declare a module like this: mod name {...} where you just put the content in the block. The two are functionally equivalent, from the compilers perspective.

[–] [email protected] 1 points 6 months ago (1 children)

I don't understand how to follow this bullet point that I was replying to.

do not use mod unless it’s test for the current module. No I don’t want to Star Wars scroll your 1000 line file. Split it.

I already know what mod does in a basic sense, I wanted to know what the commenter meant by this.

[–] [email protected] 1 points 6 months ago* (last edited 6 months ago) (1 children)

This point advocates against the use of mod with content in a file unless it is used for a testing module. A common pattern is to have the unit tests for a module inside the main module file. Tests in rust are just specially tagged functions. To avoid compilation costs in non-test builds and false unused code warnings you can put all test related code in a submodule and tag that module with #[cfg(test)]. That way the module will only be included and compiled if the crate is being compiled to run tests.

The Star wars thing refers to scrolling long text files similar to the intro of the starwars movies where a long text is scrolled for the viewer.

[–] [email protected] 1 points 6 months ago

Oh so its just referring to writing the mod's code in the same file the mod is declared in being bad form? That seems very reasonable; since the point of a module is code separation so it makes sense to always put it in its own file. Good, I'm already doing that at least!

load more comments (20 replies)