7 ms·
I'd love to be sold on a new language, but using it by showing me ridiculous examples of a language I know reduces the credibility of the seller. It's an other
by efsavage 15y ago
I'd love to be sold on a new language, but using it by showing me ridiculous examples of a language I know reduces the credibility of the seller. It's an otherwise good looking article but when I see this:
public boolean hasUpperCase(String word) {
if (word == null) {
return false;
}
int len = word.length();
for (int i = 0; i < len; i++) {
if (Character.isUpperCase(word.charAt(i))) {
return true;
}
}
return false;
}
Where it should be this:
public boolean hasUpperCase(String word) {
return word != null && word.matches(".*[A-Z]+.*");
}
It casts doubt on the other examples. I'm not saying Java isn't a verbose language that can get tedious, but let's use some decent examples.
- swannodette 15y agoIt's doesn't cast doubt on the examples at all, your code is less generic: (defn has-uppercase? [string] (some #(Character/isUpperCase %) string)) This code can deal with any sequence of characters: String, Array, PersistentList, PersistentVector, Cons, LazySequence, etc. Your example only deals with String.
- efsavage 15y agoFair enough, but mine was not an alternate to his Clojure code, but to his Java code (which only checks String). Perhaps he should have focused on the functional efficiency like you did, rather than just bloated lines of code.
- seabee 15y agoIt's still not an alternate to the Java, since there are more upper-case characters than the 26 your regex matches. But I'm sure there is an appropriate Unicorn character class you could use instead.
- palish 15y ago... A String is a sequence of characters. Why would you want a "list of characters"? That's called a String.
- barrydahlberg 15y agoUseful if you are composing the function with others which may not be built specifically for strings, e.g. filter, map etc.
- Nrsolis 15y agoI'm with you. The author doesn't appear to be very experienced in any of these languages. He just seems to be posting his own naive interpretations. That said, I'm really excited about Clojure. I got back into programming via Ruby and now Clojure is getting me back into data structures.
- bozhidar 15y agoI'm annoyed and irritated by non-stop off the topic comments. Apart from the fact that the regular expression solution will be several times slower (and not equivalent to the original problem), regular expressions are a DSL of their own right and Java can claim no credit for their conciseness. When you're writing an overview article you cannot go into lengthy examples. These small out of context snippets just serve to illustrate the general idea. I wouldn't dwell too much on them :-)
- efsavage 15y agoI appreciate that you can't write entire projects to compare, and for your article overall, just asking you (and other authors who write similar articles) to give people familiar with the incumbent a bit more to chew on. Nobody writes code to see if a string has an uppercase character, that code was written long ago. Regex may be something that Java imported, but it's right there in the JDK so I have no reason not to use it. How do the two languages look when you're validating email addresses against a whitelist of domains from a config file or URLs for parameters based on some business rules? How do you handle an I/O error when you're halway through writing a file? These are the small problems we know we'll have to do over and over again, but not be able to stash in a standard library (or use someone else's). Pointing out that JavaBeans are silly doesn't hold much weight either, I use lombok :)
- Nrsolis 15y agoHow is my comment off-topic? I've read the article author's other work and my general impression is that he's inexperienced with all of these languages but that isn't stopping him from writing ill-researched tracts with poor examples. I've seen MUCH better writing here on HN. But hey, downvote away.
- abp 15y agoActually it's an example taken from here: http://www.jarvana.com/jarvana/view/org/apache/axis2/axis2-kernel/1.5/axis2-kernel-1.5-sources.jar!/org/apache/axis2/util/JavaUtils.java?format=ok http://www.jarvana.com/jarvana/view/org/apache/axis2/axis2-k... But i don't know whats less performant, your reimplementation with regexes or the clojure sample.
- nicpottier 15y agoI don't know Clojure, but it certainly reads as a direct translation of the original implementation, which is about as fast as you could make it in Java. The Regex version, while similarly terse would be massively slower. I agree the example is a bit odd, but it does serve the purpose of comparing equivalent code, which I believe is the whole point. It does do the job of showing that simple (and by inference (?) not so simple) algorithms can be more succinctly expressed in Clojure than in Java. (not that that is particularly difficult)
- abp 15y agoI know, i like clojure. Diving into it for a while now. :)
- efsavage 15y agoThe regex version is indeed slower, 5-20x depending on the length of the string and the location of the uppercase character, etc. > It does do the job of showing that simple (and by inference (?) not so simple) > algorithms can be more succinctly expressed in Clojure than in Java. (not that > that is particularly difficult) My point was not that Java is/can be as concise as Clojure (or Ruby or Python or some other version of Java or whatever) but that if you are focusing on comparing things, don't use examples that are so easy to write off. It is not clear to me, as a person experienced with Java but not with Clojure, that the Clojure method deals with many types of character sequences, nor is it clear to me that it's faster or slower than a regex match. If the argument solely lies on lines of code, my mind will think "is he comparing code optimized for performance from one language to code optimized for conciceness from another?" Is that ability to traverse sequences some bona-fide intelligence on the languages part or just a mess of instanceof expressions that really only work for a few expressions? This just struck me because I've been evaluating a lot of languages lately (Clojure is up soon) and it's frustrating to wade through people talking about languages using examples that are ignorant of all but their own, or contrived solely to make a point. Demonstrate the language in how powerful it is, how efficiently it runs, how gracefully it handles errors, how many lines of code it takes to solve specific problems, not standard utility ones that could easily be tucked into some library somewhere and it doesn't really matter if its 2 lines or 20.
- cema 15y agoI agree with the other comments which mentioned that Clojure is more flexible here (works with any seq, not just character strings). A similar Java code would have to accept an enumerable parameter (not sure what it is in java, something like IEnumerable in .NET) and the loop would change accordingly. The loop-less regex version would not work (it might with the input converted to a string, which may or may not involve a explicit loop, depending on what is available in java ecosystem).
- Sandman 15y agoI agree with your point, but you might be surprised how many people would write the more verbose version simply because they don't feel comfortable with regular expressions. Or how many wouldn't even think of using a regex for this.
- munificent 15y agoYour revision doesn't handle non-English uppercase characters. Character.isUpperCase() does.
- fauigerzigerk 15y agoYour example suffers from the fact that it is not an example for anything. It's just a special case and it's incorrect on top of that. The example he gives shows the power of lambdas and the generalized concept of sequences perfectly well.
- zwp 15y agoStuart Holloway uses something close to this in one of the presentations at infoq.com. He uses a method from Apache Commons stringUtils as an example of battle-tested enterprise java. Whether or not that is truly representative of java code is perhaps another question but there is certainly a body of similar accepted code out there eg http://commons.apache.org/lang/api-2.5/src-html/org/apache/commons/lang/StringUtils.html#line.5553 http://commons.apache.org/lang/api-2.5/src-html/org/apache/c....
- sevenproxies 15y agoThe Person example also seems overly verbose. Does the equivalent Clojure example have private attributes name and age? It looks like the author is accessing the name and age attribute directly instead of using getter methods as defined in the Java version. class Person { String name; int age; Person(String name, int age) {this.name = name; this.age = age} } This is still more verbose than the (defrecord person [name age]) but still, I agree with the dodgy examples used. Disclaimer : I don't know Clojure or similar and therefore I could be missing something. I did a quick search for defining private attributes in Clojure but didn't find any good hits.
- Periodic 15y agoThere are definitely some in the Java community who would view public properties as incorrect code. For example, it was not accepted at the large corporation where I used to work, nor is it allowed in my wife's Intro to Java class. There are some cultural language specifications as well as the technical ones.
- mquander 15y agoIt doesn't usually make any sense to speak of getter methods in Clojure. Records are immutable after construction.
- kaffiene 15y agoI agree with the point that you make, although I think that something like this is more idiomatic Java: boolean hasUpper(String str){ if(str != null){ for(char c : str.toCharArray()){ if(Character.isUpperCase(c)){ return true; } } } return false; } It's wordier than the Clojure example, but less than the Strawman example given in the article. I'd also argue that you could claim it reads better. To me, "Character.isUpperCase(c)" explains what it's doing better than "(some #(Character/isUpperCase %) string))"