this post was submitted on 21 Mar 2024
296 points (94.8% liked)

Programmer Humor

19725 readers
57 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
 
top 50 comments
sorted by: hot top controversial new old
[–] sukhmel 67 points 8 months ago
[–] [email protected] 42 points 8 months ago (6 children)

Const everything by default

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

[–] noli 45 points 8 months ago (2 children)

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

load more comments (2 replies)
[–] sukhmel 13 points 8 months ago (1 children)

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

[–] [email protected] 4 points 8 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

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

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

[–] [email protected] 4 points 8 months ago (2 children)

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)
[–] [email protected] 4 points 8 months ago (1 children)

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

[–] [email protected] 5 points 8 months ago (2 children)

Scala? Can we reimplement it in Rust?

[–] [email protected] 4 points 8 months ago

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

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

But does it do everything in anonymous functions and lambdas?

[–] [email protected] 2 points 8 months ago
[–] [email protected] 3 points 8 months ago* (last edited 8 months ago) (11 children)

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.

load more comments (11 replies)
[–] [email protected] 20 points 8 months ago (1 children)

Ngl, it'd solve a lot of bugs

[–] [email protected] 16 points 8 months ago

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

[–] [email protected] 14 points 8 months ago* (last edited 8 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.

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

can someone explain please?

[–] noli 32 points 8 months ago (2 children)

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.

[–] [email protected] 4 points 8 months ago

thanks, kinda understand

[–] Mir 2 points 8 months ago (2 children)

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

[–] noli 10 points 8 months ago (2 children)

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)
[–] [email protected] 4 points 8 months ago

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

[–] [email protected] 3 points 8 months ago

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

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

j = i + something

[–] [email protected] 9 points 8 months ago (4 children)

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.

[–] [email protected] 9 points 8 months ago (5 children)

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

[–] [email protected] 4 points 8 months ago

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

load more comments (4 replies)
[–] [email protected] 3 points 8 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.

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

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

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

state management crying in the corner

[–] [email protected] 2 points 8 months ago

Functional state management is fine

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

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.

[–] [email protected] 2 points 8 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

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

you mean let.

and then letting Hindley-Milner do the rest

[–] [email protected] 14 points 8 months ago

let there = "light"

[–] [email protected] 3 points 8 months ago

Needs more monads

[–] LinearArray 2 points 8 months ago
load more comments
view more: next ›