3 ms·
>> But, really, is that so much worse than this? names = [m.Name for m in machines] You can take that code and interpret that as database query, like C# LI
by dominotw 11y ago
>> But, really, is that so much worse than this?
names = [m.Name for m in machines]
You can take that code and interpret that as database query, like C# LINQ.
you frist example is 'how' vs 'what' of second example. Once you stop telling the computer how to do things and just tell it what you want all kinds of things become possible.
- NateDad 11y agoBut then you have no idea what the computer is actually doing. There's a big performance difference between an in-memory loop and querying a database. This is one of the things I like about Go... what the computer actually does in response to any random line of code is pretty obvious (except for function calls, which of course can do anything). When you hide away the loop inside a map statement, you get people doing dumb things like this: names = [m.Name for m in machines] ids = [m.ID for m in machines] addrs = [m.Address for m in machines] So now we're iterating of over the list of machines 3 times... or making 3 database queries or whatever.
- dominotw 11y ago>But then you have no idea what the computer is actually doing. You trust it the same way you trust go compiler to do the right thing when you give it code to compile.
- NateDad 11y agoThat's the nice thing about Go code... I almost always know exactly what it'll make the computer do. I know how much memory will get allocated, what the likely CPU usage is going to be, etc. The abstraction between the code and the computer is low, which helps lets a lot in understanding why your code is slow, or why it's producing a lot of garbage. "oh hey, here's a loop in a loop... oops, N^2 time".
- prodigal_erik 11y agoWhat the computer actually does is dramatically different between reading a register and reading a block of a mapped file, but I value concisely expressing the intent rather than elaborately repeating mechanics that might change (e.g., I could optimize machines.__iter__ without rewriting all the statements).
- sythe2o0 11y agoIt's worth noting you'd probably want to write this out in Go as follows: names := make([]string, len(machines)) for i, m := range machines { names[i] = m.Name } append() is pretty costly, and should be avoided when possible for performance. This is actually relevant to your how vs what concern-- on the one hand, we could trust the compiler and the builtins to always take the most efficient approach, and only program 'what' we want. That could avoid something like using append in a loop where you know the length of your intended output slice. On the other hand, if there are edge cases which the built-ins take into account that we don't care about and want to ignore, being able to and having the intention of programming 'how' can have performance benefits. I like writing 'how' because I get to benchmark and test different approaches to solve the same problem, whereas 'what' might take a usually-fast approach that isn't a good solution in our specific case.
- NateDad 11y agoYeah, true. And in production, that's what I do. Append is for when you don't know how long a slice will end up being.