News for hackers. Stuff that matters.
Re:The Value in Go’s Simplicity by erokar on Saturday November 16, 2019 @09:25AM (#21552764)
While there is certainly something to be said for simplicity in a language, I think Go is too extreme in this regard. What it gains in simplicity it looses in expressiveness. This leads to more lines of code per feature, and bugs in software has been shown to be a function of LoCs [1,2,3].

One example of Go's lack of expressiveness is that loops are not abstracted away, e.g. you can't use higher level constructs like map and filter on collections. There are other languages that IMO strikes a better balance between simplicity and expressiveness, like Python and Elixir.

1. https://ieeexplore.ieee.org/document/1702967

2. https://link.springer.com/chapter/10.1007/978-1-4471-3003-1_...

3. https://www.amazon.com/gp/product/0735619670/ (Chapter "Developer Testing")

Re:The Value in Go’s Simplicity by dantillberg on Saturday November 16, 2019 @10:59AM (#21553248)
I ported a fairly complex (thousands of lines of code, in use in production) program from Python to Go.

The Python version is around 1/3-1/2 the size of the Go version, but the clarity with which bugs are visible in the Go version is astonishing.

There's a lot of code in Python version that "looks right" and even "does kind of the right thing a lot of the time." But the Go version lays bare corner and edge cases that were not obvious in the concise Python version.

I agree that Go usage does suffer sometimes from lack of generics. I've sometimes used code generation in its place, and it works, but it's a poor solution. At the same time, the lack of generics has helped me avoid over-abstracting in other places, which is a common bane of mine whenever I work in more "expressive" languages.

Re:The Value in Go’s Simplicity by jerf on Saturday November 16, 2019 @09:53AM (#21552894)
The results on code lines being directly related to bugs is getting old. Programming languages and practices have changed substantially in the past couple of decades. I'd want to see more recent evidence.

Besides, in practice I find lines of code in Go vs. Python are only maybe 20% different in many common cases, if you ignore the struct declarations which I have a hard time calling "code". (Being not a Turing complete language, that particular subset can't be considered vulnerable to the issues that Turing complete things can have.) I find myself very frequently wondering how many people complaining about all the lack of abstraction in Go have A: used it and B: used it to the point they're writing Go in Go. (Go just blows if you're trying to write Python in Go or something, or if you insist that you know better and must use map, filter, etc. rather than for loops. For all languages X, writing X in X is important, but Go is really graceless if you don't learn how.)

Now, if you do get to B, it is still true that Go does not quite abstract as "well" as some other languages, but it does abstract well enough that you can start having reasonably conversations about whether those other abstraction tools are actually paying their way in a lot of larger, shared code bases. If we throw out a ton of language complexity, but only pay a relatively small price, are they really carrying their weight? The answer will vary from code base to code base, but I think it's not necessarily some sort of horrid, unthinkably idea that not every program must be assaulted with the most powerful, complicated toolkit regardless of the price incurred by the complication.

Re:The Value in Go’s Simplicity by coldtea on Saturday November 16, 2019 @12:25PM (#21553735)
>The results on code lines being directly related to bugs is getting old. Programming languages and practices have changed substantially in the past couple of decades.

The results in those "past decades" used all kinds of languages, some far more advanced and modern than Go is today (e.g. Lisp, Smalltalk, Ada).

Re:The Value in Go’s Simplicity by _ph_ on Sunday November 17, 2019 @05:19AM (#21557298)
As someone who programs both in Lisp and in Go (and I have used a little bit of Smalltalk), I have to comment your statement.

In a sense, Lisp is the most advanced language ever designed, and still, it is ignored by most of the programming community :). Equally, I think, Smalltalk does object orientation better than most languages which followed it.

Both languages are mostly dynamically typed, so comparisons to static typed languages need to take this into respect, they are different beasts. On the other side, I think Go has an interesting mix out of static and dynamic typed features, having interfaces for static contracts and interface{} as a fully dynamic type, which still offers full type safety at runtime.

Go structs have also some interesting properties, if you look at them beyond being containers. Any type can have methods, and structs can embed other structs and thus "inhert" their methods in a sense. This is not full blown object orientation, but gets you surprisingly far.

Re:The Value in Go’s Simplicity by jerf on Sunday November 17, 2019 @05:57PM (#21560592)
If we're going to quote science, we need to use it scientifically. It may be the case that all the changes in the decades since those studies are irrelevant, but we really can't just assume that. Even if we assume that the results of those studies are even relevant to real programming in the first place, since IIRC they, like almost every other study ever done on programming, did all their analysis on completely toy problems.
Re:The Value in Go’s Simplicity by Un1corn on Saturday November 16, 2019 @09:41AM (#21552843)
I'm not sure I would have called Python simple, it's a great language and it's not as complicated as something like C++ but it's not simple.

For example when should I use NamedTuple and when should I use a dataclass? Should I be using a list comprehension or a map/filter/reduce? When should I split that list comprehension? I have seen (and made myself) terribly long lines with only list comprehension and it was hideous.

Re:The Value in Go’s Simplicity by erokar on Saturday November 16, 2019 @09:51AM (#21552880)
When I said Python was simple I was talking about the concepts, functions and datatypes in the core of the language. I agree that if you include all the modules that come with Python it is a "big" language.
Re:The Value in Go’s Simplicity by fernandotakai on Saturday November 16, 2019 @02:05PM (#21554310)
> Should I be using a list comprehension or a map/filter/reduce?

yes, use list comprehensions -- they are more readable.

>When should I split that list comprehension?

just use common sense. if you can read it and understand without thinking too much, that's good.

it's readability beats short code 100% of the time.

Re:The Value in Go’s Simplicity by Scarbutt on Saturday November 16, 2019 @10:37AM (#21553136)
You can write perfectly fine Python without any of the features you mentioned.
Re:The Value in Go’s Simplicity by makapuf on Saturday November 16, 2019 @11:07AM (#21553287)
Sure, but the more language that you may not use to write perfectly fine programs but have to know to know the whole language the more it seems superfluous. (See the whole drama over walrus operator). Python has accumulated a bit of rust also, old ways of doing things that were replaced with more modern ways, but that you should not use often. New languages or languages that don't evolve fast don't have them. python is becoming more and more complex (not tragically like c++) and v1.7 was indeed much simpler but it's still a nice language considering its age and scars.
Re:The Value in Go’s Simplicity by imode on Saturday November 16, 2019 @09:37AM (#21552820)
To add, here's a nice article on implementing map from scratch: https://blog.burntsushi.net/type-parametric-functions-golang...

It illustrates what you have to pay to get this kind of functionality for arbitrary slices.

I wonder how this would change with generics.

Re:The Value in Go’s Simplicity by alfalfasprout on Saturday November 16, 2019 @06:19PM (#21555555)
And yet introducing higher level functionality also leads to more difficult to detect bugs. I'm not sure the "lines of code:errors" correlation truly applies to all languages.

That said, anecdotally I too have found it much easier to find errors visually in go programs due to its simplicity. I find the same is true of C vs. C++.

Re:The Value in Go’s Simplicity by sagichmal on Saturday November 16, 2019 @11:30AM (#21553432)
> One example of Go's lack of expressiveness is that loops are not abstracted away, e.g. you can't use higher level constructs like map and filter on collections.

This is (or was) deliberate, with the rationale being that it makes O(n) blocks of code trivial to identify in review.

Whether or not you buy that argument is a separate thing, of course.

Re:The Value in Go’s Simplicity by h_r on Saturday November 16, 2019 @12:42PM (#21553840)
Can you explain why you think it makes O(n) blocks trivial to identify?

How is such identification made easier by manually writing out a `for` loop applying a function foo to each element rather than writing `map foo myCollection`?

Re:The Value in Go’s Simplicity by kjksf on Saturday November 16, 2019 @02:28PM (#21554426)
In every mainstream language I can think of `map foo myCollection` creates an intermediary map.

Memory allocation is so expensive that making that copy is often more expensive that calling `foo` on each element.

Sometimes making a copy is exactly what you need and there's no way around that cost (but hold that thought).

But I've also seen `sum map foo myCollection` so many times (especially in JavaScript).

Here you have a short, neat but also extremely wasteful way of doing things. I see it so frequently that I assume that many people are unaware of this cost (or completely disregard performance concerns).

If you were to write this imperatively, it would be obvious that you're making a copy and maybe you would stop and re-think your approach.

But there's more.

If you're paying attention to performance, an easy way to improve performance of `map foo myCollection` in e.g. Go is to pre-allocate the resulting array.

For large arrays, this avoid re-allocating of underlying memory over and over again, which is the best a `map` can do.

In imperative code those costs are more visible. When you have to type that code that does memory allocation, you suddenly realize that the equivalent of `map` is extremely wasteful.

Re:The Value in Go’s Simplicity by h_r on Sunday November 17, 2019 @12:38PM (#21558921)
I don't want to discuss this forever but I have a couple of comments:

Your points about efficiency are a separate topic entirely from the original claim that manually writing out an imperative solution makes it easier to see the algorithmic complexity. That was a surprising claim to me because, in my experience, if I understand what some HOF is doing, reading the code is even easier because there is less of it to wade through (and mental exhaustion doesn't make one easier to read vs the other).

> In every mainstream language I can think of `map foo myCollection` creates an intermediary map

You need to build up the final result, sure. Not an intermediary map but whatever structure (or functor) you're mapping over. That is the whole point of immutable data structures. Also when you're using persistent data structures, which all modern FP languages do, the cost of constructing the result can be far less than what you expect, especially if the result is lazy and you need only some of the results. There is a cost to immutability and if it's unbearable in some situation, fall back to in-place mutation but the semantics of these two approaches are definitely not the same.

> But I've also seen `sum map foo myCollection` so many times

Yeah... that should be a fold (reduce, whatever). :)

Re:The Value in Go’s Simplicity by LandR on Saturday November 16, 2019 @02:38PM (#21554478)
>> In every mainstream language I can think of `map foo myCollection` creates an intermediary map.

Language support for Transducers can fix this, you can compose functions like map / filter / reduce over a collection and only hit each item once.

Re:The Value in Go’s Simplicity by erik_seaberg on Saturday November 16, 2019 @07:25PM (#21555802)
Even Java can do this. You only pay for another Collection<T> if you actually #collect the elements of a Stream<T>.
Re:The Value in Go’s Simplicity by Gaelan on Saturday November 16, 2019 @07:29PM (#21555817)
Is Rust mainstream? If so, this code:

    numbers.iter().map(|n| { ... }).sum();
Compiles to a plain loop, with no allocation.
Re:The Value in Go’s Simplicity by steveklabnik on Saturday November 16, 2019 @08:52PM (#21556078)
(You want for_each not map, by the way)
Re:The Value in Go’s Simplicity by Gaelan on Sunday November 17, 2019 @01:15AM (#21556774)
Wait, why? AFIAK for_each doesn't return another iterator, and I'm calling sum afterward.
Re:The Value in Go’s Simplicity by steveklabnik on Sunday November 17, 2019 @08:59AM (#21557926)
.... hacker news cut off the “sum” part, and so I didn’t see it. On my phone it just happened to clip it exactly long enough that it was still valid syntax, wow. Anyway, sorry and my bad!
Re:The Value in Go’s Simplicity by tfha on Saturday November 16, 2019 @01:20PM (#21554076)
The way many people read code, it's much easier to overlook three characters than it is to overlook three lines with indentation.
Re:The Value in Go’s Simplicity by h_r on Saturday November 16, 2019 @01:42PM (#21554209)
That's quite an interesting, if frightening, take on it. Assuming a developer knows what `map` does, then I wouldn't have very much confidence in their reading comprehension if they somehow mentally skipped over the main higher order function being called on a three word line of code.

Would anyone expect such a developer to read and parse several more lines of code more reliably, in order to understand the algorithmic complexity? Seems unwarranted to me...

Re:The Value in Go’s Simplicity by tfha on Saturday November 16, 2019 @01:53PM (#21554247)
Humans make mistakes. People code review after exhausting days or late at night. Or sometimes they skim more than they intended to. You want to make problems in the code obvious everywhere that you can.

In programming, if you miss an important detail the repercussions can be high. In a million line codebase every idiom that's slightly more complex than it needs to be is going to result in dozens of additional bugs, sheerly because of the increased surface area for making mistakes with that idiom.

Re:The Value in Go’s Simplicity by h_r on Saturday November 16, 2019 @02:34PM (#21554452)
I agree with everything you've said here but being mentally exhausted doesn't make it more likely that you can read even more lines of code in a more reliable fashion.

I think the clue to your thinking, for me, is in your description of the `map` HOF as "slightly more complex". Having years of experience with both paradigms, I've found that grokking a call to one of these fundamental building blocks (map, filter, reduce, fold, etc) is nearly instantaneous. We've all experienced reading prose where the author was excessively verbose when the same point could have been made succinctly. It feels the same way reading for loops once you get over the learning curve of these very basic functional constructs. You have to keep repeating that boilerplate endlessly and it's very tedious to keep writing and reading it.

Re:The Value in Go’s Simplicity by ernst_klim on Saturday November 16, 2019 @02:54PM (#21554558)
> Humans make mistakes.

Yeah, and it's much easier to make mistake with a loop than with a map or filter.

Re:The Value in Go’s Simplicity by coldtea on Saturday November 16, 2019 @12:27PM (#21553746)
>This is (or was) deliberate, with the rationale being that it makes O(n) blocks of code trivial to identify in review.

Whereas, greping for .map(), .filter(), .reduce() wouldn't?

Re:The Value in Go’s Simplicity by azth on Saturday November 16, 2019 @08:02PM (#21555907)
> This is (or was) deliberate, with the rationale being that it makes O(n) blocks of code trivial to identify in review.

Like how

    for (int i = 0; i < strlen(str); i++) { ... }
is O(n)?
Re:The Value in Go’s Simplicity by coldtea on Saturday November 16, 2019 @08:12PM (#21555937)
Let's give them the benefit of the doubt, and consider that they meant that:

  for (int i = 0; i < strlen(str); i++) { ... }
could be grepped and easily discerned from:

  for (int i = 0; i < str_length; i++) { ... }
and generally, that every looping point could be easily identified.

After all, if you call functions inside the body, all bets are off as to what complexity the loop means anyway...

Re:The Value in Go’s Simplicity by temac on Saturday November 16, 2019 @05:18PM (#21555315)
So how does it work exactly? Are functions forbidden?
Re:The Value in Go’s Simplicity by sagichmal on Sunday November 17, 2019 @09:08AM (#21557946)
Functions (and methods) are the only way to "hide" computation and allocation in Go. That rubric makes reading Go code to build mechanical sympathy relatively straightforward.
Re:The Value in Go’s Simplicity by vhakulinen on Saturday November 16, 2019 @09:31AM (#21552798)
> What it gains in simplicity it looses in expressiveness. This leads to more lines of code per feature, and bugs in software has been shown to be a function of LoCs

If the lack of expressiveness equals to greater amount of code and thus more bugs (in Go), does the same apply for languages that are more verbose (say, Java)? And if language is more expressive and less verbose (say, Python or Javascript maybe?), does it mean less bugs?

Re:The Value in Go’s Simplicity by fluffything on Saturday November 16, 2019 @09:37AM (#21552819)
The OPs claim that it has been shown that bugs are a function of LoCs is unsubstantiated at best.

The number of bugs in a program correlates with the number of LoCs, but that's about it (correlation does not imply causation).

The claim is also quite absurd, since, for example, you can pre-process any C or C++ program ever written into a single line of code, yet this operation doesn't reduce the number of bugs in these programs, therefore the amount of bugs is not "just" a function of the amount of LoC.

Languages with better abstraction capabilities than Go (e.g. Rust or Python), might require programmers to type less, but the resulting lines of code are often dense. For example, I'm quite comfortable with Rust iterators and Python lists comprehensions, but it still takes me much longer to parse what code using these abstraction does, than code that just uses a simple for loop. It definitely feels great to cram a 10 LoC loop into a 1 LoC list comprehension in python, but reading that code 6 months later never felt that great, even for code that I've written myself. I just need to stop at those lines for much longer.

Re:The Value in Go’s Simplicity by pcwalton on Saturday November 16, 2019 @01:17PM (#21554059)
Really? For loops are a nightmare. Here's one from SPEC 2006:

    for (dd=d[k=0]; k<16; dd=d[++k])
    {
        satd += (dd < 0 ? -dd : dd);
    }
The problem with for loops is that they are too flexible, and so there's more opportunity for misuse. Iterator functions are less flexible, and therefore easier to read.

And this excessive flexibility doesn't even make simple things simple. Try counting down from N to 0 inclusive using unsigned ints.

Re:The Value in Go’s Simplicity by fluffything on Tuesday November 19, 2019 @08:19AM (#21572566)
Can you solve the exact same problem using Python list comprehensions or Rust iterators?

I'd like to see how solving this same problem in those languages is less of a nightmare.

I personally find that C loop quite readable compared with the same iteration being done in Rust.

Re:The Value in Go’s Simplicity by pcwalton on Tuesday November 19, 2019 @03:04PM (#21577638)
This is a sum of absolute differences, so in Rust:

    let satd = d.iter().cloned().map(abs).sum();
As an added bonus, there's no undefined behavior!
Re:The Value in Go’s Simplicity by tfha on Saturday November 16, 2019 @01:21PM (#21554083)
Loops in go are much less flexible than loops in C, you'd never see code like that in go.
Re:The Value in Go’s Simplicity by pcwalton on Saturday November 16, 2019 @01:28PM (#21554121)
In Go that loop could be:

    for k, dd = 0, d[0]; k < 16; k, dd = k + 1, d[k + 1] {
        ...
    }
I don't find that significantly more readable than the C version.
Re:The Value in Go’s Simplicity by bfrog on Saturday November 16, 2019 @06:22PM (#21555563)
Yeah a quick glance and I have no clue whats going on, tl;dr in review for sure
Re:The Value in Go’s Simplicity by coldtea on Saturday November 16, 2019 @12:30PM (#21553775)
>The number of bugs in a program correlates with the number of LoCs, but that's about it (correlation does not imply causation).

No, but it suggests it, and in this case, there's not many other options that could explain this than causation.

>The claim is also quite absurd, since, for example, you can pre-process any C or C++ program ever written into a single line of code, yet this operation doesn't reduce the number of bugs in these programs, therefore the amount of bugs is not "just" a function of the amount of LoC

That's an irrelevant strawman. When people speak of LoC they mean as to how many LoC are needed for various tasks based on the expressiveness of the language. Not whatever old LoC based on arbitrary adding needless lines (e.g. by preprocessing to inline function calls)...

Re:The Value in Go’s Simplicity by littlestymaar on Sunday November 17, 2019 @04:22AM (#21557177)
> The OPs claim that it has been shown that bugs are a function of LoCs is unsubstantiated at best.

But when looking at OP:

> bugs in software has been shown to be a function of LoCs [1,2,3].

He provided three different sources! Maybe it's wrong, or maybe you don't believe it, maybe it doesn't fit with your beliefs, but you can't say the OP claim is unsubstantiated when he provided academic sources on the subject!

Re:The Value in Go’s Simplicity by erokar on Saturday November 16, 2019 @10:03AM (#21552949)
In general, yes. The more lines of code per feature, the more bugs per feature. Also, the guarantees you get from a typed language like Java does not seem to prevent bugs to a meaningful degree. Probably things like programming culture associated with a language is more important, like a strong preference for testing.

This is an interesting study on programming languages' effect on software quality: https://web.cs.ucdavis.edu/~filkov/papers/lang_github.pdf

Re:The Value in Go’s Simplicity by fauigerzigerk on Saturday November 16, 2019 @05:30PM (#21555367)
I wonder if everyone actually does initial testing and bug fixing after committing, because otherwise any effect of programming language choice might be gone before the code reaches the commit logs.

I'm not sure it makes sense to consider code correctness separately from productivity. You can probably get code written in any language to roughly similar levels of correctness. But how much work does it take to get there? That's what I'm really interested in.

Re:The Value in Go’s Simplicity by eternalban on Saturday November 16, 2019 @09:58AM (#21552918)