New answers tagged

2

Here's one I use for debugging (in Clojure): user=> (defmacro print-var [varname] `(println ~(name varname) "=" ~varname)) #'user/print-var => (def x (reduce * [1 2 3 4 5])) #'user/x => (print-var x) x = 120 nil I had to deal with a hand-rolled hash-table in C++, where the get method took a non-const string reference as an argument, meaning I can'...


1

One of the best rules of thumb in such situations is a famous phrase: "Code is read more often than it is written." You're going to write the code once. It's not that big of a deal to type a few more characters. When starting use macros, the question should be "does this make it easier for the reader to understand." The answer to that question depends ...


3

To answer your question, yes you could write a macro to accomplish the for loop. However, this is not a very good idea. As another user pointed out, this adds difficulty in understanding the program. Also, I have seen first hand where this can become a problem. I once witnessed a situation where a contractor used 4 or 5 levels of macros on top of one ...


11

Don't do this. Using macros to reconfigure the language like this is like writing in slang. One or two instances might not seem so bad to someone has to read it (including yourself) but every time you do it you make it that much more likely that the 'well what does that mean' effect happens. You're needlessly obscuring your code, adding a layer of mental ...



Top 50 recent answers are included