Learning Javascript

Besides HTML and CSS a frontend engineer must also be a master in javascript.

Learning and mastering

These are books I recommend. If you are new to javascript I would read in the following order:

  1. Eloquent Javascript (free online version) by Marijn Haverbeke
  2. Javascript Patterns by Stoyan Stefanov
  3. Javascript Design Patterns (free online) by Addy Osmani
  4. Javascript: The Good Parts by Douglas Crockford
  5. Javascript for Web Developers by Nicolas C. Zakas

The javascript bible

The ulitmate reference book to own is Javascript: Definitive Guide by David Flanagan. This book covers everything javascript has to offer.

Further reading

A javascript programmer is a software engineer and needs to know classic software engineering skill. It makes sense to read books such as HTTP: The Definitive Guide to improve your overall understanding of HTTP and how the world wide web works. Head First Design Patterns is a book usually read by java developers but I strongly recommend javascript developers to also read this. Design Patterns can be applied to any programming language with minor adjustments at most.

Conclusion

Reading and owning these books for re-reading is highly recommended. Reading alone is not enough though. It is important to practice theory by writing code, experimenting, creating bugs on the way and figuring out how these bugs were caused.

Post Update

21.05.2013: Add link to Addy Osmani’s Design Patter online book.

Posted in frontend, javascript | Tagged , , , | Leave a comment

How to chose a javascript library

There are so many free 3rd party libraries out there that are able to solve a problem. The question is which of these libraries to pick for the job.

Identifying reliable libary

How to chose a 3rd party library and make sure it’s reliable. The focus lies on libraries that are free to use.

  • The library’s source code is on a public repository such as Github.
  • The library is top ranking in search results. This means the library has been linked to often, perhaps from blogs where it has also been discussed.
  • There are questions and answers on Stackoverflow. Questions will arise if the library is used.
  • There are independent blog posts about the library. Only worthwhile libraries will be spoken about.

Github helps

Github makes identifying reliable libraries very easy. At the time of writing Javascript makes 21% of Github’s hosted source code, so it is very likely, that a javascript library is hosted on Github.

Have a look at the screenshot in Listing 1. There are three marked areas:

  1. Stars. The more the better. Only a reliable library’s repository will be favourited often.
  2. Forks. The more the better. Only a promising or otherwise interesting library’s repository will be forked to work on. More forks means more people are working on improving the library.
  3. Last update. The more recent the better. Recent updates means the library’s repository is alive and probably well kept.
github_2013-05-12_10.15.15.png

Listing 1: jQuery-Mobile on Github (2013-05-12 20:15:15)

Other things to look at are tests. A folder named test in the repository’s root directory is a good sign. Tests provide confidence. A detailed documentation with examples and usage instructions is also a sign of a popular library. The author wouldn’t make this effort, if the library wasn’t meant for re-use.

Conclusion

Avoid re-inventing the wheel at all costs. If there are libraries out there then use them, but be careful about which library to use. The wise web developer will take some time in selecting a reliable library. He will do this eagerly because he is lazy and because he is aware of the price of using a poorly maintained library or creating an own one.

Posted in frontend, javascript | Tagged , , , , | Leave a comment

GIT Release Package Zip

On one of the projects I worked on previously I could develop a module independently. I had to develop independently for security reasons. This meant I had to use separate versioning for my code. I chose GIT. I prefer GIT to SVN for a few reasons.

This also meant that I had to hand over my module to the rest of the development team. The handover was a Zip-File containing my work. So how did I manage my handover packages?

Creating a Tag in GIT

When I was ready for the handover I made sure I was on the master branch and all the working features were merged with the master branch. The steps after that are described
below:

1. Created a tag with a comment

git tag -a Release-YYYYMMDD -m 'Customer preview release'

2. Push tag to the remote branch

git push --tags

Creating a Zip with GIT

1. Create zip-archive for the handover

git archive --format zip --output /PathTo/Release/Release-YYYYMMDD.zip master

Don’t forget the branch name master at the end.

2. Create a simple release documentation
To monitor the changes I also included a list of the files that had actually changed between two releases. There’s a nice handy git-command to do this.

Get all changes between two commits (use commit hashes):

git diff --name-only d20354c d6070d48

That’s it already!

Posted in git, quicktip | Tagged , | Leave a comment

JSP Basics for Frontend Engineers

I am working on a website that uses Day CQ5.5 as it’s backend. Frontend engineers and CQ specialists are required to work closely together. This means that frontend engineers are writing html surrounded by jsp-logic. There are three useful reference links that make working with jsp a lot easier. A frontend engineer should be familiar with the basics and not run to the java software engineers for every little change.

The two useful links are:

Posted in quicktip | Tagged , , , , | Leave a comment

Energy consumption in Switzerland 1910 – 2010

I was randomly browsing the Swiss Government’s statistic site bfs.admin.ch and came across an excel sheet showing Switzerland’s total energy consumption from 1910 until 2010. There are many interesting bits and pieces of information in this spreadsheet for example:

  • Why did the total energy consumption suddenly drop in 1917? (There’s also a drop after 1943)
  • Why was more coal used from 1980 until 1991 and less before and after this period?
  • The usage of renewable energy from 1990 onwards
  • and so on…

10 times increase

In my opinion the most notable information is the increase in energy consumption from 1910 until 2010.

  • 1910 – 99’990 Terajoules
  • 2010 – 911’550 Terajoules

That’s roughly 10 times more in 2010. Obviously one needs to consider factors such as population growth, modern transportation and technology to make a solid statement about the consumption increase.

To give you an idea how many energy that is:

1 Terajoule = 277’777,778 kWh.

An average Swiss household consumes 3’500 kWh per year (2007), (Source [1]). I’ll let you do the math.

Sources

[1] Der typische Haushalt-Stromverbrauch, Jürg Nipkow, Stefan Gasser, Eric Bush, Bulletin SEV/VSE 19/2007

Posted in open data | Tagged , , , , , , | Leave a comment

TerrificJS changes from version 1.1.2 to 2.0

A couple of weeks ago TerrificJS 2.0 was released. In this post I’m going to write about the changes and why they rock. I wrote about using TerrificJS in an earlier blog post. Have a look at that, if you’re new to TerrificJS and want to know why it’s – well – terrific.

Version 2.0 Improvements

Smaller file size

TerrificJS is smaller in file size (2kB less than TerrificJS 1.1.2). This is an important improvement since TerrificJS is being used by quite a few high traffic websites such as the Swiss railway site (www.sbb.ch) where performance is vital.

Allow for external dependency management

The file size reduction was possible, because TerrificJS dependency management was removed. This allows the software engineer to have the freedom of choosing his own dependency management solution. There are a few out good ones out there such as yepnope and StealJS.

Hook changes

With the dependency management gone, there is no need for the dependency- and beforeBindng-hooks. onBinding has become on and afterBinding was changed to after.

CSS naming

Before Version 2.0 module names were written as .modMyModuleName css class. As from Version 2.0 also .mod-mymodulename can be used as well. Have a look at the Conventions for more details.

Connector id can be string

Connectors can be identified by any given string value. Before Version 2.0 and integer was required to specify a connector channel.

Basic structure of a TerrificJS Module

Below is an example of what the TerrificJS Module structure looks like for TerrificJS 2.0. Use this as a template to create new modules. The code example is taken from the TerrificJS documentation.

(function($) {
    /**
     * Default module implementation.
     *
     * @author Remo Brunschwiler
     * @namespace Tc.Module
     * @class Default
     * @extends Tc.Module
     */
    Tc.Module.Default = Tc.Module.extend({

        /**
         * Initializes the Default module.
         *
         * @method init
         * @constructor
         * @param {jQuery} $ctx the jquery context
         * @param {Sandbox} sandbox the sandbox to get the resources from
         * @param {String} modId the unique module id
         */
        init: function($ctx, sandbox, modId) {
            // call base constructor
            this._super($ctx, sandbox, modId);
        },

        /**
         * Hook function to do all of your module stuff.
         *
         * @method on
         * @param {Function} callback function
         * @return void
         */
        on: function(callback) {
            callback();
        },

        /**
         * Hook function to trigger your events.
         *
         * @method after
         * @return void
         */
        after: function() {
        }
    });
})(Tc.$);

Source: http://terrifically.org/api/module/#inheritance

Hello World Example

I’ve put up a Hello-World example on jsfiddle.

Conclusion

TerrificJS has matured with version 2.0. Users of TerrificJS now deal with a smaller file size, can choose their own dependency management and write less javascript for each module.

Feel free to contact me if you have questions on TerrificJS and how to use or visit www.terrifically.org

Posted in frontend, javascript, terrific | Tagged , , | Leave a comment

Character Encoding

In this compact post I am addressing two issues:

  1. Using UTF-8 as a character encoding
  2. Position of charset meta tag in html document

Too many websites are still using a character encoding other than utf-8.
Too many websites don’t have the charset-meta tag as first element within the <head>-section.

According to w3 the meta tag for the charset needs to be placed as early as possible in the html document and should be the first element within the <head>-section. You can find examples of how to do this for HTML5, XTML, HTML4 on W3′s Q&A about doctype declaration. It makes sense to add this tag even before the <title>-tag since the title may contain Umlaut or other character which are not part of ASCII. UTF-8 is good for multilingual support.

The META declaration must only be used when the character encoding is organized such that ASCII-valued bytes stand for ASCII characters (at least until the META element is parsed). META declarations should appear as early as possible in the HEAD element.

Source: http://www.w3.org/TR/html4/charset.html

Examples

Twitter Bootstrap

Notice the first element after <head>:

<!DOCTYPE html>
<html lang=”en”>
  <head>
    <meta charset=”utf-8″>
    <title>Bootstrap, from Twitter</title>

Source: https://github.com/twitter/bootstrap/blob/master/examples/container-app.html

HTML 5 Boilerplate

Notice the first element after <head>:

<!doctype html>
<!– paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ –>
<!–[if lt IE 7]> <html lang=”en”> <![endif]–>
<!–[if IE 7]>    <html lang=”en”> <![endif]–>
<!–[if IE 8]>    <html lang=”en”> <![endif]–>
<!–[if gt IE 8]><!–> <html lang=”en”> <!–<![endif]–>
<head>
<meta charset=”utf-8″>

Source: http://html5boilerplate.com/

Conclusion

  • Always specify a character set.
  • Use UTF-8.
  • Place the meta tag immediately after the head-tag

Further reading / Sources used

Posted in frontend, quicktip, Uncategorized | Tagged , , | Leave a comment

Using TerrificJS in your website

I have set up a few projects with TerrificJS and I have to say it’s great! With TerrificJS I don’t have to wonder about how to organize my javascript source code. The structure is taken care of. The timing is taken care of. There’s no danger of one module’s code getting in the way of another. So what’s a module? TerrificJS handles modules much like OOCSS. OOCSS is a CSS framework which gives common components or modules (I’ll stick with module) a blueprint structure.

Here’s a minimum module structure (see OOCSS Modules for more details).

<div class="mod MyModulenName"> 
    <div class="inner">
        <div class="bd">
            My Module contents goes here.
        </div>
    </div> 
</div>

What if you would like to use TerrificJS in your existing website which does not use the OOCSS structure for it’s modules? Doesn’t matter – you can, with RegisterModule. More of that later in this post.

Adding TerrificJS to your website

You will need to download jQuery and TerrificJS:

Include both files, first jQuery and the TerrificJS.

 <script src="http://code.jquery.com/jquery-1.6.4.min.js" ></script>
 <script src="/yourpath/terrific-1.0.0.min.js" ></script>

The Bootstrap

jQuery and TerrifficJS are included. Next the TerrificJS Bootstrap needs to be added.

<script src="http://code.jquery.com/jquery-1.6.4.min.js" ></script>
<script src="/yourpath/terrific-1.0.0.min.js" ></script>
<script >
	// TerrificJS bootstrap
    (function($) {
        $(document).ready(function() {
           var $page = $('body');
           var application = new Tc.Application($page);
           application.registerModules();
           application.start();
         });
     })(Tc.$);
</script>

The TerrificJS Bootstrap is added after the jQuery and TerrificJS libraries. A lot is happening here.

jQuery noConflict

First of all TerrificJS makes use of jQuery noConflict (also have a look at the TerrificJS uncompressed source code).

Tc.$ = jQuery.noConflict(false); // Part of TerrificJS source

This explains the self-executing function which begins on line 4 and ends on line 11. Tc.$, which is a reference to jQuery, is passed on to $. Inside the self-executing function $ can be used to access jQuery. This saves on code and time.

If you need jQuery to be assigned to the dollar variable $ then add the following line after the TerrificJS bootstrap code:

$ = Tc.$ // This goes after the TerrificJS bootstrap

Application context

Line 6 sets the context. If nothing else, the html body containing the visible part of your website is the context.

var $page = $('body');

You could apply TerrificJS to a much smaller area on your website if you don’t want the whole DOM to be scanned by TerrificJS.

var $page = $('#someOtherContainerActingAsContext');

Getting the party started

Lines 7 and 8 initialize the application/website. The context is passed to Tc.Application and all modules are registered. This is where OOCSS comes in. TerrificJS looks for all html elements with the css class mod. It then looks for a second css class with the prefix modMyModuleName. You can see all the naming conventions used by TerrificJS on http://www.terrifically.org/learn/basics/conventions. Does this mean you need to add a mod and modMyModuleName to all your already existing modules on your website? No. Carry on reading.

Using TerrificJS without OOCSS

You’re not starting from scratch and would like to add TerrificJS to your existing website. No problem. Let’s say you your main navigation html looks like this.

To treat the div with the id MainNav as a module the following line needs to be added to the TerrificJS Bootstrap.

application.registerModule($('#MainNav'), 'MainNav');

The first parameter is the jQuery object of the module and also the jQuery context used. The second parameter is the name of javascript class used by TerrificJS. For more information on the parameters see registermodule.

Module Javascript Class

The predefined methods used in the code below are explained on http://www.terrifically.org/learn/module. Putting it all together:

(function($) {
    Tc.Module.MainNav = Tc.Module.extend({

        init: function($ctx, sandbox, modId) {
            // call base constructor
            this._super($ctx, sandbox, modId);
        },

        dependencies: function() {
            //this.require('File.js', 'util', 'beforeBinding');
        },

        beforeBinding: function(callback) {
            var $ctx = this.$ctx;
            // Put code that only depends on the existance of this
            // dom readyness of this module

            callback();
        },

        onBinding: function() {
            var $ctx = this.$ctx;
            // Put all your code in here that also depends on the
            // dom readyness of other modules. For example if 
            // this module communications with other modules via 
            // connectors.
            // You're on the safe side if you put all your code 
            // here.

            // Example: Change the background color of this 
            // instance of div#MainNav
            $ctx.css('background-color', 'red');

        },

        afterBinding: function() {
            var $ctx = this.$ctx;

        }

    });
})(Tc.$);

The class name is set on Line 2 (MainNav).

Tip: You could save the above javascript in a file called Tc.Module.MainNav.js and included after the TerrificJS Bootstrap.

Conclusion

So, this is who you can integrate and use TerrificJS on your website. Feel free to contact me if you have any questions or want to see more specific examples.

Posted in jquery, terrific | Tagged , , , | 1 Comment

The Swiss Opendata Manifest

I’m very fond of the Swiss Opendata Manifest (German). It’s written to the point and sums up everything that open data should be about. I’m going take out and quote the interesting bits.

The manifest is divided into five sections:

  1. Initial position
  2. Uses
  3. Realisation
  4. Need for action
  5. Commitment

Initial position

This is a Swiss point of view. Where are we now? Well, government data is being collected, stored and analysed digitally for quite a while now. In the past years, laws about freedom of information (German) were established and information and communication technologies have greatly improved. Official documents must be provided and made accessibly to anyone interested (unless the law states otherwise).

Uses

The data collected and stored by the government and the municipalities can have far more uses the first expected. Government data that is not explicitly protected by law must be made open. Open Government Data is about actively making data open increasing the transparency, enabling innovation and saving money.

Transparency

The citizen has the right to see and understand. Through the many eyes of the citizens there is more control of the data and less work for the administration. Erroneous or extreme values will be spotted much quicker and the quality of the data will increase. Transparency is an essential condition for a successful relationship between the public and administration.

"Transparency is a minimum standard. Data should only be kept from the public for very good reasons such as the protection of privacy and personal data."

Innovation

The open public authorities data can be used by companies and private persons. It’s foreseeable that software engineers and other creative people will use the data to create applications to analyse and visualise the data in an innovative way.

"The information society cannot let this innovation potential to move by without action."

Saving cost

Cleaning, optimising, combining and storing data is a tedious task. If open, the public can join in on the task helping authorities reduce cost.

"The public authorities cannot afford to do without the public’s help and knowledge."

Source

Posted in open data | Tagged | Leave a comment

Improve your frontend engineering skills

If you want to improve your frontend engineering skills and help others at the same time, register yourself on stackoverflow.com and start answering questions. Pick out all the html, css and javascript topics. You’ll learn a lot from other people’s questions and code samples.

Posted in tip | Tagged , , | Leave a comment