Earlier quoted context omitted.
That 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…
Decreasing 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. Tes…
Code Smells: Iteration
51–60 of 95 posts
Re: Code Smells: Iteration
#52Re: Code Smells: Iteration
#53At 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 struct…
Agree. 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 it…
Re: Code Smells: Iteration
#54I 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.
Re: Code Smells: Iteration
#55Earlier quoted context omitted.
That 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…
Decreasing 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. Tes…
If a dev can't be bothered to figure out where different pieces of code use a variable and whether the change is appropriate for them, do you think they will have the foresight to search for the duplicated code when they need to change it everywhere? Any decent dev should do both, and the former is a MUCH easier process than the latter.
Edit: despite the above, I don't completely disagree with your statement.
Re: Code Smells: Iteration
#56Does 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?
So 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.
Re: Code Smells: Iteration
#57Earlier quoted context omitted.
Agree. 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 it…
Specially if you require higher performance and there are no tail call optimizations.
Re: Code Smells: Iteration
#58Does 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?
Are 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?
Re: Code Smells: Iteration
#59At 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 struct…
It'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 statical…
In C++ we see quite a bit of abuse of std::pair instead of just making a fucking struct to hold 2 properly named, easy to read pieces of useful data. Nope! Apparently calling all your data members "first" and "second" is better to some people.
I don't know if it's an educational problem, a language issue, or something else, but I see it so frequently that there must be some common underlying cause (or set of causes).
Re: Code Smells: Iteration
#60 boolean 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/...
wraps
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 getEntry(Object key) {
int hash = (key == null) ? 0: \
hash(key.hashCode());
for (Entry 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...]