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've done quite a lot of javascript over the years and I'm using a more object-oriented approach, specifically with the module pattern. Which kind of approach do you do use to avoid a bigger code-base to becoming spaghetti. Are there any "best approach"?

share|improve this question
1  
How would you know if you've written readable and easily maintainable code? - Your peer tells you after reviewing the code. –  gnat Oct 14 '12 at 11:01
    
But if a developer think stuff gets complicated after 3 lines of code? What to do then? –  marko Oct 14 '12 at 11:09
2  
You cannot determine this yourself because you know more as the author than the code says by itself. A computer cannot tell you, for the same reasons that it cannot tell if a painting is art or not. Hence, you need another human - capable of maintaining the software - to look at what you have written and give his or her opinion. The formal name of said process is "Peer Review" (quote source - top voted answer to a question which is same as yours) –  gnat Oct 14 '12 at 11:11
    
That would be awesome if we did at work. –  marko Oct 14 '12 at 17:29

3 Answers 3

up vote 7 down vote accepted

Dan Wahlin gives specific guidance about avoiding function spaghetti code in JavaScript.

Most people (including myself) start out writing JavaScript code by adding function after function into a .js or HTML file. While there’s certainly nothing wrong with that approach since it gets the job done, it can quickly get out of control when working with a lot of code. When lumping functions into a file, finding code can be difficult, refactoring code is a huge chore (unless you have a nice tool like Resharper 6.0), variable scope can become an issue, and performing maintenance on the code can be a nightmare especially if you didn’t originally write it.

He describes a few JavaScript design patterns that give structure to the code.

I prefer the revealing prototype pattern. My second favorite is the revealing module pattern, which differs slightly from the standard module pattern in that you can declare public/private scope.

share|improve this answer
    
Yeah, it is kind of what I'm doing, when building bigger stuff. –  marko Oct 14 '12 at 17:38
1  
But where I saw the named pattern for the first time, must have been in Javascript Patterns - (amazon.com/JavaScript-Patterns-Stoyan-Stefanov/dp/0596806752/…). –  marko Oct 15 '12 at 17:40
    
I hate to be the guy to say "just use a framework"... but for me, I'm always thinking about the guy coming after me, and to have a well-tested, documented, and community supported framework... it's a no brainer to me. My favorite is Javascript MVC (soon to be CanJS). It has scaffolding scripts and a very intuitive dependency manager (steal()) which allows you to have a very well structured application while compiling scripts down to 1 or 2 minified files. –  Ryan Wheale Oct 15 '12 at 23:33

A separation between working code and deployed code is helpful. I use a tool to Combine and Compress my javascript files. So I can have any number of modules in a folder, all as separate files, for when I am working on that specific module. But for deploy time, those files get combined into a compressed file.

I use Chirpy http://chirpy.codeplex.com/ , which also supports SASS and coffeeScript.

share|improve this answer

Code standards in general are useful.

That means:

Modules are definitely necessary. You also need consistency in the way "classes" are implemented, i.e. "methods on the prototype" vs "methods on the instance". You should also decide which version of ECMAScript to target, and if you are targeting i.e. ECMAScript 5 use the provided language features, (e.g. getters and setters).

See also: TypeScript, which could help you standardize e.g. classes. A bit new at the moment, but I don't see any disadvantages in using it since there is hardly any lock-in (because it compiles to JavaScript).

share|improve this answer
    
I think Typescript is unnecessary. –  marko Oct 15 '12 at 18:28
1  
@marko: Unnecessary for what application? Would you implement a 25 kloc compiler without static typing? –  Janus Troelsen Oct 15 '12 at 20:25

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.