JavaScript: The Right Way(jstherightway.org) |
JavaScript: The Right Way(jstherightway.org) |
There's a fair amount of typo's and grammar mistakes however.
What would you remove or add?
More importantly, what isn't it good at - for instance, some of the bigger frameworks are tricky to integrate if you don't want to hand them control over the whole page.
Would be cool to automatically change the URL to the anchor as I scroll.
:/
If you just need a choice, somewhere to start. I'll make some choices for you. Here's the zen selection. My view of the standard javascript library:
jquery ($)
underscore (_)
mustache ({{}}) (possibly accentuated by iCanHaz)
Backbone (Backbone)
And that's your basic starter pack. It's a confused ecosystem because it was only since about 2007 that people started to realise that javascript was an actual programming language and not just a stupid toy. I remember that year chatting with people on #javascript on IRC who simply didn't believe that I had an actual full time job writing server side javascript. the idea seemed too absurd. And so here we are, a mere 7 years later and we've pieced together a very humble ecosystem in the face of that level of bald hatred and misunderstanding. I remember it wasn't so long ago that flash was in a similar position, and not long before that, java, with its popular image being its slow crummy applets.
So, yes, in conclusion, you're right, and it's because it's young and every language goes through this stage.
Also, I would highly recommend adding an AMD loader like require.js to your "basics kit". Code organization gets very important very fast, and it helps to think modular-and-reusable from the beginning.
I suppose I could ask experienced developers on StackOverflow.... (Just kidding! "Stop! Silence! Nobody answer him! Not constructive! Not Constructive!")
I left my web agency role over a year ago to work on my start up with a friend so I've been in relative developmental isolation (lots of python and js, not so much keeping up with the latest trends). I came back to help out on a project recently and had to spend a day researching all the new tools. Particularly around package management, bower, grunt, yeoman, node, npm etc. A year ago a js lib was generally delivered as a js or min.js file. Now there's the whole system to sit around it; every project on github now seems to include the config for each of the different package managers. Even a vagrant file so you can get it all up and running on a brand new vm.
Something as simple as adding bootstrap to a project. It's not totally obvious when you first look at it. Where does the code go? How do you compile it in such a way that you don't touch the vendor src itself but you can customise it etc. I have a method that works for me, but I had to play around a little to figure out how to fit it to my needs.
Once I got through that stage I was really happy with where things were at. JS libs are nice and easy to install and version control now, dependency management with bower has worked really well for me so far. Grunt makes the automation side of development nice, contained and easily shared. All in all it's a really good transition.
I'm lucky in that I've been developing for the web since people were changing files in ultraedit and ftping to the live server to see the changes. When each new thing comes along I only have one new thing to learn. Step away for a year and suddenly there's a lot to figure out to get back in to it.
I can only imagine how daunting it could be for a complete newbie trying to find their way.
As ever these tools can be great, but you ultimately you really want to understand what goes on underneath so they don't limit you too much. Amazing talk on this subject posted by another HNer in a comment the other day - https://www.youtube.com/watch?v=ShEez0JkOFw
If you know you’re replacing many parts of your page with data you have to load, use a framework like angular, backbone, ember, …
If you visualize data, use d3.
If you need to animate some UI elements but don’t need much else, use jQuery.
Else use plain JS, maybe with some shortcuts like function $(sel) { return document.querySelector(sel) }. maybe with underscore.js if your love funtional programming.
In any case, strive to write idiomatic code. d3, jQuery, and many else e.g. have their own variant of $, select, querySelector, … – decide on one framework and use its way to write code.
Any you’ll want to find a way to compartimentalize code. https://github.com/mbostock/smash is good, and http://browserify.org/
All those frameworks may be nice, but as soon as the tools get a distraction, I think it is useful to step back. And JS doesn't really need anything to be useful.
In the browser yes , i would say there is no consensus as to what lib to use. Some hate jQuery others like it,but the kind of projects people do are very different on the client.
I'm not going to use the same framework for a game or a LOB app or an interactive "flash like" experience, and some framework will use a paradigm I may or may not like.
Also on the client frameworks and libs are totally monolitic because JS doesnt have a proper module system. Python and Ruby dont have this issue. So JS lib are always re-inventing the wheel.
Despite what people say , client side , there is not "one true way" to do stuffs( like using AngularJS for everything).
I personally use jQuery and Ractive because they are fast and easy to use. On bigger projects I use browserify to build my own stack instead of a monolitic framework.
1) Front-end applications tend to have wider variation in functionality requirements from the start (real-time, graphs, animations, forms, modals, pushstate). There isn't a "standard CRUD app" that people are building with front-end js, often js is just being used for a single effect or widget.
2) Performance tradeoffs are very tangible to end-users. For instance, in most cases, straight HTML is still lighter and snappier than an AngularJS app (until you click on something, then that's another tradeoff) but an app served by 2 rails servers will usually be indiscernible from one served by 1 sinatra server.
With rails, you can say that the performance penalties are worth productivity gains because those penalties are really only going to effect deployment costs, not the UX. With JS, every extra bit you add to your framework is going to effect UX.
For me, the biggest pain is still dependency management. Bower is great for pulling stuff in but then I have to blend it with requirejs or an AMD loader or hand-wire it into my html. Likewise, when I want to remove something I have to touch a bunch of files. I'd love to just be able to "bower install backbone" and get back to writing code but building such a feature would require bower to be able to understand my project's structure.
But in the end, I now have a way to build a nice draggable widget that can contain a menu in the draghandle. Now, to embed a Google Gadget in that (the menu is for setting UserPrefs) and make the OpenSocial API work... Oh joy.
If you're looking for popular tech, i think a good proxy is github stars.
While I don't think popularity alone is how you should chose a framework. It appears that AngularJS tops all except jQuery at this time.
http://www.google.com/trends/explore#q=angularjs%2C%20backbo...
"Backbone.js: The most popular JS client-site framework"
worry me. Most people I talk to avoid Backbone nowdays in favour of the other popular frameworks.
Also: some descriptions appear to be lacking/missing & you definitely should mention Grunt under "Helpers". At least it's a GH repo and I assume you'll accept PR's? ;)
I'm sure everyone's second Backbone app looks a lot better than their first, but it's still an underdesigned framework, IMO. I think Marionette does a lot of good in terms of adding those missing structures and best practices, though. And I'm also intrigued by the idea of replacing views with React components.
I read about the problems it has, but most of the devs just love it for being small and easy to use, which isn't the case with angular.
http://www.google.com/trends/explore#q=angularjs%2Cbackbonej...
Not sure the cause, perhaps the influx of engineering "computer science disciplined" brains into the frontend world. Or a panicked competitive race to achieve a nirvana dev environment. Or a tendency to think that workflows for large websites with countless modules is the best workflow for a one-page web app.
Overkill for the intended task, or cluttering your project with too much technology, is the thing to watch out for in the frontend aspect.
Layers of tools and libraries and plugins can easily get out of control and beyond a joke. Don't forget you've got a web app to build while you mess around with the "right way" to build it.
You do not need a cup-holder on your scaffolding.
Just using plain JS with Jquery, and perhaps some additional smaller specific plugins/libraires, is enough to do a lot of cool stuff if we're talking "javascript" projects. Then just get a good editor and off you go.
"We recommend you whenever possible to verify you code style with a Lint tool."
That's not all they need a Lint tool for!
I see there's a lot of great suggestions and critics here!
Since it's an open source project, you can feel free to open a pull request telling which resource you think it's better to be there, or which one should be removed. As well help me correct the grammar errors. I'm from Brazil, and my english is not that good.
I'm really glad about all the opinions, and for sure the site will be improved from now on.
Thanks!
[1] https://github.com/braziljs/js-the-right-way [2] "Based on a work at http://jstherightway.org"
Your page is split in half on the iPhone, check it out. Cheers.
http://eloquentjavascript.net/
I think Polymer (Google) and Brick (Mozilla) deserves a mention there as Web Components have a very high possibility of becoming the ultimate way for the future of web development.
Want to work with dates? Use a third party library (or use built-in 1995-style library). Want to format a string? Use a third-party library. Want to do X? Use a third-party library.
Coding in e.g Python feels completely different: almost everything I need is in the standard library.
A script on this page may be busy, or it may have stopped responding. You can stop the script now, or you can continue to see if the script will complete.
I've found a sweet spot for myself, at least for single page apps which is what I work on most of the time. jQuery, Require.js, text.js and JSLint. r.js for building release versions. Those, along with a small "view" class I wrote whose constructor takes an html string, finds all elements with a data-vid attribute and adds properties to itself pointing at those elements. Markup is kept in bite-size html files, loaded by require during development but compiled into the single js file for release. Also, I can't remember the last time I used a jQuery selector.
It's not mention lodash / underscore which is very elementary if you want to avoid reinvent the wheel.
The thing that really got me going? Typescript. Not only does it help with abstracting javascript syntax, but the intelisence made me not have to google every 20 characters i write.
I have an issue with the above statement. There are no compiled or interpreted "languages". There are only implementations.
There's nothing preventing someone from "compiling" it.
While there have been attempts to compile a language like Python down to native code, they are still very limited and not useful in a practical sense. Hence, Python is a de facto interpreted language, since its usable implementations are implemented as interpreters.
The same goes for JavaScript today. Given that all of the usable implementations are interpreters, JavaScript can currently be considered an interpreted language.
This hasn't been true for quite some time. SpiderMonkey and JavaScriptCore do include interpreters, but only use them while code is first running. If a function gets hot, they then JIT compile the code. V8 has no interpreter at all, only JIT compilers (and is certainly usable and in wide use). And finally SpiderMonkey even just added an AOT compiler for compliant asm.js code (which, while a subset, is indeed valid JS code).
JavaScript Patterns by Stoyan Stefanov (made the list under reading/books) is a must for anyone wanting to learn JavaScript the right way. The content volume is just right with great examples. Can be read in a week easily and get you rolling writing better code fast!
What would be an example of a language with weaker OOP capabilities?
PS: "The bad parts" section did not fit the page?
> What would be an example of a language with weaker OOP capabilities?
Something without the concept of objects built in? C?These new tools and frameworks are becoming numerous and popular precisely because for large, complicated front-end applications you can't just get away with using "plain JS with Jquery" without it becoming an unmaintainable mess and writing boilerplate DOM manipulation code all over the place. The only people I meet who have an aversion to these modern JS frameworks are front-end devs who refuse to learn them or server-side devs who hate javascript.
Nonsense. Maintainability is all about good documentation, neat code, sensible peer-reviewed code, a good dose of manual testing and a healthy injection of personal responsibility in seeing the project be the best it can be for your users.
I test my work manually on a bunch of test devices and computers I own. It doesn't take long and I get to see any browser specific display glitches that automation tools wouldn't pick up. It's a no-brainer, and you LEARN about the nature of browsers by testing manually.
I hate bootstrap, don't use it. I've made big website templates with lots of traffic, and smaller components and other things. I don't want bootstrap in the mix thanks. Whenever I have to maintain a project which someone else started with bootstrap, I sigh and dive into the ridiculous use of simulated rows and columns, and the crappy CSS overrides meant to give us flexibility, but actually giving us not much at all.
So how do other developers like my work when they have to maintain it? Well, I leave them something SIMPLE and well documented to decipher, so they can get straight to work doing the maintenance and getting on with business. No learning curves of countless toolsets and pre-processing junk. Just the basics. I've had no complaints.
Frontend is very much concerned with visual elements and interaction. I don't know much about server side except for what I find behind the projects I work on and fetch with AJAX or other means... such and such data feeds, this or that outputs etc. I'm more interested in user interfaces and how a web app or webpage "feels". I know exactly what to do when something doesn't feel snappy, or a graphic flashes or renders poorly on load. I know what to do because I haven't sold my soul to a framework that promised me the world, but delivered mediocrity.
Each to their own.
That's a rather negative view, and I don't think it's entirely realistic. I am involved with multiple long-running web projects that have been using things like (real) MVC architectures and single-page applications since long before any of these trendy JS frameworks existed. The projects have been built using sound software development principles and usually a small number of carefully chosen supporting tools and libraries. They are still doing their jobs just fine, and they have stood the test of time in terms of maintainability.
The only people I meet who have an aversion to these modern JS frameworks are front-end devs who refuse to learn them or server-side devs who hate javascript.
Please consider that there is at least one more option, which I imagine applies to many of the older readers here. Some of us have seen before the rapid evolution of an ecosystem as a programming language takes off, and we simply don't buy the hype.
We saw the arrival of Java, with all its garbage collected wonder. Then we saw the nightmarish descent into over-engineered hell that followed.
We saw the arrival of Boost for C++ and the potential of community-developed, peer-reviewed libraries. Then we noticed that in 2014, C++ still doesn't even have basic features for working with filesystems or rendering a simple UI as standard.
We saw the evolution of very dynamic "scripting" languages like Python and Ruby into widely used server-side languages for real applications. Then we saw people trying to maintain those applications a few years later, when they weren't prototypes any more and when quality and performance mattered.
More recently, we've seen front-end web development shift toward JavaScript and move away from the likes of ActiveX controls, Flash plug-ins and Java applets, as language support became more consistent across browsers and some real effort went into improving performance. Today we are also seeing the inevitable explosion of the surrounding ecosystem as JS becomes a tool for building more heavyweight software, signalled by the arrival of vast numbers of different automation tools and libraries and frameworks.
But with the cynicism of the industry veteran, we also see that many of these new tools will do 80% of a job easily but make the last 20% much more painful. We know that some are merely short cuts aimed at people who now work in front-end development but whose backgrounds aren't necessarily in programming and who haven't yet developed their general software development skills in that way; there's nothing wrong with that, of course, but such tools have no (or negative) value to those who have moved beyond the level where they are helpful. Most of all, we know that five years from now, as the ecosystem evolves standards and consolidates, it is highly unlikely that most of the currently trendy tools will still be as effective and well maintained as they are today, but that like the five-year-old projects some of us work with today, someone will still have to maintain those projects after all the hypesters have moved on.
This doesn't mean that we somehow refuse to learn new technologies. It just means that if I can see what looks like a fire, and it feels as warm as I expect when I get close enough to examine it, I'm not still going to jump in for a few minutes just to see if it's really as hot as it looks because I read on a web site somewhere that all the cool kids were doing it.
Now get off my lawn. :-)
For a tiny app, sure, maybe it's not so bad to just use jQuery. But if you're working on something with a very rich front-end architecture, a framework gives you a very necessary...er...framework to use to eliminate boilerplate and organize your work. Not only that, but you get to minimize your code debt to your actual business logic, not the plumbing.
This debate feels loose. We are lucky to have so many options. It all depends what project you're working on.
I prefer rich possibilities in design and functionality rather than rich possibilities in minimising how much I have to think/solve/innovate. I don't want a crutch, I want a turbo boost of creativity in web development... and that comes from hard work, not automating your life.
These tools are often about slapping the same old stuff down day in day out. Where's the fun in that?
Am I re-inventing the wheel? Spreading boilerplate code around in a mess? No I am not. My boilerplate code is nicely organised and easily deployed thanks very much! And the wheels.. well, the wheels I make are lightweight, and I know and trust them inside out. They're my wheels. I made them, and they stand out from the crowd.
Perhaps not, but plenty of us write them using a lightweight framework like Flask rather than heavyweights like Django, and even in a relatively mature field like ORMs, there are downsides to using them that sometimes outweigh the advantages.
For a tiny app, sure, maybe it's not so bad to just use jQuery.
I've got ~100,000 lines (before minimising etc.) and ~5 years of successful maintenance "just using jQuery" for one web app that I can quickly check from here that is still going strong. I think most of us would agree that's not a tiny single page application.
(At least at first, before it starts to matter that it's no help in terms of organizing a larger application. :)
No, they are aimed at people who have worked in front-end development for a long time and whose backgrounds are in programming precisely because they are sick of reinventing the wheel every time they start a new project and are able to understand the technical value behind concepts modern frameworks give us like dependency injection, unit/e2e testing, modularity, and separation of concerns. The myth that front-end developers are not "necessarily from a programming background" is poisonous thinking and it unfortunately usually comes from older programmers or non-front-end developers; the very people who could help guide the front-end world into maturity.
You write as if we didn't understand the technical value of those things before a couple of years ago when the current crop of JS frameworks arrived, and as if using one of those frameworks is somehow necessary to achieve them.
In reality, everything you mentioned has been widespread in other software development fields for decades. It's stuff the junior guy on the team learns in his first few months.
Moreover, I think there is a reasonable argument that of your chosen examples, testing is the only one that has sufficiently common requirements for building large but general tools to be worthwhile. The other design principles are valuable, but how best to use them will be highly project-specific, and therefore any widely useful framework will tend to be over-engineered and over-generalised for most applications. See also the Java nightmare I mentioned previously, the trend toward lightweight frameworks on the server side, etc.
Edited to add:
The myth that front-end developers are not "necessarily from a programming background" is poisonous thinking and it unfortunately usually comes from older programmers or non-front-end developers; the very people who could help guide the front-end world into maturity.
There is a difficulty in the industry that significant numbers of people are coming into this kind of front-end development from some sort of HTML/CSS and/or design background. They used to use a bit of JS here and there that they copied off a demo site to get their buttons to animate, and now they're being asked to move into more of a mainstream programming role, but neither they nor the people asking them necessarily appreciate what that involves.
That's not anyone's fault. It will be fixed over time as the industry matures, both because knowledge will spread and because more specialised roles than "front end developer" will probably evolve. But right now, today, it is the situation in large parts of the industry.
I looked it up, and it seems to be missing quite a bit, including the .noConflict() option, which I seem to have to use quite a bit when working on others' code....
"The ecosystem isn't (that) confusing, just use X Y and Z"
- "No, use X Y and A"
-- "Who uses Y? Use C"
Myth: confirmed!
http://requirejs.org/docs/optimization.html
Your comment makes it seem like it doesn't.
IME any application which grows that large without using a framework ends up inventing its own. If the code's maintainable by people other than its original author you've presumably separated the routing code from the template rendering code (and have a module that's effectively a generic template renderer) from the backend service access (and again, probably have a module that's some kind of generic service access layer). You'll have a set of utility functions that's more or less equivalent to underscore. And so on.
I don't know. That project has no direct equivalent to any of the current frameworks, other than approximately following MVC for its overall architecture and using message passing/publish-subscribe in various places to keep things modular and testable.
I imagine you could achieve some of the same behaviour using data binding tools in modern frameworks, but either way you're typically looking at a little boilerplate to connect things up in the first place and then one-liners to connect individual fields from a model to the corresponding parts of the DOM. It's not clear whether there would be any savings at all, but if there were, they wouldn't be significant for any framework I've considered so far. In any case, that represents a tiny fraction of the overall code base for this application.
IME any application which grows that large without using a framework ends up inventing its own.
All software has an architecture, sure. As with any framework vs. library debate, you're trading off the benefits of having some of that architecture predetermined for you and having some of the laborious plumbing work dealt with automatically against the overhead of managing a major external dependency and any extra costs you incur if you need to do something that goes against its natural capabilities. Sometimes using a framework is a win, and sometimes it isn't.
If the code's maintainable by people other than its original author you've presumably ...
You've made some assumptions there about how web apps work, but they aren't necessarily true.
For example, much of the complexity in this particular UI comes from providing many views of the same underlying model. Some of them are graphical rather than text/form based. Some of those rely on intermediate cached data structures for their source data because rerendering an entire view from the raw data in the model can be prohibitively slow. We have an efficient and simple architecture using the same basic principles as MVC for dealing with this kind of caching. We'd derive very little benefit here from the kinds of data-binding and template-rendering functionality found in the major frameworks.
Just to be clear, libraries designed to render interactive graphical page elements efficiently, allowing for portability across browsers and different image/scene formats, are another question entirely. I'm not arguing for reinventing the wheel or arguing against using tried-and-tested code from external sources. I'm just arguing that it should only be done when the benefits outweigh the costs, and that this won't necessarily be the case when considering whether or not to use one of the modern JS frameworks as the foundation for your web app.
Why? I've worked on non-web software projects 1-2 orders of magnitude bigger that didn't need anyone's magical framework to develop and maintain a sound architecture. How come we can't just use good design that fits the project if the software we're writing happens to be the front end for a web app?
It's about picking the right tool for the job, the same as always. For some projects and some development teams, I'm sure these modern JS frameworks and tools have proved valuable and will continue to do so. On the other hand, we're currently planning another web app that is expected to grow at least as big as the one I described earlier, and no-one on the team is seriously considering using any of the current crop of front-end frameworks. That's not because we don't know what they do or how they work. It's simply because none of us thinks it's a smart idea to build such a large and hopefully long-running project with a fundamental dependency on the kind of technology that typically measures its time at the top in months.
jQuery does that.
Underscore gives you array extras in every browser, even ones that don't have array extras.
Mustache because let's face it, javascript doesn't have any nice way of doing templating or string formatting or anything. not without resorting to doing things that might not work in every browser.
and backbone because, while some of the newer browsers and newer versions of ecmascript provide facilities for monitoring and reacting to changes, and doing data binding like things natively, not all browsers do.
now I'll admit that backbone is not as new and shiny as say, that om/react/clojure stack making the rounds on hacker news, it's been around for a few years and it's still here. So that's something.
Well, not a valid usage either given the context. From the examples you give, the only fitting one is Backbone, and even that it's stretching it a little (nobody uses the ES6 monitoring/reacting facilities directly atm, so it's not like Backbone gives them compatibility for them, it's like it provides them in the first place).
_(fn).bind(this), anyone?
Very soon to be IE8.
… a framework.