3 ms·
I'll give one reason why Python may be better for team development than clojure/lisp: powerful macros. They do make it harder to reason about code you didn't wr
by yummyfajitas 17y ago
I'll give one reason why Python may be better for team development than clojure/lisp: powerful macros. They do make it harder to reason about code you didn't write. In Python, you can look at the inside of a function and have some idea what it is doing.
"It's doing kwyjibo(i,v) to all the i in zqmfgb. Now, lets see what v and zqmfgb are."
In clojure, that isn't always the case. An example: recently, I was trying to write an OpenGL app in Clojure. I found the following code on the net (pretend it was written by a team member):
(doto obj
(.glBegin ...)
(.methods ...)
(.glEnd))
I decided I wanted to factor out the (.glBegin ...) and (.glEnd). Aha, this is a job for macros!
(gl-begin-end body) ; this should macro expand to:
(do
(.glBegin ...)
body
(.glEnd))
This would just fit inside the the doto, and be exactly what we need. The problem is that doto significantly modifies it's contents:
(doto obj
(.method x)
(.method y))
(clojure.core/let [G__1206 obj]
(.method G__1206 x)
(.method G__1206 y) G__1206)
So I got this:
(doto obj
(gl-begin-end body))
;macroexpand once
(clojure.core/let [G_1725 obj]
(gl-begin-end obj body))
;macroexpand twice
(clojure.core/let [G_1725 obj]
(do
(.glBegin ...)
G_1725
body
(.glEnd)))
Obviously this didn't work.
In this case, the problem is me: `(doto obj body)` is idiomatic clojure, and I should have known about it. On the other hand, `(kwyjibo obj obj2 body)` could be very hard to figure out and modify.
I should, however, raise the caveat that I'm not a good enough lisp programmer to know if this is really a problem in practice.
- jules 17y agoYou say that (doto obj (.glBegin ...) (.methods ...) (.glEnd)) > I decided I wanted to factor out the (.glBegin ...) and (.glEnd). Aha, this is a job for macros! (gl-begin-end body) ; this should macro expand to: (do (.glBegin ...) body (.glEnd)) But why did you change `doto` to `do`? Here's how you can do this: (defmacro gl-begin-end [obj & body] `(doto ~obj (.glBegin) ~@body (.glEnd)))
- yummyfajitas 17y agoAs I said, I know the programming mistake I made here (and that it reflects lack of knowledge of clojure on my part). But the problem I had with the doto macro could easily translate to a problem you have with my kwyjibo macro. In python, I know what this code does, regardless of what happens at higher levels of indentation (ignoring some rarely used class-level hacking): f(a,b,c) g.h(i) In lisp, I can't necessarily assume that (f a b c) (.h g i) will actually call f on a,b,c and then call the h method of g on i. The ability to modify syntax programmatically implies that syntax (i.e. the s-exp structure) may not mean what you think it means. I'm speculating that this may make collaboration more difficult.
- gruseom 17y ago[Macros] make it harder to reason about code you didn't write. No, actually, they don't. Given the usual high quality of your comments, I'm surprised you would repeat this canard. you can look at the inside of a function and have some idea what it is doing. This is just as true of a macro. In fact it's more true: you can expand a macro in place on your code and see what it is doing, a maneuver there is no exact analog to in functionland. The macroexpansions in your example make it immediately apparent what the problem was. Macros are different from functions. That's why they're useful. I'd encourage you to stick with the learning process. The key difference with what you were expecting is that macros control the evaluation of their arguments.
- yummyfajitas 17y agoMacros do more than just control evaluation of their arguments (although that is often their primary use). If you simply want to control evaluation, you can do that with controlled laziness and functions. Macros can rewrite your code. To understand a line of code, you must understand enclosing sexps before you can understand what the line of code does. In a macro-less language, that is not necessary. It is something extra you need to think about (and I agree that C-x C-m helps), and that makes it harder to reason about. I never said they aren't useful (and I'd strongly disagree with someone who did). It doesn't change the fact that they can make understanding code harder.
- gruseom 17y agoMacros do more than just control evaluation of their arguments Yes, but I was commenting on your example: it was what the macro was doing to control evaluation that caused it to depart from your function-centric intuition. As for the general point - that macros "can make understanding code harder" - I haven't heard any argument that doesn't apply just as much to functions mutatis mutandis. A function adds complexity too. You have to understand its name. You have to know what each of its arguments is there for. To truly understand a function, you may have to look at its source code. Therefore functions can make understanding code harder! Except, of course, that they make it easier, assuming they're used properly to organize a program, and people who have experience with them consider them indispensable. Same for macros. Most of the objections people bring up are exactly this sort of pseudo-argument that, if it were true, would in fact be a reason to avoid higher-level languages. If you're really making a general claim to the contrary, please come up with more than a single trivial example that is confusing only at the beginning of the learning curve.
- omouse 17y agoPowerful macros and functions make it harder to reason about code. In Lisp and Smalltalk, you can look at the inside of a function and have some idea what it is doing. With Lisp you can use #'DISASSEMBLE and with any Smalltalk worth its weight, you can simple click around and find the method that you want to look at
- twopoint718 17y agoI agree completely, the notion of the "inscrutable macro" is often brought up when arguing against a Lisp. Whatever one could say is dangerous about macros, I would argue, could be said equally of functions. But I think something else is going on here, the Inscrutable Macro idea serves as a kind of "intuition pump" (of the negative kind) http://en.wikipedia.org/wiki/Intuition_pump http://en.wikipedia.org/wiki/Intuition_pump to try and demonstrate macros' badness. If someone isn't very familiar with lisp they'll offer code that's merely not idiomatic, when someone is familiar with lisp then they'll offer up something that's just downright pathological, and I'll be the first to admit, that a pathological macro can be really pathological. At this point, equipped with their executable straw man, they claim "this macro is insane, and so macros are unmanageable." If the measure for unmanageability is just that impossible-to-understand code can be written in some language then the IOCCC means C shouldn't be used. In practice the Inscrutable Macro just isn't written.