Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
JawnV6
Jul 4, 2004

So hot ...
gee id hate to use a machine with this "global mutable state"

Adbot
ADBOT LOVES YOU

FAT32 SHAMER
Aug 16, 2012



comedyblissoption posted:

The primary reason code in Java/C#/C++/javascript/whatever devolves into spaghetti is unnecessary pervasive use of global mutable state. Pervasive use of global mutable state defeats localized understanding of code. To understand code in the context of global mutable state requires looking at how that state is used everywhere. To modify code correctly in the context of global mutable state, you often must maintain implicit global invariants. Even though function Foo might not have global mutable state under the hood now, in the future it silently may.

Java/whatever programmers will create contorted buzzword constructs so they can kid themselves they do not suffer from global mutable state defeating localized understanding. A current example of this delusion is the usage of dependency injection frameworks to put a curtain around global mutable state. Pervasive unnecessary global mutable state is idiomatic in all of the industry C#/C++/Java/javascript codebases I have worked on.

Mutability of variables and parameters is fine as long as you can notate the scope of mutability. Java/C# do not allow you to do this meaningfully for function parameters. Notating and enforcing mutability scope is important to productively maintain invariants without programmer error.

see my imposter syndrome goes off real hard when i read really insightful posts like this that i 1) never would have been able to put to words 2) had to google half of the terms to refresh myself from sophomore year of my bs in cs

Shaggar
Apr 26, 2006
its not insightful its a load of bullshit. if your application doesn't have state its not an application its a math toy. state is hard to do right, but you have to do it.

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

Shaggar posted:

its not insightful its a load of bullshit. if your application doesn't have state its not an application its a math toy. state is hard to do right, but you have to do it.

you're confusing side effects with state. but the only thing they have in common is that the FP crowd rails against both.

an app must necessarily have side effects to do anything useful. it doesn't necessarily need internal state. a good backend application will put all its state out of the application code (in the storage, where filesystem and databases take care of hard problems like consistency and atomicity), and then treat each command as if it were brand new and freshly started

Zemyla
Aug 6, 2008

I'll take her off your hands. Pleasure doing business with you!

Shaggar posted:

its not insightful its a load of bullshit. if your application doesn't have state its not an application its a math toy. state is hard to do right, but you have to do it.

comedyblissoption posted:

The primary reason code in Java/C#/C++/javascript/whatever devolves into spaghetti is unnecessary pervasive use of global mutable state.

The way to do state right is to have the majority of your functions transform immutable data into immutable data, then have clearly-annotated operations manipulate the global state and handle interaction using these functions. Ideally, you express the difference between "function" and "operation" using some method that means you can't accidentally have an operation take place inside a function, such as through the type system.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

NihilCredo posted:

you're confusing side effects with state. but the only thing they have in common is that the FP crowd rails against both.

an app must necessarily have side effects to do anything useful. it doesn't necessarily need internal state. a good backend application will put all its state out of the application code (in the storage, where filesystem and databases take care of hard problems like consistency and atomicity), and then treat each command as if it were brand new and freshly started

cgi was...right?

comedyblissoption
Mar 15, 2006

Zemyla posted:

The way to do state right is to have the majority of your functions transform immutable data into immutable data, then have clearly-annotated operations manipulate the global state and handle interaction using these functions. Ideally, you express the difference between "function" and "operation" using some method that means you can't accidentally have an operation take place inside a function, such as through the type system.
The future of static type systems will probably have some way to notate side effects in a way comprehensible to most programmers.

Instead of making everything immutable, Rust has an interesting approach to limiting mutability to aid program understanding. In Rust, you must notate scope of mutability and immutability. Rust also enforces that data is not mutably aliased. The default is for immutability. This approach combines the performance benefits of mutability with the localized understanding of immutability. Since all mutability is explicit, you can treat a mutable function parameter as just an input->output relationship of the function. Here's a cool blog post talking about this: https://manishearth.github.io/blog/2015/05/17/the-problem-with-shared-mutability/

Rust does have a way to put in global mutable state for an application, but abusing this system is unidiomatic.

Lutha Mahtin
Oct 10, 2010

Your brokebrain sin is absolved...go and shitpost no more!

comedyblissoption posted:

Pervasive unnecessary global mutable state is idiomatic in all of the industry C#/C++/Java/javascript codebases I have worked on.

you could call it......idiotmatic :xd:

HoboMan
Nov 4, 2010

is there a way to get vs code to automagically hide all the files specified in my .gitignore?

comedyblissoption
Mar 15, 2006

Lutha Mahtin posted:

you could call it......idiotmatic :xd:
Just to elaborate on what I mean by this:

Instead of defining a function that just trivially takes in an A and returns a B, the industry standard is to instead:
Write a function that takes in an A. Read and mutate important global variable G. Also return a B. Also maybe mutate A. Repeat for all your functions dealing with this area until you need to understand how all the functions interact together in a grand spaghetti before you can usefully touch any function.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

i wonder if haskell nerds have hard drives...

comedyblissoption
Mar 15, 2006

idk I think it's important to try to define what causes spaghetti code in concrete terms so you can avoid creating spaghetti code yourself

Zlodo
Nov 25, 2006
most of the spaghetti c++ code I've seen recently was because of fuckers who like to reuse code by massively cut and pasting everything and not that much because of mutability (for instance I was recently able to take a giant disgusting function full of repetitive copy pasted crap and move it almost unchanged into a multithreaded job because as disgusting as it was it wasn't that hard to determine what it touched)

Night Shade
Jan 13, 2013

Old School

comedyblissoption posted:

In Rust, you must notate scope of mutability and immutability... The default is for immutability.

Same sort of thing with F#, though being part of the .net runtime means you can abuse reflection to mutate things the compiler considers immutable. But that's where code review comes in.

ML-flavoured languages lend themselves far more to "function returns new state computed from passed args" style programming than C-style languages do imo.

Brain Candy
May 18, 2006

carry on then posted:

i wonder if haskell nerds have hard drives...

imagine if you did a thing and it couldn't do anything that it didn't promise it would do. imagine if that was was how things worked by default

imagine if you wrote a browser doing things this way because you were tired of being stabbed in the foot because you checked whether the lights were on. imagine you were tired of programming around optimizations that made sense forty years ago, but are deoptizations today. so you threw c++ in the trash, where it belongs

you could call it firefox quantum or something

Zlodo
Nov 25, 2006
imagine if you had to ship a product this year

Xarn
Jun 26, 2015

carry on then posted:

i wonder if haskell nerds have hard drives...

HDDs are for the dirty mutable peasants to deal with

Night Shade
Jan 13, 2013

Old School
c tp s: assumptions about fifo ordering of messages break down in the face of "oh, an exception? just put that message on the back of the queue and deal with it later"

Zemyla
Aug 6, 2008

I'll take her off your hands. Pleasure doing business with you!

carry on then posted:

i wonder if haskell nerds have hard drives...

Haskell actually has several methods of managing mutability:


The first two aren't strictly necessary - any function that could be calculated with ST can be calculated without ST, with at most an O(log n) slowdown; and it's rare that a program would want a plain old IORef instead of an MVar or full STM. The other two are primarily for communicating between threads, which does require mutability (but if you've got threads, you're already doing IO).

Brain Candy
May 18, 2006

Zlodo posted:

imagine if you had to ship a product this year

uh i do and i'm using immutable things because it's a technique you can use in any language?

you fool, you loving moron

Sapozhnik
Jan 2, 2005

Nap Ghost
java/c#/etc are good languages because brainlets can pick them up and start writing software and then you can over the course of several years tenderly beat them around the head with a shovel until they learn how to do it correctly i.e. use immutable and persistent data structures everywhere it makes sense to do so, which when you're writing database skins for http turns out to be most places.

CRIP EATIN BREAD
Jun 24, 2002

Hey stop worrying bout my acting bitch, and worry about your WACK ass music. In the mean time... Eat a hot bowl of Dicks! Ice T



Soiled Meat
by enforcing immutable objects everywhere in our codebase i've completely eliminated our lower level guys from doing really dumb poo poo like having models passed around that are sometimes completely filled and sometimes need to be "filled" later by calling another function.

Zlodo
Nov 25, 2006

Brain Candy posted:

uh i do and i'm using immutable things because it's a technique you can use in any language?

you fool, you loving moron

I was talking about the "throw c++ in the trash" part

but applying immutability everywhere is also a fantasy when you are performance constrained (console cpus are poo poo and you have 30ms to update the massive amount of disparate crap that have been piled up in your game to reach 30fps)
immutability is a useful abstraction but it is still an abstraction: the actual machine that exists in the real world is mutable. that abstraction have a cost that you can't always afford to pay everywhere in all circumstances

Shaggar
Apr 26, 2006

NihilCredo posted:

you're confusing side effects with state. but the only thing they have in common is that the FP crowd rails against both.

an app must necessarily have side effects to do anything useful. it doesn't necessarily need internal state. a good backend application will put all its state out of the application code (in the storage, where filesystem and databases take care of hard problems like consistency and atomicity), and then treat each command as if it were brand new and freshly started

state is the side effect. the entire point of your application is to change state.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Zemyla posted:

Haskell actually has several methods of managing mutability:


The first two aren't strictly necessary - any function that could be calculated with ST can be calculated without ST, with at most an O(log n) slowdown; and it's rare that a program would want a plain old IORef instead of an MVar or full STM. The other two are primarily for communicating between threads, which does require mutability (but if you've got threads, you're already doing IO).

cool. java also has ways to avoid global state spaghetti so stop comparing the worst java to the best of your favorite language then deciding that all java programmers must be children, thanks

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
you're supposed to compare the best of java to the worst of your programming language, then come to exactly the same conclusion

HoboMan
Nov 4, 2010

someone is handing off an android app to me that has apparently been written in groovy. how worried should i be?

cinci zoo sniper
Mar 15, 2013




HoboMan posted:

someone is handing off an android app to me that has apparently been written in groovy. how worried should i be?

a lot

Luigi Thirty
Apr 30, 2006

Emergency confection port.

HoboMan posted:

someone is handing off an android app to me that has apparently been written in groovy. how worried should i be?

you are already dead

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

HoboMan posted:

someone is handing off an android app to me that has apparently been written in groovy. how worried should i be?

:rip:

FAT32 SHAMER
Aug 16, 2012



HoboMan posted:

someone is handing off an android app to me that has apparently been written in groovy. how worried should i be?

prepare for a total refactor, my dude

Shaggar
Apr 26, 2006

HoboMan posted:

someone is handing off an android app to me that has apparently been written in groovy. how worried should i be?

try decompiling it to java

jony neuemonic
Nov 13, 2009

HoboMan posted:

someone is handing off an android app to me that has apparently been written in groovy. how worried should i be?

hahahaha

Arcsech
Aug 5, 2008

HoboMan posted:

someone is handing off an android app to me that has apparently been written in groovy. how worried should i be?

:sever:

FAT32 SHAMER
Aug 16, 2012



speaking of writing apps in meme languages, my coworker keeps trying to get us to write android apps in react native and my gut reaction is to run away screaming

how terrible is react native

Brain Candy
May 18, 2006

Zlodo posted:

but applying immutability everywhere is also a fantasy when you are performance constrained

it's actually loving fantastic when you have many beefy cores or lots of wimpy cores because you can use cool tricks like CAS instead of locks

quote:

immutability is a useful abstraction but it is still an abstraction:

i contend that mutability is instead an optimization? one that was almost always required in the past, but should be used only with cunning and skill now?

quote:

the actual machine that exists in the real world is mutable. that abstraction have a cost that you can't always afford to pay everywhere in all circumstances

even talking about bytes on an 'actual machine' is somewhat of a useful lie. the target of a programming language is primarly a human mind

bob dobbs is dead
Oct 8, 2017

I love peeps
Nap Ghost

HoboMan posted:

someone is handing off an android app to me that has apparently been written in groovy. how worried should i be?

you have no chance make your time

Sapozhnik
Jan 2, 2005

Nap Ghost

Luigi Thirty posted:

you are already dead

Brain Candy
May 18, 2006

JawnV6 posted:

gee id hate to use a machine with this "global mutable state"

i mean this misses the point on one level, but on another is totally correct?

you computer doesn't know poo poo about classes or structs or functions or whatever mental model you write to that lets you get the outcome you'd like

Adbot
ADBOT LOVES YOU

comedyblissoption
Mar 15, 2006

talking about just mutable or immutable persistent data structures is a false dichotomy

you can also have mutable data structures (for performance reasons) that you can make explicitly immutable in a scope. you can get most of the benefits of both worlds

  • Locked thread