Do I write a new function or put it in a (display-partial)?

Blog post


Rocket Man
Posts: 358
Posted on: 2012-10-15 23:14:11.000
Eliminating duplicate code is a great thing. If you have code that is duplicated on many pages, if you want to change it you have to remember to change it on all your pages.

I originally made a framework function (display-navbar) that displays a navigation bar and sign-in form. This was great, but (display-navbar) needs to take a list as an argument to decide what menus to actually show. I didn't want to repeat this list on each page, but I also needed a way to show which page was currently active.

One way would be just to define a new function, (display-rockets-nav "active page") that would then call (display-nav) and specify which page is actually active. But then I would want to make sure this function is defined on every page. Well, I already have a set of common functions in the partial "rockets-common-functions.lsp". I could just put it in there.

The other option is to define a new partial page, "rockets-navbar.lsp", and call this partial each time. Which approach is better? They both require the same amount of code (I have to add a line to either call the function or display the partial). The only real difference is that if I go the function route I would have to have two functions, one in the framework, and one in the application, that had similar names, and I'd have a function calling a function. If I put it in a partial file, it looks like it is doing something different.

Ultimately it's an aesthetic choice. If you make your own application you might decide to do it differently. I might even change my mind at some point!

Rocket Man
Posts: 358
Posted on: 2012-10-23 00:06:35.000
Note: as time has gone on, I think I'm happy with the (display-partial) choice in this case. I'm also using the partial to display any error messages right below the navbar, and I think having a partial works well for that.

Views: 2965