Most active commenters
  • xxs(3)

←back to thread

504 points azhenley | 25 comments | | HN request time: 1.166s | source | bottom
1. hyperhello ◴[] No.45767863[source]
> I wish it was the default, and mutable was a keyword.

I wish the IDE would simply provide a small clue, visible but graphically unobtrusive, that it was mutated.

In fact, I end up wishing this about almost every language feature that passes my mind. For example, I don't need to choose whether I can or can't append to a list; just make it unappendable if you can prove I don't append. I don't care if it's a map, list, set, listOf, array, vector, arrayOf, Array.of(), etc unless it's going to get in my way because I have ten CPU cores and I'll optimize the loop when I need to.

replies(8): >>45768027 #>>45768166 #>>45768207 #>>45768240 #>>45768356 #>>45769342 #>>45769717 #>>45770340 #
2. bee_rider ◴[] No.45768027[source]
I don’t have any useful ideas here but if you make a linter for this sort of thing, I suggest calling it “mutalator.”
3. estimator7292 ◴[] No.45768166[source]
Your IDE probably supports this as an explicit action. JetBrains has a feature that can find all reads and writes to a variable
replies(1): >>45769329 #
4. nielsbot ◴[] No.45768207[source]
I use Swift for work. The compiler tell you this. If a mutable variable is never mutated it suggests making it non-mutable. And vice versa.
replies(2): >>45769302 #>>45769704 #
5. NathanaelRea ◴[] No.45768240[source]
I don't think this is the best option, there could be very hard bugs or performance cliffs. I think I'd rather have an explicit opt-in, rather than the abstraction changing underneath me. Have my IDE scream at me and make me consider if I really need the opt-in, or if I should restructure.

Although I do agree with the sentiment of choosing a construct and having it optimize if it can. Reminds me of a Rich Hickey talk about sets being unordered and lists being ordered, where if you want to specify a bag of non-duplicate unordered items you should always use a set to convey the meaning.

It's interesting that small hash sets are slower than small arrays, so it would be cool if the compiler could notice size or access patterns and optimize in those scenarios.

replies(1): >>45768282 #
6. bbminner ◴[] No.45768282[source]
Right, sql optimizers are a good example - in theory it should "just know" what is the optimal way of doing things, but because these decisions are made at runtime based on query analysis, small changes to logic might cause huge changes in performance.
7. HDThoreaun ◴[] No.45768356[source]
Clang-tidy's misc-const-correctness warns for this. Hook it up to claude code and it'll const all non mutated mutables.
8. qmmmur ◴[] No.45769302[source]
As will Typescript, at least using Biome to lint it does.
replies(2): >>45773283 #>>45774260 #
9. Denvercoder9 ◴[] No.45769329[source]
It also has the ability to style mutated variables differently.
replies(1): >>45769672 #
10. worthless-trash ◴[] No.45769342[source]
If you write in erlang, emacs does this by default ;)
11. greenicon ◴[] No.45769672{3}[source]
Yes, depending on your highlighting scheme. Not every highlighting scheme shows this by default, unfortunately.

To me, this seems initially like some very minor thing, but I find this very helpful working with non-trivial code. For larger methods you can directly discern whether a not-as-immutable-declared variable behaves immutable nonetheless.

12. bartvk ◴[] No.45769704[source]
Yup, it's pretty great. You get into the habit of suspiciously eyeing every variable that's not a constant.
13. throwaway2037 ◴[] No.45769717[source]
In my IntelliJ (a recent version), if I write a small Java function like this:

    private static void blah()
    {
        final int abc = 3;
        for (int def = 7; def < 20; ++def)
        {
            System.out.print(def);
        }
    }
The variable 'def' is underlined. Mouse-over hint shows: 'Reassigned local variable'. To be clear, 'abc' is not underlined. When I write Java, I try to use the smallest variable scopes possible with as much final (keyword) as possible. It helps me to write more maintainable code, that is easier to read.
replies(3): >>45771853 #>>45774068 #>>45774408 #
14. considerdevs ◴[] No.45770340[source]
Could Pylint help? It has atleast check for variable redefinition: https://pylint.pycqa.org/en/latest/user_guide/messages/refac...
replies(1): >>45778212 #
15. xxs ◴[] No.45771853[source]
1st) you use ++def in a loop, don't be weird; 2nd) if 'abc' is to be used in the loop body, define in the loop, e.g. for (int def = 7, abc =3; ...); 3rd) this is an IntelliJ bug - both 'def' and 'abc' in the sample are always defined.
replies(2): >>45772154 #>>45772852 #
16. pacoverdi ◴[] No.45772154{3}[source]
3) looks like you read 'underlined' as 'undefined'
replies(1): >>45772279 #
17. xxs ◴[] No.45772279{4}[source]
true that, thanks!
18. sitzkrieg ◴[] No.45772852{3}[source]
the only thing that is weird is your lack of understanding temporary variables
replies(1): >>45772896 #
19. xxs ◴[] No.45772896{4}[source]
perhaps... yet, Java doesn't have a definition for temporary variables
20. maleldil ◴[] No.45773283{3}[source]
eslint has this too: https://eslint.org/docs/latest/rules/prefer-const
21. shagie ◴[] No.45774068[source]
As an aside, you might also enjoy the inline inferred annotations.

https://www.jetbrains.com/help/idea/annotating-source-code.h...

Seeing @NotNull in there even if the author hasn't specifically written that can help in understanding (and not needing to consider) various branches.

22. nielsbot ◴[] No.45774260{3}[source]
My very minor complaint about TypeScript is you use to use `const` which is 2 additional letters.

Seriously though, I do find it slightly difficult to reason about `const` vars in TypeScript because while a `const` variable cannot be reassigned, the value it references can still be mutated. I think TypeScript would benefit from more non-mutable values types... (I know there are some)

Swift has the same problem, in theory, but it's very easy to use a non-mutable value types in Swift (`struct`) so it's mitigated a bit.

23. e-topy ◴[] No.45774408[source]
This works in RustRover as well! Super useful.
replies(1): >>45775348 #
24. sn9 ◴[] No.45775348{3}[source]
Rust's type system specifically facilitates more powerful tools: https://github.com/willcrichton/flowistry
25. spidersouris ◴[] No.45778212[source]
For type only though.