Fun with macros



  • I'm sure this will rate somewhere between "meh" and "ugh" on the WTF scale, but it's pissing me off right now.

    For background, I work mainly in SAS, which is a WTF in its own right. It's a little bit like SQL, but with a whole bunch of extra functionality for things like statistical analysis, data mining, etc. Macros in SAS work similarly to C, but are prepended with % instead of # (except for macro *variables*, which are marked with & and have a whole other level of complexity that I won't go into). So when you run code with macros in it, the preprocessor will go through and replace macro calls with the code inside the macro definition. The macro might be defined earlier in the code you're running, or it might be in an include file (i.e. using the statement %include "some file" will run all the code in "some file", which can include macros), or it might be in an autocall library (so when you call %macroname, SAS will look in a set of predefined folders for a file called macroname.sas with a %macro macroname definition). Macros can also take parameters, which can be referred to and altered within the macro.

    The program I'm trying to make sense of right now mostly uses the autocall library, which includes a couple of folders of macro files. For the most part, this has been done pretty well with decently named macros - for example, the macro %apply_amendments applies a set of amendments to the dataset, the macro %max_and_mean calculates the maximum and average values of a set of variables, as well as the ratio of these two values. Commenting is a bit sparse (and occasionally sarcastic), but for the most part it's been readable and I can generally work out what bits of the code does with just a little bit of frantic hopping between multiple files to track down where a macro or dataset or variable came from.

    Then comes %prog. What does %prog do? It runs a program, duh. Well, actually, it runs a little bit of code to get some data into a particular form, then it runs a program. Which program? It depends on the value of the parameter, p. So of course when p = data_aggregation, it runs %data_aggregation, right? Of course not - it runs %data_aggregation when p = 1. And so forth, for p = 2, 3 and 4. Actually, it loops through a number of strings to run each of the four programs on a series of input datasets, but the loop for this is half-hidden amongst other code, and nowhere in the comments does it mention that it does this. And where are these programs stored? Not in the autocall library, but in four separate program files that are %included in the main program - so to find out what %prog(1) does, first I have to look at %prog to see what program it calls when p = 1 (which is %data_aggregation), then I have to open up prog1.sas and find the macro definition for %data_aggregation, then look at the code for that and realise that it calls about 10 other macros in the same program file that are in no particular order.

    Frankly, I should have realised this particular bit of code would be trouble as soon as I saw the line %local c q y v l i;



  •  Can't you %macro data_aggregation_procedure 1  %macro some_other_procedure 2 and then %prog data_aggregation_procedure?

    (or something like that)

    That said... I just imagined what could happen if  someone decided to add local symbols to C macros (just expand them to "_mlocal"+sha1(filename+macro_name)) So much space for new WTFs... Also IOCCC contestants would have new possibilities.



  • Perhaps I'm misreading, but it sounds more like a shortcoming in the development environment than particularly poor coding practice - although I've never tried it, and have no idea what I'm talking about really. If it's reasonable other than the clumsiness to want to split the code out into macros, then you need to have an environment that helps negate the clumsiness.



  • @ConMan said:

    I'm sure this will rate somewhere between "meh" and "ugh" on the WTF scale
    So long as you avoid a 'mugh' rating...



  • Welcome to object oriented debugging.



  • @viraptor said:

    Also IOCCC contestants would have new possibilities.
    Seriously ? IOCCC back on the track ? Would be excellent news. They had announced a come-back sooner this year but nothing came...


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.