Tuesday, June 20, 2017

A Beginner’s Guide to Testing Functional JavaScript

Functional programming and testing. Maybe you've given them a try in isolation, but somehow you never made either a part of your regular practice. They may sound innocent by themselves, but together testing and functional programming can create an irresistible temptation, almost compelling you to write cleaner, tighter, more maintainable code.

Testing Functional JavaScript — A crash test dummy sits at a computer with a mug of coffee

Well the good news is that working with both techniques together can offer some real advantages. In fact, once you've tasted how sweet this combination can be, you might find yourself as addicted to it as I am, and I'd be willing to bet that you'll be coming back for more.

In this article, I'll introduce you to the principles of testing functional JavaScript. I'll show you how to get up and running with the Jasmine framework and build out a pure function using a test-driven approach.

Why Test?

Testing is about making sure that the code in your application does what you expect it to do, and keeps doing what you expect it to do when you make changes so you have a working product when you're done. You write a test that defines your expected functionality under a defined set of circumstances, run that test against the code, and if the result isn't what the test says it should be, you get a warning. And you keep getting that warning until you've fixed your code.

Then you get the reward.

And yes, it will make you feel good.

Testing comes in a lot of flavors, and there is room for healthy debate about where the borders are drawn, but in a nutshell:

  • Unit tests validate the functionality of isolated code
  • Integration tests verify the flow of data and the interaction of components
  • Functional tests look at the behavior of the overall application

Note: Don't get distracted by the fact that there's a type of testing called functional testing. That's not what we'll be focusing on in this article about testing functional JavaScript. In fact, the approach you'll use for functional testing of the overall behavior of an application probably won't change all that much whether or not you're using functional programming techniques in your JavaScript. Where functional programming really helps out is when you're building your unit tests.

You can write a test at any point in the coding process, but I've always found that its most efficient to write a unit test before writing the function you're planning to test. This practice, known as test-driven development (TDD), encourages you to break down the functionality of your application before you start writing and determine what results you want from each section of code, writing the test first, then coding to produce that result.

A side benefit is that TDD often forces you to have detailed conversations with the people who are paying you to write your programs, to make sure that what you're writing is actually what they're looking for. After all, it's easy to make a single test pass. What's hard is determining what to do with all the likely inputs you're going to encounter and handle them all correctly without breaking things.

Why Functional?

As you can imagine, the way you write your code has a lot to do with how easy it is to test. There are some code patterns, such as tightly coupling the behavior of one function to another, or relying heavily on global variables, that can make code much more difficult to unit test. Sometimes you may have to use inconvenient techniques such as "mocking" the behavior of an external database or simulating a complicated runtime environment in order to establish testable parameters and results. These situations can't always be avoided, but it is usually possible to isolate the places in the code where they are required so that the rest of the code can be tested more easily.

Functional programming allows you to deal with the data and the behavior in your application independently. You build your application by creating a set of independent functions that each work in isolation and don't rely on external state. As a result, your code becomes almost self-documenting, tying together small clearly defined functions that behave in consistent and understandable ways.

Functional programming is often contrasted against imperative programming and object-oriented programming. JavaScript can support all of these techniques, and even mix-and-match them. Functional programming can be a worthwhile alternative to creating sequences of imperative code that track the state of the application across multiple steps until a result is returned. Or building your application out of interactions across complex objects that encapsulate all of the methods that apply to a specific data structure.

How Pure Functions Work

Functional programming encourages you to build your application out of tiny, reusable, composable functions that just do one specific thing and return the same value for the same input every single time. A function like this is called a pure function. Pure functions are the foundation of functional programming, and they all share these three qualities:

Continue reading %A Beginner’s Guide to Testing Functional JavaScript%


by M. David Green via SitePoint

How to Use the Google Cloud Vision API in Android Apps

Yamm – MegaMenu for Bootstrap 3

Yamm is a lightweight and pure CSS megamenu that uses the standard navbar markup and the fluid grid system classes from Bootstrap 3.


by via jQuery-Plugins.net RSS Feed

How to Backup Your WordPress System

Few people care about backups until it's too late. The question "do we have backups?" is only asked after a catastrophic loss has occurred. Backups are essential. If you fail to prepare then prepare to fail.

The five steps to backup success:

  1. Educate your boss/client
    If they understood the technicalities of web development, they wouldn't need you. Explain the situation in simple monetary terms, e.g. if our server crashed today, we would lose everything. It would take M months to rebuild the system at a cost of $X and we could lose $Y in business. A viable backup system would cost $Z to implement and we would be back within N hours. I'm going presume you've terrified your boss/client and they're willing to proceed.
  2. Identify what you need to backup
    Backing up a WordPress system is a little complex because it depends on specific files and a database. A backup will only be viable if you have up-to-date copies of both.
  3. Implement appropriate backup technology
    This will depend on the technologies you're using and the service you offer. For example, a weekly backup may be suitable for a blog receiving a few updates a month. At worst, you could lose an article or two. A WordPress-powered online shop may need continual backups to ensure customer orders cannot be lost.
  4. Automate
    Backups should be taken out of human hands. Consider a system which requires a single click once a day to initiate the backup. You will start with good intentions but it will soon become a chore. What happens when you go on vacation? What if you leave the company? Computers are very good at scheduling so let them handle the work.
  5. Verify and test your backup
    Your system is only as good as the last successful backup. Ideally, you should automate verification of the backup's integrity, move backup files off-site, periodically test your restore procedures, and retain previous backups for a reasonable time (three old versions is typical).

Continue reading %How to Backup Your WordPress System%


by Craig Buckler via SitePoint

Customise your One Page Website or Template with WP Kraken

WP Kraken

So you’re set on your next idea, found the perfect website template, added your content but now stumped on integrating that final feature. Yup, 15 WordPress plugins just isn’t the right solution.

In steps WP Kraken, a service that helps you with the last 10% that gets your idea shipped.

Simply put, their service provides custom design and code tweaks to integrate your features not provided in your template or existing website.

“What types of services do they offer?”

  • Custom Feature Additions eg. add newsletter sign up box
  • Speed Optimization
  • Website Migration to better hosting
  • Testing & Bug Fixing
  • Design Improvements
  • Non-WordPress HTML coding too

“Rad, but why should use I them?”

  • Free Quotes
  • 100% Money Back Guarantee
  • Experience, with over 38 000 Clients

 

I know a lot of OPL readers get stuck in that final phase just before launch and hope WP Kraken can get you across the line. Please send feedback on your experience with them.

Cheers,
Rob

ps. how rad is the Octopus branding 🐙


by Rob Hope via One Page Love

Live – Papillons de Nuit

Live - Papillons de Nuit

Impressive One Pager promoting the 'Papillins de Nuit' festival that took place in Normandie, France. The vibrant Single Page site features slick off-canvas content areas, unique interactive SVG animations and a ton of awesome GIFs. Neat feature too with the media filter for GIFs, photos and videos.

by Rob Hope via One Page Love

Optania

Optania

Launching soon page with a centrally-dived layout and a countdown timer for Optania - a new tech that helps students develop their metacognitive skills. Nice touch with the beating heart in the copyright.

by Rob Hope via One Page Love