8 ms·
> Making almost every variable const at initialization is good practice. I wish it was the default, and mutable was a keyword. It's funny how functional progra
by munchler 1y ago
> Making almost every variable const at initialization is good practice. I wish it was the default, and mutable was a keyword.
It's funny how functional programming is slowly becoming the best practice for modern code (pure functions, no side-effects), yet functional programming languages are still considered fringe tech for some reason.
If you want a language where const is the default and mutable is a keyword, try F# for starters. I switched and never looked back.
- Terr_ 1y agoWhile it's amusing, I think it's sensible: One of the main tasks in most businessy programming is to take what a human wants, translate it to code, reverse-translate it back to human understanding later, modify it, and translate it again to slightly different code. This creates friction between casual stakeholder models of a mutable world, versus the the assumptions an immutable/mostly-pure language imposes. When the customer describes what they need, that might be pretty close to a plain loop with a variable that increments sometimes and which can terminate early. In contrast, it maps less-cleanly to a pure-functional world, if I'm lucky there will at least be a reduce-while utility function, so I don't need to make all my own recursive plumbing. So immutability and pure-functions are like locking down a design or optimizing--it's not great if you're doing it prematurely. I think that's why starting with mutable stuff and then selectively immutable'ing things is popular. Come to think of it, something similar can be said about weak/strong typing. However the impact of having too-strong typing seems easier to handle with refactoring tools, versus the problem of being too-functional.
- Freedom2 1y agoDecades of blog posts, articles and books about "best practices" led to a suspicion of anything that wasn't bog standard OOP. This site in particular has also contributed to that, especially in the early 2000s, where you can easily find comments disparaging FP.
- jeffreygoesto 1y agoI think there's a deeper mechanism in these discussions. They all tend to go "If you do <insert_arbitrary_method_here> fully correct[0], it solves all of your problems." But [0] is never possible.
- josephg 1y ago> If you want a language where const is the default and mutable is a keyword, try F# for starters. I switched and never looked back. Rust is also like this (let x = 5; / let mut x = 5;). Or you can also use javascript, typescript and zig like this. Just default to declaring variables with const instead of let / var. Or swift, which has let (const) vs var (mutable). FP got there first, but you don't need to use F# to have variables default to constants. Just use almost any language newer than C++.
- bathtub365 1y agoAre there languages that automatically extend this to things like data structure members? One of the things I like about the C++ const keyword is that if you declare an instance of a struct/class as const it extends that to its members. If the instance isn’t const, you can still mutate them (as long as they aren’t declared const within the structure itself)
- orlp 1y agoRust works this way, yes. There are escape hatches though, which allow interior mutability.
- snalty 1y agoYeah, here's an example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=1c4609c5f0bca55613490f2a80d0c026 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- School-Cotton 1y agoIf I understand what you’re asking correctly, rust is also like this. If you have a non-mut value of (or non-mut reference to) an object, you only get non-mut access to its members.
- Fire-Dragon-DoL 1y agoIn typescript and js you get immutable references, but the data is mutable. Definitely not the same thing
- ehsankia 1y agofunctional programming has a lot of wonderful concepts, which are very interesting in theory, but in practice, the strictness of it edges on annoying and greatly hurts velocity. Python has a lot of functional-like patterns and constructs, but it's not a pure functional language. Similarly, Python these days allow you to adds as much type information as you want which can provide you a ton of static checks, but it's not forced you like other typed languages. If some random private function is too messy to annotate and not worth it, you can just skip it. I like the flexibility, since it leads to velocity and also just straight up more enjoyable.
- tasn 1y agoFunctional programming languages (almost always?) come with the baggage of foreign looking syntax. Additionally, imperative is easier in some situations, so having that escape hatch is great. I think that's why we're seeing a lot of what you're describing. E.g. with Rust you end up writing mostly functional code with a bit of imperative mixed in. Additional, most software is not pure (human input, disk, network, etc), so a pure first approach ends up being weird for many people. At least based on my experience.
- deleted 1y ago[deleted]
- rapind 1y ago> come with the baggage of foreign looking syntax Maybe they're right about the syntax too though? :)
- kragen 1y agoWhich one, Erlang, Lisp, or ML?
- nickpeterson 1y agoHonestly I find ML derived languages the most pleasant to look at.
- kragen 1y agoI prefer Piet, but it has some real drawbacks when it comes to readability: https://www.dangermouse.net/esoteric/piet/samples.html https://www.dangermouse.net/esoteric/piet/samples.html
- tmtvl 1y agoLisp syntax is objectively superior because you can write a predicate like thus: (< lower-bound x-coordinate upper-bound) Instead of having to do the awful ampersand dance.
- the__alchemist 1y agoI think it's because (I'm looking at Haskell in particular) there are a lot of great ideas implemented in them, but the purity makes writing practical or performant time-domain programs high friction. But you don't need both purity and the various tools they provide. You can use the tools without the pure-functions model. In particular: My brain, my computing hardware, and my problems I solve with computers all feel like a better match for time-domain-focused programming.
- cogman10 1y agoMy problem with functional languages is there never seems to be any easy way to start using them. Haskell is a great example here. Last time I tried to learn it, going on the IRC channel or looking up books it was nothing but a flood of "Oh, don't do that, that's not a good way to do things." It seemed like nothing was really settled and everything was just a little broken. I mean, Haskell has like what, 2, 3, 4? Major build systems and package repositories? It's a quagmire. Lisp is also a huge train wreck that way. One does not simply "learn lisp" There's like 20+ different lisp like languages. The one other thing I'd say is a problem that, especially for typed functional languages, they simply have too many capabilities and features which makes it hard to understand the whole language or how to fit it together. That isn't helped by the fact that some programmers love programming the type system rather than the language itself. Like, cool, my `SuperType` type alias can augment an integer or a record and knows how to add the string `two` to `one` to produce `three` but it's also an impossible to grok program crammed into 800 characters on one line.
- v9v 1y ago> Lisp is also a huge train wreck that way. [...] There's like 20+ different lisp like languages. Lisp is not a language, but a descriptor for a family of languages. Most Lisps are not functional in the modern sense either. Similarly, there are functional C-like languages, but not all C-likes are functional, and "learn c-likes" is vague the same way "learn lisp" is.
- School-Cotton 1y ago
- jandrewrogers 1y agoPure functional works great until it doesn't. For a lot of systems-y and performance-oriented code you need the escape hatches or you'll be in for a lot of pain and annoyance. As a practical observation, I think it was easier to close this gap by adding substantial functional capabilities to imperative languages than the other way around. Historically, functional language communities were much more precious about the purity of their functional-ness than imperative languages were about their imperative-ness.
- jancsika 1y agoHehe, purity is one helluva drug! For some reason, this makes me think of SVG's foreignObject tag that gives a well-defined way to add elements into an SVG document from an arbitrary XML namespace. Display a customer invoice in there, or maybe a Wayland protocol. The sky's the limit! On the other hand, HTML had so many loose SVG tags scattered around the web that browsers made a special case in the parser to cover them without needing a namespace. And we all know how that played out. Posted from an xhtml foreignObject on my SVGphone
- Fire-Dragon-DoL 1y agoUnfortunately unless there's an explicit way to state what has side effects like a mut keyword, a lot of fp programming advantages lose value because most devs default to mutable stuff, so the fp benefits don't compound
- black_knight 1y agoI think this works quite well with IO in Haskell. Most of my code is pure, but the parts which are really not, say OpenGL code, is all marked as such with IO in their type signatures. Also, the STM monad is the most carefree way of dealing with concurrency I have found.
- christophilus 1y agoF# isn’t purely functional, though, and strikes a nice balance. I just don’t really like the .NET ecosystem, being 100% Linux these days, as it always seems slightly off to me somehow.
- keeda 1y agoWe are still living with the hangover of C, which was designed for the resource-starved machines of eons ago, and whose style later languages felt they had to copy to get any kind of adoption. (And as you point out, that is how things turned out.) My bet is functional programming will become more and more prevalent as people figure out how to get AI-assisted coding to work reliably. For the very reasons you stated, functional principles make the code modular and easy to reason about, which works very well for LLMs. However, precisely because functional programming languages are less popular and hence under-represented in the training data, AI might not work well with them and they will probably continue to remain fringe.
- johnisgood 1y agoJust use OCaml in which you can mix imperative, functional, and OOP. I use all of them in a single codebase, whichever, wherever appropriate.
- charcircuit 1y agoConst by default is not functional programming.
- School-Cotton 1y agoIndeed, but it's one of the (many) good ideas from functional programming that have filtered into more mainstream languages.
- brrrrrm 1y agoone thing I've learned in my career is that escape hatches are one of the most important things in tools made for building other stuff. dropping down into the familiar or the simple or the dumb is so innately necessary in the building process. many things meant to be "pure" tend to also be restrictive in that regard.
- runevault 1y agoFunctional languages are not necessarily pure though. Actually outside Haskell don't most functional first languages include escape hatches? F# is the one I have the most experience with and it certainly does.
- gf000 1y agoFor what it's worth, Haskell has plenty of escape hatches itself as well.
- johncolanduoni 1y agoI’ve done a significant amount of functional programming (including F#) and still reach for it sometimes, but I don’t think it provides substantial advantages for most use-cases. Local mutability is often clearer and more maintainable. Also, category theorists think how excited people get about using the word monad but then most don’t learn any other similar patterns (except maybe functors) is super cringe. And I agree.
- otikik 1y agoDon't get too comfortable, history likes oscillation. If it becomes the norm then someone will have a "actually writing imperatively makes things go super fast".
- LogicHound 1y ago> It's funny how functional programming is slowly becoming the best practice for modern code (pure functions, no side-effects), I once mentioned both these concepts to a room of C# developers. Two of them were senior to me and it was a blank expression from pretty much everyone. > yet functional programming languages are still considered fringe tech for some reason. You can use the same concepts in non-functional programming languages without having to buy into all the other gumpf around functional programming languages. Also other programming languages have imported functional concepts either into the language itself or into the standard libraries. Past that. It is very rare to be able to get a job using them. The number of F# jobs I've seen advertised over the last decade, I could count on one hand.
- gf000 1y agoIt's because you want a tasteful mix of both. I believe Scala was pretty ahead here by building the language around local mutability with a general preference for immutable APIs, and I think this same philosophy shows up pretty strongly in Rust, aided by the borrow checker that sort of makes this locality compiler-enforced (also, interior mutability)
- dionian 1y agoWorth nothing that idiomatic scala uses constants by default, variables are discouraged and frankly rare.
- physicles 1y agoAgreed, and Carmack as always was ahead of the curve here. In case anyone here hasn’t seen it, here’s his famous essay, Functional Programming in C++: http://sevangelatos.com/john-carmack-on/ http://sevangelatos.com/john-carmack-on/ He addresses your point: > I do believe that there is real value in pursuing functional programming, but it would be irresponsible to exhort everyone to abandon their C++ compilers and start coding in Lisp, Haskell, or, to be blunt, any other fringe language. To the eternal chagrin of language designers, there are plenty of externalities that can overwhelm the benefits of a language, and game development has more than most fields.
- skeezyjefferson 1y ago> If you want a language where const is the default and mutable is a keyword whats the difference between const and mutable?
- Thorrez 1y ago"const" means something can't be changed. "mutable" means it can be changed. You don't need both a "const" keyword and a "mutable" keyword in a programming language. You only need 1 of the keywords, because the other can be the default. munchler is saying the "const" keyword shouldn't exist, and instead all variables should be constant by default, and we should have a "mutable" keyword to mark variables as mutable. As opposed to how C++ works today, where there is no "mutable" keyword, variables are mutable by default, and we use a "const" keyword to mark them constant.
- alcover 1y ago> you don't need both a "const" keyword and a "mutable" keyword What if the lang has pointers? How express read-only?
- skeezyjefferson 1y agocompiler flags with line and column number seems like the easiest way
- Thorrez 1y agoYou can make everything read-only by default, and if you need non-read-only, you use "mutable".
- alcover 1y agoYou need two keywords. One for assignability and one for writability : const ptr; // can't reassign, can't write-through (if r/o by default) const mut ptr; // can write
- bmitc 1y agoI get your sentiment, but I side on it's infuriating that it's taken this long. Lol. F# is wonderful. It is the best general purpose language, in my opinion. I looked for F# jobs for years and never landed one and gave up. I write Rust now which has opened up the embedded world, and it's nice that Rust paid attention to F# and OCaml.
- danenania 1y agoIt’s because FP, great as it is, is most beneficial when 80/20’d. Trying to do it 100% pure makes everything take significantly longer to build and also makes performance difficult when you need it. It’s also hard to hire/onboard people who can grok it.
- themafia 1y agoI want a language that helps me write software. I do not need a language that's hellbent on expressing a particular ideology.