News for hackers. Stuff that matters.
Re:From First Principles: Why Scala? by brabel on Thursday February 11, 2021 @01:14PM (#26105084)
I am fairly certain the jury is out on this question:

It's always better to write simple, almost dumb code that anyone can understand than to use advanced abstractions from category theory or whatever.

Why? Because every single study or anedocte I've ever heard is like yours: adding complex abstractions to code do NOT make it more reliable. But they do make it much harder to modify, understand, and fix.

FP tought us that unconstrained mutable state is very bad, and that has helped us immensely in the mundane languages of the world (Java, C#, C++, Go). But I think it's time to learn the other lesson from FP: overly complex abstractions are also very bad and should NOT be used willy nilly as they simply have a very low or negative cost-benefit.

Re:From First Principles: Why Scala? by simias on Thursday February 11, 2021 @03:28PM (#26106669)
Finding the right level of abstraction is basically the one and only skill that matters for a software engineer IMO. I've got about 15 years of experience in programming and it's still something I'm actively working on.

I disagree with the general take that "adding complex abstractions to code do NOT make it more reliable". Good use of abstraction makes code easier to write, easier to maintain and easier to extend. Good use of abstraction can make code more concise and more regular while at the same time allowing better diagnostics when you do something wrong.

As a quick example: a generic JSON serialization library that can work with any type is probably a lot nicer to use that one that requires manual reimplementation for every class in your program. It'll be more complicated to write but it's probably well worth it in the end. Similarly a logging system that can abstract over several backends and log levels depending on the environment probably beats having a bunch of if/else every time you want to log a message.

But one needs to remember the old saying: debugging code is harder than writing it, so if you write code that's as smart as you're capable of producing then by definition you're not smart enough to debug it.

Re:From First Principles: Why Scala? by brabel on Friday February 12, 2021 @02:41AM (#26111479)
You are completely right: finding "THE RIGHT" level of abstraction is extremely important. Without abstractions we would be writing code in Assembly.

This is the reason I used the term "overly complex abstractions", not just "abstractions" in general, of course. Overly complex meaning it's a failure to use "the right abstraction" where you instead go for something much more complex than the proble called for... this is what I understand most people in this thread are referring to when they refer to previous Scala projects they've worked on.

Re:From First Principles: Why Scala? by MikeDelta on Thursday February 11, 2021 @03:14PM (#26106514)
When I was young I though that good writers are the ones who write such complex sentences that nobody can understand. Later I learned that good writers are clear and understandable.

I used to write complex code because I found it elegant. Now I write simple code because I enjoy breaking down a complex problem into its simplest form, which for me means I fully understand the problem.

Re:From First Principles: Why Scala? by jgwil2 on Thursday February 11, 2021 @04:04PM (#26107147)
I think the writing metaphor is a good one; we are writing source code for other humans to read after all.
Re:From First Principles: Why Scala? by ta988 on Thursday February 11, 2021 @10:25PM (#26110446)
We are also writing code so we can read it ourselves in a few months or years and still understand what it does!
Re:From First Principles: Why Scala? by dkarl on Thursday February 11, 2021 @04:57PM (#26107928)
I agree 100%, and I always bring it back to one simple observation that has been true of almost all the projects I've worked on: the limiting resource that programmers work with is their own time and brainpower. To my mind, much of the discipline of programming is centered around making frugal use of that scarce resource.

I still think programmers should work with powerful tools, because "dumb" tools often force you to express things in convoluted ways, and you don't need a powerful language to make a huge mess. (Java proves that you only need single inheritance to produce virtually unlimited amounts of spaghettiness in real-world projects. The difference between Java and Scala is not that Scala enables teams to produce epic piles of FP crap, it's that we long ago stopped being shocked when teams produce epic piles of OO crap.)

I think the programmers doing terrible things with Scala would be doing terrible things in any language, given the chance. What they need is hands-on technical management. When they have a wild idea, they need to be walked through an appropriate engineering decision-making process. They need somebody with the authority to tell them, "Ha ha, yeah, that design with etcd and Kafka and the robot space lasers would friggin' rock, how cool would that be, but on the other hand, we could just stick a REST API in front of a database and you'd be done in two weeks, so let's compare pros and cons."

Re:From First Principles: Why Scala? by valenterry on Thursday February 11, 2021 @06:36PM (#26108988)
That's a common falsehood though.

It does not matter how "simple" each line of code is.

What matters is how "simple" the whole application/solution is.

You can argue that you can write very simple code in Assembler - all instructions are very clearly defined and very simple (well, maybe not anymore in CISC...). Someone can learn them all in a day. The problem is just that now you end up with a lot of code and overall that will be hard to maintain.

Compare it to Javascript: certainly not the language where code is most maintainable or simple, but the end-result is easier to maintain then assembler.

In the end, the choice of programming language is like a the choice of a compression-tool like gzip. The more complex, the more difficult to learn and use (setting library size and various compression settings) but the output will be smaller compared to a simple tool or no compression (Assembler) while containing the same amount of information.

Re:From First Principles: Why Scala? by dfgdghdf on Friday February 12, 2021 @04:45AM (#26112001)
Counter-point:

I write lots of F#, and I find that computation expressions (a fancy version of do-notation) makes my code more "dumb" and "simple", despite being an advanced "FP" feature. This is because it pushes the complexity from my business logic and into library code.

With computation expressions:

    async {
      let! foos = fetchFoos

      for foo in foos do
        do! launchFoo foo

      do! clearFoos
    }
Without computation expressions (this is probably wrong but you get the idea):

    Async.bind 
      fetchFoos
      (fun foos -> 
        Async.bind 
          (
            foos
            |> Seq.fold (Async.bind (fun foo -> launchFoo foo)) (Async.just ())
          )
          (fun () -> clearFoos)
      )
Notice how my fancy non-blocking code reads like straight-forward blocking code?

I could not manage complexity without this feature.

Re:From First Principles: Why Scala? by lmm on Thursday February 11, 2021 @08:39PM (#26109874)
You should always reach for the simplest solution first (and Haoyi has a great set of guidelines about that: https://www.lihaoyi.com/post/StrategicScalaStylePrincipleofL... ). The point of those complex FP abstractions is to let you keep writing dumb code even when you want to do something fiddly (such as async operations with error recovery). But don't cargo-cult the complex solution if you don't have the complex problem! A plain function whose result is a plain value should be expressed that way.
Re:From First Principles: Why Scala? by einpoklum on Thursday February 11, 2021 @02:30PM (#26105992)
> adding complex abstractions to code do NOT make it more reliable.

Sometimes false. If the (more) complex abstractions are in well-tested libraries that haven't been written solely for your code.

> But [complex abstractions] do make [your code] much harder to modify,

Not necessarily. You can separate concerns; you can isolate changes; and you hopefully reduce the amount of code you've written yourself, significantly even.

> [complex abstractions] do make [your code] much harder to understand

This is as likely false as it is true.

> [complex abstractions] do make [your code] much harder to fix

Again, this very much depends. If you're using a well-tested, widely-used external library with those abstractions, it may well be easier to fix your own code.

Re:From First Principles: Why Scala? by jjav on Thursday February 11, 2021 @06:00PM (#26108628)
> the other lesson from FP: overly complex abstractions are also very bad

Lisp, in its persistent failure to achieve world domination, has been teaching us this lesson for over 60 years now!

Infinitely powerful languages are fun and, yes, powerful. But as soon as the project has more than one or two people in it, they also result in runaway complexity that will hurt the product far more than any gains in expressibility.

Re:From First Principles: Why Scala? by keybored on Thursday February 11, 2021 @03:32PM (#26106721)
“Complex abstraction” is a contradiction in terms since an abstraction is a relation A -> B where B needs to be simpler than A.
Re:From First Principles: Why Scala? by jgwil2 on Thursday February 11, 2021 @04:12PM (#26107274)
From the client's point of view that may be true; abstraction should be the process of pushing complexity down. But in this case I think we're talking about code that has inadvertently introduced unnecessary complexity in an attempt to be hyper-generic.
Re:From First Principles: Why Scala? by aapppwe on Thursday February 11, 2021 @02:13PM (#26105812)
i think it depends if you need it, rust for example is quite complex, but it solves a class of issue that other language have trouble solving while maintaining speed