News for hackers. Stuff that matters.
Re:Rob Pike's Rules of Programming (1989) by smegel on Saturday September 16, 2017 @04:07PM (#15266472)
How is writing code that loops over a list simpler than calling:

things.Get("key")

?

Re:Rob Pike's Rules of Programming (1989) by khedoros1 on Saturday September 16, 2017 @04:26PM (#15266564)
Your "Get" method hides complexity. It could be that it loops over a list, or that it hashes to find the bin, then loops over the list of items in the bin, or any of other dozens of other possible implementations.

So the next question is "What's the implementation of the Get(key) method?" Or maybe "Why aren't you using your language's library methods to loop over that list?"

Re:Rob Pike's Rules of Programming (1989) by smegel on Saturday September 16, 2017 @05:13PM (#15266793)
Why are you even asking the question if profiling hasn't determined a performance problem in that bit of code?
Re:Rob Pike's Rules of Programming (1989) by khedoros1 on Saturday September 16, 2017 @07:45PM (#15267393)
I'm considering cases where looping over a list mean using a simpler algorithm than whatever's provided by "Get", applying rules #3+#4, after determining that "Get" is a bottleneck.
Re:Rob Pike's Rules of Programming (1989) by parenthephobia on Saturday September 16, 2017 @06:59PM (#15267217)
Is this really an explanation of why manually iterating over a list is simpler than just calling a method?

Get is simpler precisely because it hides the complexity.

> Or maybe "Why aren't you using your language's library methods to loop over that list?"

What makes those methods not subject to the "hiding complexity" objection?

Re:Rob Pike's Rules of Programming (1989) by khedoros1 on Saturday September 16, 2017 @07:43PM (#15267382)
It's an ill-considered stream of consciousness where I wasn't clearly separating "simplicity of interface" from "simplicity of implementation".

Part of what I was going for is that a clean interface might hide a complex implementation. If you've followed rule 1 and see that "Get" is a bottleneck, it makes sense to apply rules 3+4, and see if there isn't a simpler implementation than the one provided by "Get".

Re:Rob Pike's Rules of Programming (1989) by winterlight on Saturday September 16, 2017 @04:48PM (#15266668)
The interface is the same. For example, in C# you have an interface Collection, which offers a lookup method, and which both HashSet and Lists implement.

The point of the OP is that among different implementations for the same interface, choose the simplest one unless you have empirical evidence that compels you to do otherwise.

Re:Rob Pike's Rules of Programming (1989) by seanmcdirmid on Saturday September 16, 2017 @05:00PM (#15266728)
Arrays didn't implement interfaces in C# until the Linq release. But the main problem is that arrays have an interface st is much more complex than just ICollection, they have functionality you don't want to be used and that should be properly encapsulated.
Re:Rob Pike's Rules of Programming (1989) by smegel on Saturday September 16, 2017 @05:10PM (#15266778)
> which offers a lookup method, and which both HashSet and Lists implement

How does a List map values to key? How does it even represent them?

Re:Rob Pike's Rules of Programming (1989) by winterlight on Saturday September 16, 2017 @05:45PM (#15266917)
The lookup method just checks whether an element exists in a collection or not. List and HashSet implement non-indexable collections, which have no notion of key. The Collection interface is very thin, and it's mostly used when you need to keep track of a set of elements. E.g. storing the nodes already visited in a dfs.
Re:Rob Pike's Rules of Programming (1989) by smegel on Saturday September 16, 2017 @07:30PM (#15267326)
OK I know more about C# now than I ever planned on knowing.

The problem with OP comment is that KISS and premature optimization are not diametrically opposed. Thet are two separate principles that mean different things.

Premature optimization is bad, but not because it necessarily violates KISS. Similarly, many people overcomplicate code for reasons nothing to do with optimization.

His argument reminds me of people who argue against free speech generally because we already ban people shouting fire in a cinema.

Re:Rob Pike's Rules of Programming (1989) by coldtea on Saturday September 16, 2017 @09:41PM (#15267782)
As tuples? You can very easily traverse the list, check for the key in the 0 index of the tuple, and get the value on the 1 index.

Not efficient, but very much possible.

Re:Rob Pike's Rules of Programming (1989) by rdiddly on Saturday September 16, 2017 @04:54PM (#15266705)
In terms of programmer time/effort, using .Get() is simpler.

In terms of program speed/optimization, the answer depends on how .Get() is implemented.

Re:Rob Pike's Rules of Programming (1989) by smegel on Saturday September 16, 2017 @05:15PM (#15266801)
Writing simple programs that meets requirements should be your goal.

Premature optimization is the root of all evil.

Re:Rob Pike's Rules of Programming (1989) by rdiddly on Saturday September 16, 2017 @07:34PM (#15267343)
Simplicity is great when it's not borne of ignorance and when it doesn't over-simplify. Some things are unavoidably complex. Even the word "simple" is complex, and has many meanings. What if simple is defined as...

- relies on simple "Programming 101" concepts

- doesn't require using or learning a framework or library

- corollary to above, doesn't require ascertaining that said framework or library implements a .Get() method

- doesn't rely on anyone else's code and has no dependencies or references

- doesn't require understanding ancillary concepts that might be present such as hash tables

Then writing the loop is "simpler." To answer your original question.

And just to turn things completely topsy-turvy, how about your .Get() method. If it's implemented the way such things are usually implemented, that means it uses hash tables and is very efficient. But that means it's a premature optimization, right, and therefore the devil? (Well we haven't defined "premature" either.)

The real answer is to think for yourself like a grown-up and don't rely on dogma. Every situation is different; tackle every problem by optimizing for what's important in that specific problem, using whatever resources and minding whatever constraints apply to that specific problem.

Re:Rob Pike's Rules of Programming (1989) by djur on Saturday September 16, 2017 @09:43PM (#15267799)
Selecting appropriate data structures and algorithms for a problem is not optimization. It's one of the basic skills and responsibilities of a programmer. That's why the bulk of the first volume of Knuth's _The Art of Computer Programming_ is about data structures.
Re:Rob Pike's Rules of Programming (1989) by wtetzner on Monday September 18, 2017 @09:25AM (#15275663)
Using the Set interface is probably the right thing. Then you profile, and if the set implementation you chose is too slow, you just swap it out for another implementation that implements the Set interface.
Re:Rob Pike's Rules of Programming (1989) by coldtea on Saturday September 16, 2017 @09:39PM (#15267779)
Who said you get to use a ready-made Get method / hash?