Using ES6/ES2015 in a Node.JS and Express

These are some very minimal changes to the standard express.js generated app.js file. It looks nice, “=>” syntax is getting easier to read the more I use and get used to it. Note: I took out a lot of unrelated code to isolate the topic of the blog. This example is for getting a development, learning environment ready to use Babel.js with Express on the back and front ends.

How’d I do it?

Using Babel’s “babel-node” cli tool, its compiling in real time in the development env. For production, do not use babel-node, you’ll want to compile with Babel.js then run with node. Just use “npm install -g babel-cli” and babel-node is already there and built in.

I also included eslint for good measure. Make sure to “npm install –save-dev babel-eslint” and set the parser to “babel-eslint.”

What else do you suggest that can I do to the app.js to take advantage ES6 features, without straying too far from the default?

NPM as an automated build system.

I hate having to set up complicated environments. I like JavaScript/Front End Development environments to be:

  1. Easy to understand and follow along for everyone on the team.
  2. Quick to get running and make changes.
  3. Free of having to learn new domain specific or pseudo languages for simple building.

Other platforms, Java or .NET, benefit from IDEs because they follow a predefined path and have a set of best practices defined into the language. The ugliness of JavaScript is why it’s powerful. But it can also lead to a lot of unnecessary reinventing the wheel.

Using NPM to use simpleton and replaceable tools keeps my projects fitting those three points above.

NPM is amazing and powerful. I’ve built three major projects now without Grunt or Gulp, using only NPM and locally installed modules.

The run command lets you run a locally installed module from the command line.

This runs “mocha test.js”

Scripts can also call one another.

In this previous example, “npm run check” will execute lint and test.

You can also use other NPM modules to run and watch the app.

For instance, in the following example, I use babel to convert and bundle the files after the lint and test. Then I run nodemon, which I have executing a custom script. Nodemon is also told to ignore the public directory where babel places the bundle. (I learned the fun way that this creates an infinite loop, if you don’t ignore it!)

Now when I run “npm run auto-start” it won’t just run the lint and tests and launch the app, nodemon will also watch for changes, linting, testing and restarting each time a change is detected.

Building from NPM is incredibly powerful. When I need to change the files or directories, I can do it inside the string. If I want to change the linter, I can change the dependency, and the script for “lint’s” value. Run “npm install” to update the dependencies. Then all the existing “npm run lint” calls will call the new linter. There’s only a tiny amount of configuration and no coding, just a JSON file describing what I want to happen.

Multiple calls to the same NPM module are possible, while using a different command to have different options.

Finally, the best part about building with NPM way: All that someone on the team needs to do to include this automation or build this app on another machine is clone the files and run “npm install” to get it up and running (if they have Node installed).

When won’t automated building with NPM work? Well, sometimes I have more complex builds, where one area of code might use one set of tools and another area uses some other tools. Some operations are easier to do asynchronously, rather than through piping stdout. What I’ve found myself doing more, is using NPM for my smaller personal modules and I’m still using grunt for my large projects.

Thanks for reading, if you have any ideas, concerns, or complaints please let me know on twitter @StevenLacks or in the comments.

Java Variable Example 4 – Parameters

Java Variable Comparison Chart
The difference between the various variable types in Java we’ve discussed.

Parameters are variables that represent the arguments passed into a method. They are declared in the method declaration. Take the main method as an example:

args is declared in the method declaration for main (inside the parentheses).  Like any declaration, you need to declare its type, an array of instances of the String class. In other words, you declare the class or method, then inside the parentheses you declare what Java is supposed to be looking for.  In this case it’s a set of Strings.

Parameters You’ve already seen examples of parameters, both in the Bicycle class and in the main method of the “Hello World!” application. Recall that the signature for the main method is public static void main(String[] args). Here, the args variable is the parameter to this method. The important thing to remember is that parameters are always classified as “variables” not “fields”. This applies to other parameter-accepting constructs as well (such as constructors and exception handlers) that you’ll learn about later in the tutorial. Java Variables

Makes perfect sense… wait… what? Variables and fields aren’t the same thing? Sigh… okay before we get into parameters let’s clear up that line.

Let’s look at the code from example 3 (I put it above, up yonder). In Java, Non-Static Fields/ Instace Variables are ones like speed and gear. Meanwhile, newGear is a local variable.  The gearShift is our parameter in question.

A field is a variable that is declared inside the class block but outside of the methods or constructors.

Parameters are the names of the variables that are going to be used inside of the method. You pass in arguments, and parameters are used inside of the method.  An argument is also called an actual parameter, because it is the actual value that is being computed while the parameter is the variable itself. Meanwhile, the words arguments and parameters are mostly used interchangeably, because it is often confusing when trying to talk about your methods and it’s usually not too important to distinguish.

 

Java Variables Examples 3 – Local Variables

Local variables are designated inside some block of code that limits their scope.  That means the variable can only be set, accessed, or retrieved in the scope of the block.  In programming, these variables are only visible and editable in the block of code in which they are created. More specifically in Java:

Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class. Java Variables Tutorial

Let’s look at an example:

In this example, we are in the class Bicycle with the method changeGear which takes input for the gear change (gearShift) and then outputs the new gear.  The new gear is a local variable (newGear) which is can only be used in the method.

Our example is fine for demonstrating a local variable but it’s not the only example of a local variable.   A local variable can be inside constructors, lambda’s, conditional blocks, etc.

Why keep variables local?

  1. Memory. In Java (and most languages) when a variable is local, it doesn’t have to remain in memory. This might increase performance.
  2. Safety. By “encapsulating” a variable, you keep it safe. It’s less likely that  you will accidentally change the value. Java’s encapsulation powers aren’t limited to local fields, private instance variables vary in usage, but follow similar principles.
  3. Ease of use. While points 1 and 2 definitely make coding Java easier, naming variables is really important. Picking the right, descriptive identifier is necessary to keep code legible and workable. Local variables make it easy to name them with simple and descriptive identifiers. Additionally, the type casting makes it easy to see what the variable does locally.

Java Variables Example 2 –Class Variables

Graphic shows class variable in relationship to instance.
Class variables are the same across all instances of a class. They can be changed from anywhere, unless final is invoked.

Unlike instance variables, class variables are the same across the class and all instances of the class. They can be set from anywhere, but they change across the board in all instances. Continue reading “Java Variables Example 2 –Class Variables”

Java Variables Examples 1 – Instance Variables

Instance Variables Flow Chart
What is an instance variable? A class member that each instance of the class inherits, each containing an independent value from the same member in other instances.

Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in “non-static fields”, that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.

Referencing Java Certification Tutorial, Section 1-1 Variables

In the following sample code, we set up a class. This class will be referenced in our application. This class has two instance variable members, speed and gear. While each bike shares a variables named speed and gear each instance has its own variable, with its own place in memory and value. Continue reading “Java Variables Examples 1 – Instance Variables”