310
top 50 comments
sorted by: hot top controversial new old
[-] sukhmel@programming.dev 66 points 7 months ago
[-] 9point6@lemmy.world 53 points 7 months ago

Const everything by default

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

[-] noli@programming.dev 45 points 7 months ago

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/...

[-] 9point6@lemmy.world 3 points 7 months ago

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)

[-] Corbin@programming.dev 3 points 7 months ago

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.

[-] sukhmel@programming.dev 13 points 7 months ago

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

[-] 9point6@lemmy.world 5 points 7 months ago

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

[-] ByGourou@sh.itjust.works 12 points 7 months ago

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

[-] 9point6@lemmy.world 11 points 7 months ago

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)

load more comments (2 replies)
[-] Magnetar@feddit.de 4 points 7 months ago

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

[-] crispy_kilt@feddit.de 5 points 7 months ago

Scala? Can we reimplement it in Rust?

[-] lemmesay@discuss.tchncs.de 4 points 7 months ago

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

[-] Magnetar@feddit.de 2 points 7 months ago

But does it do everything in anonymous functions and lambdas?

[-] crispy_kilt@feddit.de 2 points 7 months ago
[-] RageAgainstTheRich@lemmy.world 4 points 7 months ago* (last edited 7 months ago)

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.

[-] frezik@midwest.social 4 points 7 months ago

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

[-] RageAgainstTheRich@lemmy.world 1 points 7 months ago

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

[-] frezik@midwest.social 5 points 7 months ago

It's only a const within a function. You can pass the value to another function and changing it as it's passed. For example:

const int foo = 1
other_func( foo + 1)

In functional programming, you tend to keep track of state on the stack like this.

[-] madcaesar@lemmy.world 1 points 7 months ago

What is the advantage of this VS just overwriting the var?

[-] frezik@midwest.social 7 points 7 months ago

Keeping state managed. The data for the function will be very predictable. This is especially important when it comes to multithreading. You can't have a race condition where two things update the same data when they never update it that way at all.

[-] madcaesar@lemmy.world 1 points 7 months ago

Hm I'm having trouble visualizing this do you know a quick little example to illustrate this?

[-] frezik@midwest.social 1 points 7 months ago

Rather than me coming up with an elaborate and contrived example, I suggest giving a language like Elixir a try. It tends to force you into thinking in terms of immutability. Bit of a learning curve if you're not used to it, but it just takes practice.

load more comments (3 replies)
load more comments (1 replies)
[-] gerryflap@feddit.nl 19 points 7 months ago

Ngl, it'd solve a lot of bugs

[-] xmunk@sh.itjust.works 17 points 7 months ago

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

[-] Omega_Haxors@lemmy.ml 14 points 7 months ago* (last edited 7 months ago)

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.

[-] loxdogs@lemmy.world 11 points 7 months ago

can someone explain please?

[-] noli@programming.dev 30 points 7 months ago

In functional programming, everything is seen as a mathematical function, which means for a given input there is a given output and there can be no side effects. Changing a variable's value is considered a side effect and is thus not possible in pure functional programming. To work around this, you typically see a lot of recursive and higher order functions.

Declaring all values as const values is something you would do if you're a diehard functional programmer, as you won't mutate any values anyway.

[-] loxdogs@lemmy.world 4 points 7 months ago

thanks, kinda understand

[-] Mir@programming.dev 2 points 7 months ago

What is the best practice then when you want to update a variable's value?

[-] noli@programming.dev 10 points 7 months ago

Depends on how deep down the rabbit hole you want to go :p

  • creating a new variable that contains the updated value
  • recursion (e.g. it's not possible to make a loop that increments i by 1, but it is possible to turn that loop into a function which calls itself with i+1 as argument)
  • avoiding typical types of operations that would update variable values. For example instead of a for loop that updates every element of a list, a functional programmer will use the map function, which takes a list and a function to apply to each element of that list to create an updated list. There's several more of these very typical functions that are very powerful once you get used to using them.
  • monads (I'm not even gonna try to explain them as I hardly grasp them myself)
[-] DaforLynx@lemmy.zip 4 points 7 months ago

You just dropped a mind bomb on me. Suddenly things make sense :o

[-] shield_gengar@sh.itjust.works 3 points 7 months ago

A monad is just a monoid in the category of endofunctors, what's the problem?

[-] Nomecks@lemmy.ca 1 points 7 months ago

j = i + something

[-] ornery_chemist@mander.xyz 10 points 7 months ago

you mean let.

and then letting Hindley-Milner do the rest

[-] Magnetar@feddit.de 14 points 7 months ago

let there = "light"

[-] lemmesay@discuss.tchncs.de 9 points 7 months ago

I oscillate between using more functional paradigms and more object-oriented ones. is that normal?
I use a linter BTW(TypeScript) if that is a useful info.

[-] jkrtn@lemmy.ml 9 points 7 months ago

I think using both is normal. Closures and objects are duals of each other. Do whatever is understandable and maintainable, neither paradigm is magic.

[-] lemmesay@discuss.tchncs.de 4 points 7 months ago

that's a nice way to look at it. thanks!

load more comments (4 replies)
[-] crispy_kilt@feddit.de 4 points 7 months ago

Avoid shared mutable state like the plague in any paradigm and you'll be fine

[-] lemmesay@discuss.tchncs.de 2 points 7 months ago

state management crying in the corner

[-] crispy_kilt@feddit.de 2 points 7 months ago

Functional state management is fine

[-] ZILtoid1991@lemmy.world 3 points 7 months ago

I also do that. Very simple stuff, especially of those that are easy to optimize for the compiler, are often very close to functional programming paradigms.

[-] DickFiasco@lemm.ee 2 points 7 months ago

I use a combination of both. Objects are declared const, all members are set in the constructor, all methods are const. It doesn't really work for some types of programs (e.g. GUIs) but for stuff like number crunching it's great.

[-] lemmesay@discuss.tchncs.de 2 points 7 months ago

I heavily use classes while working on back end, and when I'm making a really self-contained logic, such as a logger or an image manipulation service.
but since most frontend stuff heavily leans on functional side, I go with it

[-] crispy_kilt@feddit.de 4 points 7 months ago

Needs more monads

[-] LinearArray@programming.dev 3 points 7 months ago
[-] uis@lemm.ee 1 points 7 months ago
load more comments
view more: next ›
this post was submitted on 21 Mar 2024
310 points (100.0% liked)

Programmer Humor

19503 readers
475 users here now

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

founded 1 year ago
MODERATORS