Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am starting a new job soon as a frontend developer. The App I would be working on is 100% Javascript on the client side. all the server returns is an index page that loads all the Javascript files needed by the app.

Now here is the problem:

The whole of the application is built around having functions wrapped to different namespaces. And from what I see, a simple function like rendering the HTML of a page can be accomplished by having a call to 2 or more functions across different namespace...

My initial thought was "this does not feel like the perfect solution" and I can just envisage a lot of issues with maintaining the code and extending it down the line.

Now I would soon start working on taking the project forward and would like to have suggestions on good case practices when it comes to writing and managing a relatively large amount of javascript code.

share|improve this question
    
If I was faced with a large amount of javascript I was going to have to maintain and move the design of it forward, my first step would be learning and practicing a lot of LISP. LISP forces you to write code the way that Javascript should be written with the excessive use of closures to do everything around maintaining state and composing the system together. –  Jimmy Hoffa Sep 25 '12 at 3:53
2  
Whenever I read a question about maintainability of a larger codebase, my first reflex is to recommend Michael Feather's great book "Working effectively with legacy code". amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/… –  Doc Brown Sep 25 '12 at 5:50
add comment

closed as not constructive by gnat, Mark Trapp, Jim G., Walter, ChrisF Oct 6 '12 at 21:50

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

up vote 3 down vote accepted

This is a very good question, and a common problem in advancing JavaScript architecture.

It sounds to me like you are describing the situation of "tight-coupling".

Once functions become objectified, the tendency is to reference these wonderful objects directly, from object to object, across namespaces even. Because it is easy right?

var Object1, Object2 = {};

Object1.somefunction = function(){
   //Tight Coupling!!
    Object2.functionCall();
}

It is easy, but these seemingly innocent hard-references gang up, to make you sad when you have to remove or replace objects. That happens a lot in JavaScript, so understanding tight-coupling is key to making a JS codebase maintainable.

Here are some other thoughts:

1 - If your objects are not already communicating by - triggering and listening to events; they should be. This is the solution to hard references.

2 - Design Patterns. There are many challenges that have already been solved out there, the standardized reusable solutions - are Design Patterns.

Understanding where the patterns are helps you focus on what solutions may make sense. One pattern for communicating across objects is called Publisher/Subscriber, or PubSub.

3 - These things help with maintainability: MVC with a router, Templates - data binding, AJAX - through a Proxy or Delegate objects.

4 - Look for frameworks and libraries that solve the cross-browser problems for you. Don't re-invent the wheels you don't need to know more about. Depending on your environment, some may frameworks may become obvious choices.

5 - Think about enhancement and optimizations like build systems, minification, linting, tdd, etc.

6 - Also, most important of all: Module Loaders. Take a look at Require.js It is a very nice way to break JS into modules, and then load them all in an optimized way.

Hope that helps.

share|improve this answer
add comment

I would recommend to look at JavaScript module management and compression, also called Steal JS which is part of the great JavaScript MVC framework. This could be generally interesting for larger JS applications. It can load module files dynamically during development and for production you can create one compressed JavaScript file (it can do CSS, too).

Another alternative to look is RequireJS. however, I did not explore it in depth. just keep that option in mind.

share|improve this answer
1  
+1 I've also heard buzz on Steal.js. And thanks for the insight on it. –  ClintNash Oct 6 '12 at 23:51
add comment

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