r/programming • • Feb 22 '15

Java Makes Programmers Want To Do Absolutely Anything Else With Their Time

http://www.forbes.com/sites/quora/2015/02/21/java-makes-programmers-want-to-do-absolutely-anything-else-with-their-time/
35 Upvotes

636 comments sorted by

View all comments

Show parent comments

2

u/yogthos Feb 22 '15 edited Feb 22 '15

Using macros is generally discouraged and vast majority of problems can be solved with regular functions. Beginners often get overexcited about using macros and that's a mistake in my opinion. Macros are a useful tool, but that's really not the main appeal of Clojure. If you look at popular Clojure libraries in the wild you'll see that there are very few macros in them. As the style guide says: don't write a macro if function will do.

One of the main benefits I see using Clojure over Java is actually how rare null pointers happen, typically when doing Java interop. Since all the standard library functions handle nil values intelligently, they simply bubble up to the top in most cases. Instead of having to pepper null pointer checks all over the place, you can simply check for a value at the end of the transformation.

Again, I'm not sure what you mean by "fix the compiler". The compiler works exactly as it's supposed to, but you have to understand the intricacies of the language in order to use the most advanced features.

Macros require you to understand the details of how the reader and the evaluator work, what macro expansion is, when it happens, how macros interact with one another and so on. It's hands down the most complex feature of the language and I definitely would not recommend focusing on it as a beginner.

-1

u/nickguletskii200 Feb 22 '15

Using macros is generally discouraged and vast majority of problems can be solved with regular functions. Beginners often get overexcited about using macros and that's a mistake in my opinion. Macros are a useful tool, but that's really not the main appeal of Clojure. If you look at popular Clojure libraries in the wild you'll see that there are very few macros in them. As the style guide says: don't write a macro if function will do[1] .

It wasn't enough in my case. Why would I waste my time on fighting macros if it were?

One of the main benefits I see using Clojure over Java is actually how rare null pointers happen, typically when doing Java interop. Since all the standard library functions handle nil values intelligently, they simply bubble up to the top in most cases. Instead of having to pepper null pointer checks all over the place, you can simply check for a value at the end of the transformation.

But the compiler still nullpointed.

Again, I'm not sure what you mean by "fix the compiler". The compiler works exactly as it's supposed to, but you have to understand the intricacies of the language in order to use the most advanced features.

Throwing NullPointerExceptions instead of syntax errors isn't "working as it is supposed to" in my book.

1

u/yogthos Feb 22 '15

It wasn't enough in my case. Why would I waste my time on fighting macros if it were?

There's nothing that macros let you do that can't be done without macros. They're a way to template code when you see repetitive patterns. You still haven't described what it is that you were trying to do in the first place.

But the compiler still nullpointed.

I'm not sure what you mean by "compiler nullpointed", are saying you crashed the compiler. I've certainly never seen that happen myself.

Throwing NullPointerExceptions instead of syntax errors isn't "working as it is supposed to" in my book.

Again, perhaps you could actually described exactly you did and how you got a null pointer. Perhaps you can paste your code. I have a rather hard time believing that you managed to crash the compiler.

1

u/nickguletskii200 Feb 22 '15

Again, perhaps you could actually described exactly you did and how you got a null pointer. Perhaps you can paste your code. I have a rather hard time believing that you managed to crash the compiler.

I don't have the code anymore, that happened 1.5 years ago and I pretty much threw away the project.

There's nothing that macros let you do that can't be done without macros. They're a way to template code when you see repetitive patterns. You still haven't described what it is that you were trying to do in the first place.

Yes, but it would be considerably harder to do. If I recall correctly, macros in Clojure are actually implemented in Clojure, so yes...

3

u/yogthos Feb 22 '15

Right, so not knowing what you were trying to do it's really hard to say what went wrong there. As I said, understanding how a macro works can often be non-trivial. There are tools like macroexpand that help you see what the resulting code will look like, but they can be difficult to reason about. This is the reason macro use is discouraged unless it's strictly necessary.

My point is that it's far more likely that you simply didn't understand what the code templated by the macro was doing than there being any actual problem with the compiler.

1

u/nickguletskii200 Feb 23 '15 edited Feb 23 '15

Sorry, but if at any point the compiler gives a NullPointerException instead of a proper message, there is something wrong with the compiler. Everything else does not matter.

1

u/yogthos Feb 23 '15

Well I guess there's something wrong with the Java compiler then and it's been around a bit longer than Clojure:

Object o = null;
o.getClass();
java.lang.NullPointerException: null

1

u/nickguletskii200 Feb 23 '15

That's a runtime error, not a syntax error. A better example would be this:

Object o =

And then the end of file, so the next token is null. The Java compiler, however, handles this.

1

u/yogthos Feb 23 '15

Now show me an equivalent of that in Clojure that makes the compiler return a nil and we'll talk.

1

u/nickguletskii200 Feb 23 '15

https://github.com/timmc/sscce-macro-readstring

I ran into something similar, but not exactly this. You can remove the first 4 lines in src/org/timmc/sscce/macro_readstring.clj and you will get something more like what I got.

→ More replies (0)