9 ms·
Code Smells: Iteration
- jnty 9y agoAt the risk of sounding arrogant, isn't this a bit obvious? To me, code smells are more about spotting when someone's been forced to do something a bit weird and convoluted. It's possibly the cleanest solution available given the immediate context, but this is actually a symptom (or "smell") that something is wrong with the wider design of the system. This article seems more about detecting choices around data structures which are bad in any context and have no real link to the overall system design.
- jrs95 9y agoIt's obvious to some of us, but not all of us. I see things way worse than this on a fairly regular basis.
- jnty 9y agoI guess - but I feel like you'd call it an "error" rather than a "smell". Better to just teach people data structures rather than which constructs they should be afraid of. Approaching it "sideways" like this just leads to people going "I heard that loops can be bad, so I copied and pasted that line 25 times instead".
- jayd16 9y agoIts a smell and not an error because it works perfectly fine and it actually is perfectly fine for now because the n for that linear search is low.
- rootlocus 9y agoSets have a very specific use case: they only contain unique values and (unless using a specific implementation) they don't have any defined order. Switching from arrays to sets just to get rid of an iteration doesn't sound like fixing a smell to me. Also, insertion, iteration and memory usage are less efficient for a set.
- JackFr 9y agoImportantly Sets have different semantics than Lists. Creating a set is an information losing process. A list converted to set cannot be recovered, but a set converted to a list is easily recovered. I don't think this is handled well in TFA. Because of some dubious iteration at the call site, the author changes the semantics of getLoadNames(), blithely assuming that duplicates should not be allowed and order is not important. The author mentions that the function is called at two other places. For all we know the original author was aware of Sets but chose List because it more correctly matched the use of the structure.
- CupOfJava 9y agoNaw man, code smells are anything that when you look at code and you wrinkle your face like you would if a cow farted right in front of it.
- chillydawg 9y agoIt's obvious to anyone with a CS (or similar) degree, but may not be to someone who is learning to code from various tutorials. Having hints like this in your IDE can greatly improve the quality of your code and help you learn.
- mikeash 9y agoExactly, this is pretty clearly aimed at beginner/intermediate programmers who haven't entirely figured out how to decide when to use List versus Set versus Map. It's probably obvious to the typical HN reader, but valuable for the target audience. Might not belong here, although the fact that it's currently at #3 on the front page suggests there's enough interest in it.
- deleted 9y ago[deleted]
- dionian 9y agoPeople with CS degree and decades of real world experience still benefit from automated code analyses. We deal with systems with millions of lines of code...
- koolba 9y agoThat was my take as well. It's like seeing code with the same constant inlined in multiple places rather than centralized: // Use some inlined string as a magic code value: doStuff("Some Magic Code"); ... // Later on same inlined string: doOtherStuff("Some Magic Code"); versus: // Define it once: const GOOD_NAME_FOR_MAGIC_CODE = "Some Magic Code"; // Use it: doStuff(GOOD_NAME_FOR_MAGIC_CODE); ... // Use it somewhere else: doOtherStuff(GOOD_NAME_FOR_MAGIC_CODE); If that triggers an "ah-ha!" moment for you then you've likely got bigger problems.
- wereHamster 9y agoDecreasing code duplication is good of course. But you need to be careful that the two pieces of code are inherently equal and not just by accident. Because once you consolidate the code, for example like you suggest, and somebody comes along and changes `GOOD_NAME_FOR_MAGIC_CODE` to something else, without being aware that it is used in multiple places, he might inadvertently make a change he did not mean to do. Tests help, a good IDE helps (to find location where a symbol is used). But still, it's a fine line to walk.
- chairmanwow 9y agoWouldn't this name change be immediately caught by the compiler?
- yxhuvud 9y agoIt is obvious, but the code examples in the original link scare me.
- hinkley 9y agoIt's worse than that. We have met the Enemy and the Enemy is us. Java, as introduced, claimed that nearly everything was an Object. What we got instead was nearly everything is a String. The Real WTF in this code is that all of the important information is passed around as Strings. The iterator and its source hint at this but she fixes the wrong problem. Ever has this been the way with Java. Despite having a statically typed language nobody turns the data into information and instead your modules just vomit strings at each other. Sometimes in groups of four or more.
- nickbauman 9y agoAgree. I think we're all Stringly-typed developers. We use strings everywhere. We throw strings across system boundaries at will.
- hinkley 9y agoThe first time I ran into this was on a project that was passing contact information around like String, String, String, int. Back and forth across the system in four or five workflows with extra data passed in here and there. Of course they left off Address Line 2 and rather than try to fix this in thirty places as I was told I made an Address object instead. With very rare exception, a street Address (Address 1) is meaningless without at least the zip code. And god help us if you had two addresses. Later it took someone a couple hours to fix it for 9 digit zip codes. No further changes unless a new State joins the union (this was for a state court system and they required a domestic mailing address. One of the few times I didn't eventually need to process international addresses)
- seanp2k2 9y agoIf you're just using objects to represent groupings of strings and encapsulate behavior around converting them back to strings for output at some point, why not just operate on the strings instead of all the serialization / deserialization? Maybe enough of the quick stuff I do isn't enough to warrant more complex data structures, but I get pretty far into a problem with just dicts (in Python) and generally get annoyed when something returns back objects that have no easy str representation without finagling the object into giving me what I need. I think that this is generally a bigger issue with libraries and treating object metadata (stuff like time stamps, extra attributes, etc) as of equal importance as the data it represents; if an object has one obvious attribute for what the thing is, make that the default thing you get when you just refer to the thing, instead of making everyone call some weird method to turn the instance into the representation that you likely want to get out of it.
- hedora 9y agoThe "smell" here is that the API does O(n) allocations for a read-only check of a database schema? Why add an API to have a method that checks for containment directly (and hopefully delete the conatiner returning one entirely?) Also, it probably makes sense to cache the schema data somewhere. Finally, my gut tells me the check for the existence of this name probably can be moved earlier in execution (like during initialization of the called class), which will cause the system to fail earlier and be easier to debug (so it would be good to check that before touching it).
- dionian 9y agoMany of the syntax highlighting that IDEA does is very obvious once you're alerted. The nice thing is it does hundreds if not thousands of analyses and presents them to you as actionable feedback.
- lstyls 9y agoAgree. What makes a pattern a code smell is that it is immediately recognizable and is almost never a false positive. Either the immediate implementation is problematic, or something about the larger context of the implementation is problematic. A functional programming diehard could assert that iteration is a code smell, because recursion is the preferred solution. But there is nothing inherently wrong with using iteration to implement a solution in Java.
- burgerdev 9y agoI think the blog post is not so much about advanced code smell mitigation strategies, but rather about highlighting the features of IntelliJ.
- ubertaco 9y agoI like this kind of article not necessarily because it's revolutionary or groundbreaking (because it's not), but because it serves as a helpful reminder sometimes, and can help stick an "observation bias" bug in my brain to notice more often the sorts of examples it calls out.
- Ar-Curunir 9y agoYup, I already know where sets and maps are better than lists, but this article now primes my brain to look carefully the next time I write more code.
- maweki 9y agoThe author proposes using data structures that use hashing without even mentioning the necessity of having a hash function and immutable data for the hashed objects, because in their case they were basically just using strings. It isn't always that easy.
- megawatthours 9y agoSomething I see often and is a huge code smell to me is not using the most restrictive form of iteration. If you see collection.map(...) you know that each iteration is simply a pure function from original element to transformed element, which is an immense help when reading the code. If you can use only map / filter / takeWhile / join etc to express what you are doing, use those! If not, try and just use reduce / foreach. If not, try and just use for. Only use while if nothing else works!
- masklinn 9y ago> If you see collection.map(...) you know that each iteration is simply a pure function from original element to transformed element, which is an immense help when reading the code. You'd think so, but I've had colleagues who managed to fuck that up and use map or list comprehension solely for side-effects.
- mikojan 9y agoThis. I've seen FP features (and incidentally OOP features as well) misused so hard, I'm thankful for every plain loop (or struct for that matter).
- OtterCoder 9y agoEven Excalibur can be used to mince garlic, and even a supercomputer can be used to play Zork.
- masklinn 9y agoYeah I mean my point is "trust but verify", I love restricted iteration construct, but just because they're being used does not mean they're being used "properly" unless the language ensures it.
- apta 9y agoI saw some code like this: def update(): Try[Unit] = { parser.parse(...).map(result => updateState(result)) } And I thought it was abusing the map() call for side effects. However, it is still shorter than writing it out as follows: def update(): Try[Unit] = { parser.parse(...) match { case Success(result) => updateState(result) Success(()) case f@Failure(_) => f } } So I didn't have a strong opinion either way since semantically both do the same (and in the case of Scala, the first one is potentially more performant since it relies on the JVM doing virtual dispatch as opposed to calling unapply() and matching, not to mention potentially less garbage being generated).
- glibgil 9y agoHashing is indexing and there is no faster and memory efficient index than an array (not vector) of sorted items. Iteration is the best if you can organize your data for it
- deleted 9y ago[deleted]
- jdmichal 9y agoNot sure what you're trying to get at. Indexing by surrogate index, yes. Indexing by value? That doesn't help. The best you can do is binary search -- O(log n). Hash-based structures are average amortized constant time -- O(1). This works because the key is a function of the value, rather than a surrogate key which is unrelated to the value. Straight from Wikipedia: "In many situations, hash tables turn out to be more efficient than search trees or any other table lookup structure. For this reason, they are widely used in many kinds of computer software, particularly for associative arrays, database indexing, caches, and sets." https://en.wikipedia.org/wiki/Hash_table https://en.wikipedia.org/wiki/Hash_table
- deleted 9y ago[deleted]
- jnordwick 9y agoDoes anybody here really have to be told this? This is literally CS 101 (your first month of 61B at Cal). Why is this posted here?
- jimbokun 9y agoSo everyone here is required to have a CS degree now? Lots of self taught developers, and people with business or quantitative-but-not-coding backgrounds read Hacker News, I think.
- jnordwick 9y agoI expect a higher level of content from HN, not basic material. I think other sites are more appropriate for this.
- Froyoh 9y agoI wasn't taught this in my class, I was always dealing with very small lists so iterating did not make much of a difference.
- seattle_spring 9y agoAre you the guy in that same class that points out how "obvious" everything is, in an attempt to prove how much smarter you are to the professor?
- strictnein 9y agoYes. He's in the discussion about building a Hash function in C doing the exact same thing.
- squiguy7 9y agoThese examples seem to discuss data structure choice what I thought it might be about. I have encountered two major bugs at two separate companies all involving iteration and maps. Both of the examples were written in Go and although language shouldn't matter, iterating over a map is non-deterministic. But each time the author intended for them to construct the returned data structure in a pre-defined order. We had been suffering from poor caching performance and saw our Cassandra reads spiking as a result. Once we spotted the incorrect cache key construction, our reads to Cassandra dropped significantly allowing for better performance all around. Perhaps others would have spotted this but it seems innocuous in code review. I know I will be more diligent in the future.
- codeulike 9y agoI really don't like the term Code Smells. Its just seems like it heralds finicky criticism that ignores wider context and real-world pressures. Outside of academia, you can't release a build without creating a few smells
- KirinDave 9y agoReading this article, I got a bit bothered. I agree with the sentiment: people repeat entirely too much code. There are very few cases where a for-loop is the right thing to write. Using generic methods which can be tested and shipped in isolation is basically always better. But the article seems to imply there are performance concerns in some cases with its talk of using "a data structure". This irked me, because it's not like Set will beat linear time in balanced read-writes and without care the hash tables end up being non-constant as well.
- needcheapbw 9y ago> There are very few cases where a for-loop is the right thing to write. I'm trying to get my head around that, do you mean any for loop is bad or many nested loops?
- KirinDave 9y agoIndexed loops are just a bad problem waiting to happen. They introduce arithmetic bugs, are the least efficient way to traverse most any data structure but a sequential memory segment, maximize the opportunity for error, and are often harder for compile-time optimizers to work through (since it's harder to prove what they do in situ). Abstracting away your iteration is important.
- needcheapbw 9y agoLike, for(int i=0; i<10; i++) { // todo } OK well yes there can be bugs using the index and even C++ back when I was doing it had the Boost library which had for_each. In Java we got something similar back in Java 5 or 6. Also we have fancy stream operations now in Java 8 which took me the longest time to come to appreciate. The only down side is removed compatibility with other languages like C and C++ and C#, locking in the app to a specific language more. There is on rare occasion, however, sometimes a problem that is solved more clearly or handily using indexing, similar to step indexing in BASIC. One use case where I've seen stepped indexing useful is in the field of robotics, which uses step characteristics for synchros and servos.
- e12e 9y agoboolean hasName(String storedName) { return getLoadNames().\ contains(storedName); } > That’s it. No more looping, just a simple check. And how is set.contains implemented? https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/util/HashSet.java https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/... wraps https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/util/HashMap.java https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/... public boolean contains(Object o) { return map.containsKey(o); // map is HashMap } public boolean containsKey(Object key) { return getEntry(key) != null; } And finally: final Entry<K,V> getEntry(Object key) { int hash = (key == null) ? 0: \ hash(key.hashCode()); for (Entry<K,V> e = \ table[indexFor(hash, \ table.length)]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key \ || (key != null \ && key.equals(k)))) return e; } return null; } TL;DR how did they imagine a general setContains would be implemented without a loop? [ed: obviously it makes sense to use a datastructure that more closely match your intent, but the wording here struck me as a bit odd...]
- deleted 9y ago[deleted]
- flavio81 9y agoTo say that iteration should be "considered a code smell" is not a good thing to do for the programming community. Present-day CPUs operate on structures by iterating. On a typical high-level programming language, they are at some point done by either (a) iteration or (b) recursion. If the compiler does not do automatic tail call optimization; then (b) recursion can't be made as optimal (in terms of speed and resources) than (a). The latest Java compiler does not perform tail call optimization and probably never will be able to do it. I mention Java because the article is targeted to the Java crowd. Thus, iteration IS a very important tool for enhancing performance, and real-world (production) systems might have very important performance requirements. Iteration, in a code, should just be considered... iteration. No kind of smells. In the past, there was a very important computer scientist called Edsger Dijkstra wrote a paper called "Goto Statement Considered Harmful", GOTO being considered a "code smell": Dijkstra is a very important guy; an unit of measure, the nanoDijkstra, was named in his honor. However, even afterwards, new programming languages did include a GOTO statement (or some form), because there are special cases when they are needed for higher performance or (believe it or not!) producing more readable code.
- maxscam 9y agoClickbait title, article doesn't seem to back up this hyperbolous claim either
- gpvos 9y agoThe autoplaying code editing animations are really annoying. By the time I've read the preceding text, it is somewhere in the middle and I don't know what it's doing. Please, can we just agree that animations and videos (and audio) should only play when the reader initiates it? Also, a progress bar would be handy, although it may not always be necessary (for short animal videos and similar).