What the Heck is Memoization?

OK, I admit it. I went through a phase when I first heard the word memoization where I ignored that it existed. After all, what could a technique with a name like that have to offer me?

Well, I finally threw away my pride, admitted it might be valuable and spent some time learning about it. Yes, it is a technique used in functional programming, but it is a technique that can be applied to any coding paradigm. And it can be quite valuable.

So to make my penance complete, I thought it appropriate to do a tutorial on memoization.

Now don’t let the term scare you off. It really is not a complex concept. The patterns for implementing it are a bit involved, but the concept itself is pretty easy to understand.

I just published the tutorial on memoization and you can access it below. Take a look and give me your thoughts.

Functional JavaScript

Functional programming in JavaScript has been a topic of considerable discussion for some time now. Several books have been published about functional programming in JavaScript and there are developers that use this approach almost exclusively.

Whether or not JavaScript is a functional programming language is beside the point now because so many people are using it for such.

I don’t know enough about functional programming to teach the ins and outs of that particular approach. However, what I do know is that functional programming has some very valuable concepts that you can draw on to improve your code. I’m advocating a pragmatic approach to functional programming in JavaScript. In other words, use elements of functional programming that you find valuable.

So this begs the question, how to get started? Well, as mentioned there are several books published about the subject, and I will be reviewing some of those in the future. But I think a great first step is to really understand some of the methods of the Array object. These methods were designed around functional programming concepts. For example, map, forEach, every and reduce are a few that are valuable to understand. And it just so happens we have a tutorial on some of those methods.

What are your thoughts on functional programming in JavaScript?

You can find the tutorial here:

Exception Handling

Exception handling is an important aspect of JavaScript coding. I know that when I first learned exception handling, it opened for me new horizons on how I could approach problems. If an exception occurred because of circumstances that were not ideal, I could react to that exception and still keep my programming running.

The main mechanism for exception handling in JavaScript is the try catch statement. It is great for exception handling, but it is also possible to use try catch in situations where there is not an exception.

I just completed two tutorials on exception handling. The first shows how to use try catch statements in your code. The second talks about when you should and should not use try catch.

Take a look at both videos and if you have any additional rules to add for using try catch statements post them here.

Obfuscation in JavaScript

I love the word obfuscation. It seems to reflect its own definition. To obfuscate something is to make it difficult to understand.

So why would we want obfuscation in JavaScript. Well, let me give you an example. Recently I was working on a project where we had to connect to an online system. The online system did not have the security fully implemented yet, and as a result we had to include some of the details about the system in the JavaScript code. Well, we didn’t want it to be super simple for someone to discover that information. We wanted to hide it a bit. So the solution…we obfuscated that portion of the JavaScript code.

I learned about a couple of sites that help do this. They obfuscate your JavaScript code by moving things around and converting information to Hex. I used both of these locations. I would pass the code through the first location and then pass the results through the second. Technically, someone could figure it out, but it is quite difficult. Drop some code in, obfuscate it, and then see if you can make heads or tails of it.



Objects, the Center of the JavaScript Universe

When someone says that JavaScript is not an Object Oriented Language, what they really mean is that JavaScript doesn’t use the approach to object orientation that they are used to. It doesn’t use classes. It is not necessary to create a class in order to instantiate an object. To me that is a big advantage with JavaScript.

To say that JavaScript doesn’t use objects is very far from the truth. I will put it this way: it is almost impossible to do anything of value using JavaScript without using objects. Everything but primitive values are objects. Arrays are object. Functions are objects. A regular expression definition is an object. A date value is an object. And it goes on and on.

So objects are central to everything in JavaScript. Because of this point I have started compiling (in the form of a playlist) a bunch of tutorials on objects in JavaScript. There are numerous topics that could be covered with objects, so this compilation is not complete, but that is what I would like to work towards.

If you haven’t taken the time to really sink you teeth into objects and understanding the object oriented nature of JavaScript, you need to! It needs to happen sooner rather than later. You can start with this playlist.

Then get a book on objects in JavaScript and dive deeper. I have reviewed a couple as a part of this blog. Here are two good ones.

Debugging Tips

When I first started working with JavaScript, I did most of my debugging with alert messages. That was painful!

Now I find that a lot of my debugging is done with the console. At times I will open up the debugger, but for most things, I can figure it out pretty quickly using the console.

Over the years I have learned a few console commands that are very helpful. I know I would have loved to have these commands when I started out, so I put together a tutorial on some of the lesser known console commands. I think these can help you find a resolution faster.

Take a look at our latest tutorial.

Working with XML Data

The other day I had to work with some XML data in JavaScript. I’ve mostly worked with JSON data, which is easy to work with because you can convert it to a JavaScript object. XML data is not too bad either once you convert it to a Document Object Model.

Browsers have a DOMParser object that will allow you to make that conversion. Once you have the XML data as an XML DOM, you can then use the same commands you use to manipulate an HTML DOM to work with the XML data.

Since I had to work with it, I decided to do a tutorial on it. It is the latest tutorial in the JavaScript Question playlist of the All Things JavaScript YouTube Channel.

The tutorial just went live today. You can view it right here:

What is the Difference Between a Programming Paradigm and a Design Pattern?

When you first start learning to program, your initial focus is on the language. You learn the syntax and the structure. Once you become comfortable and able to put statements together to solve programming problems, you are then free to think more about programming.

As experienced programmers have thought about the best ways to approach the building of an application or the best way to solve a particular problem, they have developed paradigms and design patterns. The smart developers draw on this expertise to improve their own programming skills by adopting paradigms and design patterns.

A paradigm is simply a style or an approach to programming. For example, Object Oriented programming is a paradigm. If you follow this paradigm you use objects to approach the overall program. The objects contain data and behaviors and you connect them in logical ways to successfully solve the task at hand.

Now a design pattern is a tried and tested solution to a common programming pattern. It could be considered a best practice. If you approach a program using an Object Oriented paradigm, there are a number of design patterns you can then draw on to solve specific problems.

So how do we apply this to JavaScript? JavaScript can support many different paradigms and there have been numerous design patterns developed to solve individual problems that arise with each paradigm or across paradigms. JavaScript is flexible. There is not one way to accomplish something and therefore numerous approaches have been developed. You can draw on those approaches and you can mix and match them as needed.

I really like this quote by Angus Croll:

Natural language has no dominant paradigm, and neither does JavaScript. Developers can select from a grab bag of approaches–procedural, functional, and object-oriented–and blend them as appropriate.

Aren’t you glad you are a JavaScript programmer?


Making Sense of the Prototype

Over the last couple of weeks in the All Things JavaScript YouTube channel, I have been focussed on creating a couple of tutorials that will help those that are somewhat new to JavaScript understand and grasp prototypes.

I have to admit that when I first started learning JavaScript, I ignored the prototype when it came up. I was mainly doing procedural programming, so I had what I needed and wasn’t looking for another construct. Well, I have since learned that it was a bad idea to ignore it. The prototype is a key concept in JavaScript, and a very powerful feature, but it is a concept that is sometimes neglected by those that are new to JavaScript.

In these two tutorials I have tried to first explain prototype in a way that will make sense. And then I delve into three techniques for setting the prototype of an object.

So quickly to get you started, the prototype is simply a way to link one object to another. Once you have linked your object to another object, which is called the prototype object, you can then access the properties and methods that reside on the prototype object. This mechanism is the essence of prototypal inheritance in JavaScript. This concept allows you to structure code more effectively.

If you have not watched these two tutorials yet, first watch the tutorial that explains prototypes.

Then you can watch how to set the prototype.

Are you making use of prototypes in your JavaScript coding?

Approach to Projects

Sometimes the most difficult part about a project is starting. It can reduce a lot of stress and put you well ahead if you have a process for starting a project.

For example, something I commonly do is to spend time setting up the structure of the project and the processes needed to test and minify project files. That little bit gets my head into the project and then I can get going. Otherwise, I feel like I languish before I can really get started.

The other day I read a great article at Sitepoint about how to approach projects. I thought it organized all the starting tasks really well. These starting tasks are not only important, but I realized they can accomplish the same thing of getting your head in the project and getting you going in the right direction.

Give it a read here.

What approaches do you take?