I generally try not to get too distracted by the code quality in EatDifferent and focus on user-facing quality instead, but after a while, it hurts my head knowing that my code is messy -- and makes me not want to mess with the code further. So, I spent yesterday spring cleaning my JavaScript.
One of the big improvements was to take my smattering of global functions and put them in a namespace. There are a lot of ways to namespace in JS, but I opted for the module pattern described in this article (#3).
Here's the basic template for each module's JavaScript file -- notice how this technique lets me create functions in each module that are only used inside the module, and aren't exposed outside of it.
var ED = ED || {}; ED.util = (function() { function doSomethingPrivate() { } function doSomething() { doSomethingPrivate(); } return { doSomething: doSomething } })();
I ended up with 6 JS modules used across the web and mobile (PhoneGap) version of the app.
ED.util | Utility functions, app-independent |
ED.data | Global data constant definitions |
ED.models | Classes representing data from the server DB |
ED.shared | App functionality shared by web and mobile |
ED.web | Web specific functionality |
ED.mobile | Mobile specific functionality |
I could have put the first 4 of these in "shared" but I like the conceptual division, and I'm concatenating them together before serving them to the user, so it doesn't hurt to have multiple files.
Now that my code is cleaner and more manageable, I can more confidently iterate on user-facing features!
No comments:
Post a Comment