• Coding
  • Maintainable Javascript?

So I was wondering, does any of you guys write maintainable Javascript code? I find it very hard to write clean Javascript code. For some reason, I cannot treat JS like any other language. I feel the language itself promotes dirty code.

Do you guys feel the same way?
Are there any techniques you find useful to make your Javascript readable and maintainable?
I came across this video today seemed interesting, I just started it watching it now :)
AymanFarhat wroteI came across this video today seemed interesting, I just started it watching it now :)
I watched this video today. Interesting ideas but I still couldn't materialize most of them.
I can't claim to write good Javascript, I haven't been using the language that long. However, I have come across some good guidelines that help me keep things clear.

First, take a look at the Google Javascript Style Guide. It's got some pretty good guidelines. Always run your code through JSLint, it's strict and it can keep your code in shape.

Actually, Douglas Crockford's Code Conventions for the JavaScript Programming Language is a very important read. I think it's more clear than Google's hints.

Finally, I think the most important guideline is: do not pollute the global namespace. It's ridiculously easy to do that in Javascript, and the consequences aren't always clear. I don't particularly enjoy using the module pattern, but it works.

These are just my initial observations from a couple of months of using the language, if anyone has some other insights and corrections please feel free to share.
saeidw wroteActually, Douglas Crockford's Code Conventions for the JavaScript Programming Language is a very important read. I think it's more clear than Google's hints.
To be honest I'm completely dumb when it comes to JavaScript. Yesterday I watched ECMAScript 5: The New Parts by Douglas Crockford and I was shocked that even he thought JS is stupid when he first started using it - that was kind of a relief to me :)

But yeah, those links you've shared are definitely worth looking at. I only knew about JS closures yesterday from Doug's talk. I guess those should save me from breaking existing code everytime I write more JS! This keeps haunting me over and over again! I'm hoping I would once have the time (and courage?) to dig into my JS code and refactor it once and for all. It's actually sprinkled all over the place!
If you want to have a detailed look at the quirky parts of JavaScript, check out the JavaScript Garden. A great resource that saved me on a couple of occasions. occasions.
Someone in another thread mentioned backbone.js. I'd use that if I had a big javascript project.
I usually create my own namespaces and separate code into different files. This could help but it's definitely not the best solution