views:

69

answers:

1

We are always trying to improve on our ability to apply our skills to solve a problem. Software engineering principles have significantly helped my ability to write higher quality code. This includes testing, modularization, using OO where appropriate, etc.

Here's an example of how I achieved some modularization in my JS. Maybe it is a bad way to achieve this, but it serves as an example of what I mean and contains a few questions of its own.

framework.js

Framework = {
    CurrentState : {
        IsConfigurationLoaded : false,
        IsCurrentConfigurationValid : false,
        Configuration : undefined  //undefined .. good? bad? undefined?
    },
    Event : {
        //event lib
    }, 
    //you get the idea
}

Question:

In what ways do you apply software engineering principles to improve the readability, maintainability, and other quality attributes of your JS?

Other Related (more specific) Questions to help in answering:

I had once written a simple JS unit testing framework, which had simple asserts and a test helper method taking a lambda. What are your thoughts on unit testing javascript?

How important is defining the boundary between your code and framework?

JS is mostly used in a browser or in a website. Does this reduce/nullify certain concerns?

Do you suggest usage of Classes and OO principles?

Usage of undefined and/or null? Should it be forbidden?

Usage of try/catch? Suggested?

When do you go from JSON to classes? Do you use Util methods that operate on the data?

Usage of prototype? Suggested? What is a good case where you wouldn't use it?

+1  A: 

hi scoobie,

in large project i tend to differ between model-, control- and view-files ([mvc-pattern][1]).

the model-file contains everything concerning data especially my class (OOP). an example for a model-file could be:

function myClass(){
   //private variable
   var variable=5;

   //public variable    
   this.newVariable = 10;

   function myFunction() {
      //do some stuff
      alert("my function");
   }    

   //public stuff
   return {
      myPublicFunction: myFunction
   }
}

the view-file contains everything concerning to the layout and view and the control-file is filled with the stuff concerning to data-handling. control-file uses the class declared in the model-file and works with it. so the view only needs to include the control-file and call the functions it needs for the layout.

but in general it's quite different to generalize. i like the oo-pattern and trie to use is, if it makes sense. but i have only experience with iPhone-development, so i am not able to say something concerning web dev.

  [1]: http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller
mkind