Patrick (patrickwonders) wrote,
Patrick
patrickwonders

  • Mood:

The role of functions/procedures/methods...

So, I've been coding in Objective-C for a month or so now. It is interesting (in a Sapir-Whorf sort of way), how the language changes the way that I code. Switching from Objective-C to Lisp made me notice something that I hadn't really noticed when moving from C++/Java/Perl to Lisp.

In Lisp, I will pull something out into a separate function if it makes the current function more self-contained, more one idea. In C++, I will only pull something out into a separate function if I need the same functionality in multiple places.

Actually, it's even worse than that in C++. For stuff that is less than six or so lines, I might maintain it in several functions. Or, if I'm using some Literate Programming tool, I will just use the same chunk in multiple places.

The notable exception in C++ is when I want to use something as a loop conditional, I may bother to break it out into its own function.

    while ( incrementCounter( cntr, min, max, dimensions ) ) {
        // body of loop here
    }

In C++ or Objective-C, I might do something like this:

    // some portion of my function

    unsigned int choice = random() % length;
    void* currentChoice = options[ choice ];
        // yes, I know I can memcpy(), but that's not as obvious
    for ( unsigned int ii=choice+1; ii < length; ++ii ) {
        options[ ii-1 ] = options[ ii ];
    }
    --length;

    // some code using currentChoice

In Lisp, I would never consider keeping that code inline. I would put it in another function right away.

This, this is one of the things I meant when I said "Lisp is just plain fun." It's easy to make a new function. I can just do it. I don't have to fret too much over the name. I don't have to fret too much over the argument list. I don't have to pre-declare it in this header file with the same signature as that implementation file. I don't have to pretend its a method when it's really just a function. I can return multiple values if I need to do so. I don't have to worry much about which compilation units will need to see this to compile.

Part of the maze generation code that I wrote in Objective-C needs to track walls. I don't need the same structure during generation that I will use once it's generated. So, I have a Wall class declared. It feels wrong to declare it right in the header file for the Maze. It feels silly to break it out into its own header file. What I should be doing is making a separate MazeFactory and have its implementation include the declaration of this Wall class. But, that is such overkill here. I just want the damn maze. In Lisp, I would just be done with no feelings of guilt at all.

Tags: c++, lisp, objc, programming
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments