67 points

permalink
report
reply
42 points

Const everything by default

If you need to mutate it, you don’t, you need to refactor.

permalink
report
reply
45 points

Dogmatic statements like this lead to bad, messy code. I’m a firm believer that you should use whatever style fits the problem most.

Although I agree most code would be better if people followed this dogma, sometimes mutability is just more clean/idiomatic/efficient/…

permalink
report
parent
reply
1 point

I agree somewhat, but I’d also say any codebase needs some level of “dogmatic” standard (ideally enforced via tooling). Otherwise you still end up with bad, messy code (I’d even say messier, as you don’t even get consistency)

permalink
report
parent
reply
-3 points

Define your terms before relying on platitudes. Mutability isn’t cleaner if we want composition, particularly in the face of concurrency. Being idiomatic isn’t good or bad, but patterned; not all patterns are universally desirable. The only one which stands up to scrutiny is efficiency, which leads to the cult of performance-at-all-costs if one is not thoughtful.

permalink
report
parent
reply
13 points

I’d agree with the first half, but not the second. Sometimes mutability allows for more concise code, although in most cases it’s better to not mutate at all

permalink
report
parent
reply
4 points

I feel like I should maybe have put a “probably” in there

After all “there’s no silver bullet”, but in anything but a few edge cases, the rule applies, IMO

permalink
report
parent
reply
8 points

Sorry, I want to make an app that works, not a perfect art piece.

permalink
report
parent
reply
4 points

The app working isn’t good enough, it needs to be maintainable. From a professional perspective, unmaintainable code is useless code.

Code that mutates everywhere is generally harder to reason about and therefore harder to maintain, so just don’t do it (unless there’s literally no other practical way, but genuinely these are very rare cases)

permalink
report
parent
reply
1 point

I personally disagree, forcing yourself to use non mutable variables only leads to longer and more convoluted code.

permalink
report
parent
reply
4 points

Scala user unite! There are dozens of us, dozens!

permalink
report
parent
reply
5 points

Scala? Can we reimplement it in Rust?

permalink
report
parent
reply
4 points

sure, just make sure to add “blazingly fast” in the description and append “-rs” to the name

permalink
report
parent
reply
2 points

But does it do everything in anonymous functions and lambdas?

permalink
report
parent
reply
3 points
*

That is a… strange take.

Random example, imagine a variable that holds the time of the last time the user moved the mouse. Or in a game holding the current selected target of the player. Or the players gold amount. Or its level. Or health. Or current position.

permalink
report
parent
reply
1 point

In all those cases, the answer is to swap in a new variable and throw the old one away.

permalink
report
parent
reply
1 point

Legit question because i think I’m misunderstanding. But if its a const, how are you able to swap or replace it?

permalink
report
parent
reply
2 points
Deleted by creator
permalink
report
parent
reply
11 points

A const object meets an immutable variable

permalink
report
parent
reply
1 point

I think the general idea would be to take the original const, and create a new const with the new location applied. Destroy the original when it’s no longer needed or scoped. State maintained through parameters passed to the move function e.g. move(original const, new location) -> new const object instead of stateful members in the object like move(mutable, new location) -> updated mutable.

permalink
report
parent
reply
20 points

Ngl, it’d solve a lot of bugs

permalink
report
reply
2 points
*
Deleted by creator
permalink
report
parent
reply
3 points

Do you have any example in mind? I’m very interested!

permalink
report
parent
reply
5 points
Deleted by creator
permalink
report
parent
reply
2 points

Is this some joke I’m too Rust to understand?

permalink
report
parent
reply
16 points

The only const in life is to const all the things.

permalink
report
reply
14 points
*

Why even use variables in the first place? Just place the values directly into your code. If you need to change a value, that’s just bad planning. Hell, why even use values either? Just run a loop on the INC instruction until you get the value you need. It’s just efficient programming.

permalink
report
reply

Programmer Humor

!programmer_humor@programming.dev

Create post

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics

Community stats

  • 3.3K

    Monthly active users

  • 1K

    Posts

  • 38K

    Comments