MVC code patterns, templates, and complexity


Post #: 65
Date: 2012-11-08 15:15:49.000
Author: Rocket Man

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.


Rocketeer on 2012-11-08 15:38:17.000

Okay so how does this look then?

Rocketeer on 2012-11-08 16:53:36.000

Okay, seems good on a number of levels - so further to that let's listen to the jungle drums over in a slashdot thread:

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian Kernighan, then he updated that somewhat with, "Everyone knows that debugging is twice as hard as writing a program in the first place. So if you’re as clever as you can be when you write it, how will you ever debug it?"

"You write code that is right on the bleeding edge of your ability to understand it. But when you come to debug it, you need even more understanding, which you haven't got." Anon.

"It's simple math, yes? Let's say you're writing a application named "Thingy".

X = how smart you are.
S(WriteThingy) = how smart you need to be to write the code for Thingy.
S(DebugThingy) = how smart you need to be to debug the code for Thingy.

Debugging is twice as hard as writing the code in the first place.

So we get:

S(DebugThingy) = 2 * S(WriteThingy)

Given that:

if you write the code as cleverly as possible

We have:

X = S(WriteThingy)

Which basically means that you are no smarter than being able to write Thingy.

And since:

S(WriteThingy) < 2 * S(WriteThingy)

We get:

X = S(WriteThingy) < 2 * S(WriteThingy) = S(DebugThingy)

Or:

X < S(DebugThingy)

Which is basically what he said:

you are, by definition, not smart enough to debug it." Scaimer.

Rocket Man on 2012-11-08 17:22:26.000

I've found a source for my claim about the number of bugs relating to the number of lines of code.

"Productivity and reliability depend on the length of the source regardless of the language level." -- Greg Wilson, referring to actual research studies and not anecdotes.

This comes from his talk which can be found here: http://vimeo.com/9270320

Rocket Man on 2012-11-08 17:24:14.000

Lol, I was using [ code ] and it translated it to the code tag, tee hee, need a
in there...

Tiger on 2012-11-10 14:42:53.000

A few years ago I actually made some macros, like (setq h "123") (h3 "foo" 3 h 8) yielded <h3>foo31238</h3>

Would that be handy to fold in, in lieu of a templating language? Then it should be easy to use newLISP as the templating language.

Rocket Man on 2012-11-11 12:19:08.000

That's an interesting idea. I have some plans for doing this sort of thing with Rockets.

Tiger on 2012-11-12 15:07:46.000

The thing that makes template language nice, is the ability to run a tool like HTML "tidy" over it, to clean it up. And also, the ability to use it for stuff that isn't even related to HTML.

For instance, for years I've twiddled on and off with a music generator; it takes a source format, and outputs PDF files and various arrangments in MIDI format. So if I alter the source, ALL the variants get updated appropriately. A template language would be perfect for this.

The template language used by Django is really simple and safe.

Rocket Man on 2012-11-13 15:45:47.000

I'm a big fan of single-source generating a bunch of different outputs (that's how I generate the Documentation page... straight from the main framework file itself.. so every time I update the framework, the Docs page gets updated automatically)

I do want to make it easier to code basic HTML elements (that's why I have various commands like (display-image) for example) without having to write as much HTML. It makes the source code look a lot tidier, for one thing. The result should be clean HTML without needing external tools to tidy it up.

Lots of things to do!

Tiger on 2012-11-13 16:30:36.000

I guess the reason I'd want to use HTML tidy is, I have no good tool like "indent" that works on newLISP. I like the elisp style of indentation, but I use vi for editing, and as good as vim is about paren-matching, it doesn't indent worth shite.

View this post in the forums

Views: 3483