Posted on: 2012-11-08 15:15:49.000
Today I implemented an entirely different way to view posts and comments, the forum view. When you view a blog post, the same post also exists in the forums. In fact, you can click on the "View post in forums" button to toggle the views.
A lot of people would tell you to write this kind of thing using the MVC (model-view-controller) model, or at least use a framework that has a templating system so that you can view the same data differently.
This is a completely valid perspective and I'm not going to argue otherwise. MVC is a well-established design methodology and it works pretty well for a lot of different applications. Templates seem like they could be really useful.
I didn't implement the forum view this way, however. I just had a toggle variable called (forum-view-post) and checked to see if it was true. If it was, then I executed a different code block. The real danger here is code duplication, which is something we always want to avoid.
The thing is, implementing this very different view took only ten lines of code
(displayln "<h3>" (list-post-data 3) "</h3>")
(set 'header-list '("Author" "Message"))
(set 'post-data (list (string "<img src='images/avatars/" (author-avatar (list-post-data 1)) "'><br>" (author-name (list-post-data 1))) (format-for-web (list-post-data 4))))
(set 'PostId (int (list-post-data 0)))
(set 'post-data (list post-data)) ; okay these two lines of code are duplicated... I can live with it for now
(set 'post-comments (get-record "Comments" PostId))
(if post-comments (begin
(dolist (p post-comments)
(push (list (string "<img src='images/avatars/" (author-avatar (p 2)) "'><br>"(author-name (p 2))) (format-for-web (p 5))) post-data -1)) ; add each comment to the thread
(display-table header-list post-data "striped")
Okay, so two lines of code are duplicated in the alternate (blog) view, which I had already written. If it was much more than this, maybe I'd write a function and move it outside the block so that I didn't have duplicate code. Obviously I don't want to have to change things multiple times every time I change the view. But these two lines only extract the comment data from the database, so I wouldn't be changing these anyway.
Now, let's say I wrote this using a template system, or with MVC, or both. It would be a lot
more than ten lines of code. I would have to learn another language (the templating language) and I would have to write a bunch of classes and methods and make sure they all work properly together, maybe writing a bunch of unit tests to ensure I don't break anything if I change the contracts between the model, view, and controller. More code equals more opportunities for bugs. In fact, the number of lines of code is the only
aspect of development that has a strong correlation with the number of bugs. Everything else is too hard to measure. Fewer lines of code also take less time to write, in general. So all else being equal, wouldn't you want to have fewer lines of code to do the same thing?
As far as templates go, they make a lot of sense in a big company where there is a whole department doing user interface design and another department doing coding. The designers can learn the templating language and HTML, and not have to learn anything else. For my personal development, I am both the interface designer and the developer.
The newLISP code is
my templating language. I don't want to have to learn another language if I can just keep everything together. Overall, the arguments for using MVC and templating work better for larger projects with lots of people. newLISP on Rockets is optimized for very small development teams, including projects where the team is only one person. In these cases, simplicity almost always wins out over complexity.