Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Is there any way of improving this code? (Here's a JSFiddle with an example and comments)

Example: A Hotel greeter can greet guests and accepts tips, but it's impossible to see the total of tips a greeter has received.

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <pre>

            []___  
           /    /\     O <span id="alfred-says"><!-- the view --></span>
          /____/__\   /|\
          |[][]||||   / \
                     ALFRED

        </pre>
        <button onclick="javascript:alfred('greet')">Arrive at Hotel</button>
        <button onclick="javascript:alfred('acceptTip', 10)">Tip Alfred $10</button>
        <script>
        var HOTEL, alfred;
        HOTEL = {}; // Namespace
        (function (HOTEL) {
            // Reuseable controller
            var controllerPrototype = {
                greet: function () {
                    this.model.view.innerHTML = '"' + this.model.greeting + '"';
                },
                acceptTip: function (tip) {
                    this.model.tips += tip;
                    this.model.view.innerHTML = '"' + this.model.thankyou + '"';
                }
            };
            // Create a new greeter
            HOTEL.newGreeter = function (view) {
                var controller = Object.create(controllerPrototype);
                controller.facet = function () {
                    var args, method;
                    method = arguments[0];
                    args = 2 <= arguments.length ? [].slice.call(arguments, 1) : [];
                    if (method === "facet" || method === "model" || (controller[method] == null)) {
                        return undefined;
                    }
                    // Model, View and real Controller are never exposed
                    return controller[method].apply(controller, args);
                };
                controller.model = {
                    greeting: "Hello",
                    thankyou: "Thank you",
                    tips: 0,
                    view: view
                };
                return controller.facet;
            };
        }(HOTEL));
        // Alfred only exposes the controller facet. His tips are a secret...
        alfred = HOTEL.newGreeter(document.getElementById("alfred-says"));
        </script>
    </body>
</html>

Goals:

  • Keep it DRY
  • Keep it MVC
  • Make sure anyone with access to a "greeter" can never manipulate the view or model directly.

Limitations:

  • To avoid clutter, avoid type checking to avoid simple errors unless it is necessary to guarantee integrity.

Some specific thoughts:

  • Is there a better way of passing model and view into the controller? YES, improved
  • Is there a better way of communicating with the controller?
  • Is there a good way of making a more general view?
  • Any other improvements?

EDIT:

  • More DRY: Inherit controller methods instead of passing model as param.
  • Security/least knowledge: Prevent user from accessing model through the facet.
  • Included the whole HTML source as well.
share|improve this question
    
by "principle of least concern" do you mean "principle of least knowledge"? or "principle of least effort"? –  nrw Jun 17 at 15:13
    
I don't know where I got "Concerns" from, I meant to write "principle of least privilege", which I suppose is the same as or similar to "principle of least knowledge" –  Josef Ottosson Jun 17 at 15:19
2  
Principle of Least Knowledge, also known as the "Law of Demeter", has nothing to do with the "Principle of Least Privilege". Former is about not talking to strangers, latter is more related to security / authorisations. –  Mat's Mug Jun 17 at 15:34
    
My intention is that nothing with access to "Alfred" should be able to access or manipulate anything in a way that was not intended, so Principle of Least Knowledge/Law of Demeter is closer (although "has nothing to do with" is a bit of a stretch...). –  Josef Ottosson Jun 17 at 15:54
1  
@JosefOttosson in that case, isn't this mediator pattern a bit of a sledgehammer for a project which only does two very simple things (print a greeting and increment a counter)? If I reviewed this, my advice would be along those lines. –  Dagg Jun 18 at 23:30
show 6 more comments

1 Answer 1

From the comments, I guess you would like a theoretical review ;)

  • This:

    <button onclick="javascript:alfred('greet')">Arrive at Hotel</button>
    <button onclick="javascript:alfred('acceptTip', 10)">Tip Alfred $10</button>
    

    in my mind should be wired by the controller, that is, the linking of UI elements to data and UI changes

  • This:

    greet: function () {
        this.model.view.innerHTML = '"' + this.model.greeting + '"';
    },
    

    has your controller access data straight in your view, updating the DOM. This is wrong in my mind. The view should have functions that the controller can call with either the model as a parameter or already passed in advance.

  • Object.create(controllerPrototype); <- Any reason you are eschewing new and prototype ?

  • Not that it truly matters but, method === "model" will not catch new String('model')

  • args = [].slice.call(arguments, 1); works as well as the ternary

All in all, I think this could use some more polishing.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.