background preloader

Understanding Design Patterns in JavaScript

Understanding Design Patterns in JavaScript
Today, we're going to put on our computer science hats as we learn about some common design patterns. Design patterns offer developers ways to solve technical problems in a reusable and elegant way. Interested in becoming a better JavaScript developer? Then read on. Republished Tutorial Every few weeks, we revisit some of our reader's favorite posts from throughout the history of the site. Solid design patterns are the basic building block for maintainable software applications. A design pattern is a reusable software solution Simply put, a design pattern is a reusable software solution to a specific type of problem that occurs frequently when developing software. patterns are proven solutions to software development problems patterns are scalable as they usually are structured and have rules that you should follow patterns are reusable for similar problems We'll get into some examples of design patterns further in to the tutorial. Take a look at the three examples above.

Basic Branching and Merging Let’s go through a simple example of branching and merging with a workflow that you might use in the real world. You’ll follow these steps: Do work on a web site.Create a branch for a new story you’re working on.Do some work in that branch. At this stage, you’ll receive a call that another issue is critical and you need a hotfix. You’ll do the following: Switch to your production branch.Create a branch to add the hotfix.After it’s tested, merge the hotfix branch, and push to production.Switch back to your original story and continue working. Basic Branching First, let’s say you’re working on your project and have a couple of commits already. You’ve decided that you’re going to work on issue #53 in whatever issue-tracking system your company uses. $ git checkout -b iss53 Switched to a new branch "iss53" This is shorthand for: $ git branch iss53 $ git checkout iss53 You work on your web site and do some commits. $ vim index.html $ git commit -a -m 'added a new footer [issue 53]' Basic Merging Note

When to Avoid the Descendant Selector Those of us who have started using modular or object-oriented CSS principles have learned to avoid, as much as possible, the use of the descendant selector (or, more accurately, the descendant combinator). A selector that uses the descendant combinator looks like this: Everything you see before the opening curly brace is the selector. The combinator in this case is the space character. Now that we have a technical understanding of the selector itself, let’s discuss what to keep in mind when deciding to use this selector. Why is the Descendant Selector Restrictive? To understand why the descendant selector can cause problems, look at the example I used above. That’s pretty strict. This is outlined in the OOCSS wiki, when discussing the separation of “container and content”. Does OOCSS Discourage Use of the Descendant Selector? This question is covered in the OOCSS FAQ. No–descendant selectors are not discouraged, but putting them too high in the DOM tree is. No Dogma Here

JSPatterns.com OK Computer Solutions Setting Up A Staging Environment It's common practice to work locally on a project and push revisions to a production server, but the step that people often skip is the staging server. A staging server is a mix between production and development; you get to test your app as if it were in production. Let's review some of the issues that you'll have to consider, as well as the steps needed to replicate a production Platform as a Service (PAAS). It has happened more than once: I push a revision of my app into production, only to find a problem after it's public. These issues can be as simple as forgetting to add images to your repository, or it might be as large as changing a database's structure locally and forgetting to update the production database. Staging environments not only catch human errors, but also software-related issues. You can find and fix these issues because the staging area has the same software as your production environment. So how do you go about setting a staging server up? Apache 2.2PHP 5.4Composer

Learning JavaScript Design Patterns Design patterns are reusable solutions to commonly occurring problems in software design. They are both exciting and a fascinating topic to explore in any programming language. One reason for this is that they help us build upon the combined experience of many developers that came before us and ensure we structure our code in an optimized way, meeting the needs of problems we're attempting to solve. Design patterns also provide us a common vocabulary to describe solutions. In this book we will explore applying both classical and modern design patterns to the JavaScript programming language. Target Audience This book is targeted at professional developers wishing to improve their knowledge of design patterns and how they can be applied to the JavaScript programming language. Some of the concepts covered (closures, prototypal inheritance) will assume a level of basic prior knowledge and understanding. Acknowledgments Credits Reading Patterns are not an exact solution. Creational Design Patterns

MonoDevelop - MonoDevelop Testing Like a Boss in Laravel: Models If you're hoping to learn why tests are beneficial, this is not the article for you. Over the course of this tutorial, I will assume that you already understand the advantages, and are hoping to learn how best to write and organize your tests in Laravel 4. Version 4 of Laravel offers serious improvements in relation to testing, when compared to its previous release. This is the first article of a series that will cover how to write tests for Laravel 4 applications. Setup In-memory database Unless you are running raw queries on your database, Laravel allows your application to remain database agnostic. With Sqlite, we can set the database connection to :memory:, which will drastically speed up our tests, due to the database not existing on the hard disk. In short: an in-memory database allows for fast and clean tests. Within the app/config/testing directory, create a new file, named database.php, and fill it with the following content: Before running tests The Tests Post model Post tests

A deep dive and analysis of the JavaScript Module pattern Monday, March 15th, 2010 <>p>Ben Cherry has a really nice detailed analysis of the module pattern. He starts with the simple pattern that Crock-y documented back in the day….. and then goes on to discuss augmentation (loose and strict) and then deeper into some cool patterns: Nice work Ben! Strategies for Testing IE on Different Platforms You might believe that web developers who use Windows as their primary OS have it easy, when testing Internet Explorer; simply click on the blue "E" and go to town. That might be true if they only had to worry about the version of IE on their PC, but we all know that's not the case. Depending on your project's needs, you may need to ensure compatibility from IE6 through IE10, which certainly poses some challenges. How do we deal with this? Now if you're on Windows, you definitely have a leg up because at the very least, you can natively test a specific version of IE, but it's a whole different ballgame if you're on a Mac or Linux box. What I want to do is explore the various ways of testing Internet Explorer across popular platforms, and offer suggestions on how to lower the friction of testing for multiple versions of the browser. Versions, Versions, Versions... We all know testing is hard. Virtual machinesVirtualized browser sessions My First Goto Tool for Testing Virtual Machines

Writing Testable JavaScript The engineering culture at Twitter requires tests. Lots of tests. I haven’t had formal experience with JavaScript testing before Twitter, so I’ve been learning a lot as I go. In particular, a number of patterns I used to use, write about, and encourage have turned out to be bad for writing testable code. So I thought it would be worthwhile to share a few of the most important principles I’ve developed for writing testable JavaScript. Avoid Singletons One of my most popular posts was about using JavaScript Module Pattern to create powerful singletons in your application. For example, consider the following singleton module (contrived example, of course): With this module, we may wish to test the foo.bar method. When running this test suite, the assertion in the length test will fail, but it’s not clear from looking at it why it should. Now, your test suite will look like this: Avoid Closure-based Privacy Another pattern I used to promote is real private members in JavaScript. Conclusions

Event Based Programming: What Async Has Over Sync One of JavaScript's strengths is how it handles asynchronous (async for short) code. Rather than blocking the thread, async code gets pushed to an event queue that fires after all other code executes. It can, however, be difficult for beginners to follow async code. I'll help clear up any confusion you might have in this article. Understanding Async Code JavaScript's most basic async functions are setTimeout and setInterval. As expected, the console outputs "a", "b", and then 500 ms(ish) later, we see "c", "d", and "e". "This API does not guarantee that timers will run exactly on schedule. Interestingly, a timeout will not execute until all of the remaining code in a block has executed. The Event Loop is a queue of callback functions. Ajax Asynchronous JavaScript and XML (Ajax) forever changed the landscape of JavaScript. Asynchronously retrieving data with jQuery's $.ajax is an easy cross-browser process, but it's not immediately evident as to what exactly happens behind the scenes.

addyosmani/essential-js-design-patterns Beginning Test-Driven Development in Python Test-driven development (TDD) is a process that has been documented considerably over recent years. A process of baking your tests right into your everyday coding, as opposed to a nagging afterthought, should be something that developers seek to make the norm, rather than some ideal fantasy. I will introduce the core concepts of TDD. The whole process is very simple to get to grips with, and it shouldn't take too long before you wonder how you were able to get anything done before! There are huge gains to be made from TDD - namely, the quality of your code improving, but also clarity and focus on what it is that you are trying to achieve, and the way in which you will achieve it. TDD also works seamlessly with agile development, and can best be utilized when pair-programming, as you will see later on. In this tutorial, I will introduce the core concepts of TDD, and will provide examples in Python, using the nosetests unit-testing package. What is Test-Driven Development? Success! Or:

Related: