Is success the key to happiness, or the other way around?

Yesterday I was watching this TED talk from Shawn Achor and this question hasn’t left my mind since. The speaker talks about that being happy makes you work and learn more effective, and claims that therefore you can be more successful. I think this statement, or rather, fact is very valid and is worth to stop and think about for a minute.

Shawn mentions that chasing happiness by working towards successes that we want to have is a never ending one, as we constantly set new targets and raise the bar. Or, quoting Shawn “If happiness is on the opposite side of success, your brain never gets there.”

Standing still by what we have achieved so far makes one happy, and let’s you do and focus on what you like and love. Which, according do Gary Vaynerchuck (you should read his book Crush It! and The Thank You Economy) is the key to success and personal development. Which, in turn, raises your level of success and if you stick to this approach, your level of happiness.

Think about it. What is your attitude to happiness and success?

I found this all very interesting and worth thinking of. If you are reading this and are looking for more in-depth info I can recommend you to view Shawn’s presentation.

Posted in General, Productivity | Leave a comment

Fast search and replace in large files with sed

Last week i had to search and replace all occurences of a string inside a relatively big MySQL database dump file. My previous experiences with search and replace actions in files of similiar size or bigger suggested that this was going to take me a while. Normally i would write a small PHP script to do the search and replace action for me. However, recently i’ve been looking to find better, more productive, ways to do everyday things. So after a quick google i found the *nix tool sed.

What sed is, is best described from it’s manual:

Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

The command to search and replace is similar to the syntax you would use in vi.
Let’s say you have a file database.sql and want to replace every occurence of myolddomain.com to mynewdomain.com. You would use the following sed command:

$ sed -i 's/myolddomain.com/mynewdomain.com/g' database.sql

By executing this command sed will go through your file, searching and replacing every occurence of myolddomain.com within moments. In my case, on files of 18MB and 32MB, the search and replace took under a second. But will take just moments on files much bigger than that.
Since sed is a command line tool and accepts all kinds of input, be it streamed or piped, it is a tool that is suited for a lot of different use cases.

Sed is probably good for a lot more than just search and replace. But i’ll have to look more into it to write something meaningful about it. If you want to read more about sed or check out the slides of the presentation “Sed & Awk – The dynamic duo” by Joshua Thijssen.

Oh by the way, as i mentioned sed is a linux/unix tool, but also seems to be available for Windows. It’s probably included in Mac OS distributions as well.

Posted in Productivity, Tools | Leave a comment

Anticipating for change with the Strategy-pattern

We developers get to deal with change nearly every day. As your project progresses, stories or requirements are changed or added. And we’ve all been to the point where our code gets brittle or unmaintainable after too many changes and/or additions.

Let’s start with a metaphor. Let’s say your keyboard is broken and you need a new one. Luckily the keyboard is connected via a USB-connector to your computer, so you can replace it with another keyboard with ease.
But what if there wasn’t a common interface (USB) to connect your keyboard to a computer, for example if it was directly connected to the computer?
Indeed, you would either get an entire new computer or you’d be soldering your new keyboard onto your computer. None of which are ideal solutions to the problem.

This metaphor applies to programming in many ways, but arguably the most important principle that comes from this example is that you should program to an interface where possible.

In a lot of cases programming to an interface encourages the interchangeability of certain parts of your application. Thus it enables you to write flexible code, and anticipate for changes in domain logic.

If you’ve ever written or worked with an online shop you might have come a cross the problem that different countries have different tax rates. Each country has their own tax rate, which of course can change independently. When implementing an online shop, this problem cries out for a flexible solution, and you need to think of a durable strategy to solve this problem.

Diagram 1 - Different implementations of TaxInterface

A good way to do this is to look for commonalities of the different calculations. It is not hard to see that every tax calculation has nothing more than an in- and output number. As long as the tax is calculated correctly, the calculation itself is not really important to the system.
These commonalities are the things you define in the interface, in this case we’ll call the method calculateTax. By programming each country-specific implementation to the interface (TaxInterface) the Order does not need to know (or worry) about the outcome of TaxInterface::calculateTax(). The only thing that it needs to do is instantiate the approperiate implementation, which is out of the scope of this post.

See Diagram 1 for a clarification.

This solution is defined as the “Strategy pattern”, and it is about anticipating on (potential) change of features. In the example it is fairly easy to add a tax implementation for another country, while you have to make only small changes to your existing code.

I hope this example demonstrated the reason behind programming to an interface and and the purpose of the Strategy-pattern in a fairly simple manner.

Posted in Best practices, Design Patterns, General, PHP | Leave a comment

“I rest my case”

So there it was tonight. Some criticism on my talk/slides called “Let’s live inside the browser”. In a tweet Ivo Jansch, a well known and respected person in the PHP community, expressed how the lack of cross-browser compatibility of my slides proves why “living in the browser” is not gonna happen. Or well, that’s more or less what i think his tweet implies.
In this specific case i think the reasoning is unjustified as i didn’t program mouse control, although that’s a good idea! But of course it’s ironic that my presentation about living inside the browser lacks support for convenient input (you have to use the keyboard left/right arrows).

Anyway, this is a discussion i was hoping to start with my presentation. The discussion about if and how we will end up living inside the browser. And for obvious reasons the native vs. web apps thing is a big part of the discussion.
I have already posted my thoughts on why i think the web is suitable as an application platform a couple of days ago, but i feel that post is not comprehensive enough, so i’ll add some more thoughts.

Web apps vs. native apps
This is an interesting one. If you’re thinking mobile then, as for now, native apps win in terms of functionality and possibly user experience. Simple.

However i don’t think that the web as an application platform becoming more common will rule out native applications per se. Maybe eventually, but that takes years, maybe even decades. If it is at all.

Choosing the web as an application platform is choosing for an easy way to deliver to every device with a browser, instead of limiting to a certain vendor.
However if you choose web, you limit yourself to the use of API’s available through the browser or web services. This is a limitation that should not be underestimated, and is where native applications win big time

Of course, there are these hybrid solutions like the awesome PhoneGap. These enable you to write applications using web standards, and abstract the device specific API’s so you can still develop for cross-platform solutions, but deploy to multiple ones.
I guess this is okay solution, but it shows there’s still lots of work to do regarding web standards. And even then, web standards will always be behind the features “native” API’s provide.

Other concerns
Without a doubt, choosing between the web or native platform for a project is also dependent on the business model behind of your project.
I’d say the web provides more freedom for implementations, which encourages innovation. On the other side, native platforms, and especially mobile platforms have built-in mechanisms to monetize through your application.

Another big thing is the capability difference between desktop and mobile browsers. Mobile browsers, and especially the Android browser are lacking (draft) standard support already, which slow the large adoption of the web as an application platform. But although the mobile browsers seem to be behind on functionality, web developers have always been inventive in finding work-arounds for these issues.

So.. the web huh?
Yeah, i still believe there is a big future for the web as an application platform. And even though native apps are really powerful, the reach of applications is obviously a lot bigger.
With the web standards always in development the web will slowly become more and more suitable for deploying and distributing applications.

Although i feel this post is still not comprehensive enough, i hope i expressed my point of view of this topic.

In the end of the discussion Ivo mentioned the idea of a “web vs native bar fight” with presentations with the standpoints of each camp.
While this sounds like a whole lot fun (i’m definitely up for it!), i’m feeling more for an open discussion with the aim to share thoughts. It’s an interesting but also really important topic to share thoughts on. Preferably with people from different backgrounds.

Anyone want to help set up a meeting for it? I’m not so familiar with it..

Posted in Cross platform, Mobile, web application | 11 Comments

How HTML5 Web Workers make unicorns run

A unicornOne of the things that HTML5 brings us is Web Workers. These things are great for enhancing user experience in webapplications or javascripts that contain heavy computations or other sort of long running scripts.

Web Workers are like threads, which you might know from other development platforms. The definition of threading is the following according to Wikipedia:

“The threaded programming model provides developers with a useful abstraction of concurrent execution.”

Let’s start with identifying the problem.
For the working example for this post, check out this page and hit the ‘Freeze’ button. Under this freeze button there is a function that does a huge secret calculation that is supposed to use a lot of your CPU’s time. Actually it’s not that secret, see code sample 1 for the code. Notice that the unicorn stops moving for a while when you press that button. The freeze function is freezing the UI while executing. (Thank you Captain Obvious). See diagram 1.

// Code sample 1 - Secret freeze function
function freeze() {
    var total = 0;
    for (var i = 0; i < 100000; i++)
        for (var k = 0; k < 20000; k++) total++;
    return total;
}

Diagram 1 - Single threaded script execution

Obviously, this causes the user to wait, and that’s bad practice. Nobody likes to wait.
Web Workers enable you to start another thread that can process computations parallel to the browser’s so-called UI thread. (That executes your “normal” javascript)
With the result of not blocking the user interface.
Simply put; you delegate your heavy computations to another process, which, when ready, will send the result back to your UI thread. This is visualized in diagram 2.

Diagram 2 - Multi threaded script execution

Delegating the long running function to another thread leaves computing space in the UI thread open for other computations. UI computations of course, as it is the UI thread.
Doing this is actually quite simple, and requires to put your code in a new javascript file that will  execute in the worker’s context. Running a script in the worker context means that the script does not have access to the DOM or anything else that is not thread-safe.

Exchanging data between the UI and Worker is being done by the Worker.postMessage method. This method requires one parameter, the data you want to send.
This data is internally serialized as JSON by most browsers to prevent callbacks or other pointers being passed (which would potentially cause cross thread errors). (source)
Code example 2 demonstrates how to create a worker in javascript, and code example 3 demonstrates how the worker script itself should be defined. Notice that event handlers/callbacks are used to start and communicate between the Worker and the UI thread.

// Code example 2 - Creating the Worker and listen to onmessage
var worker = new Worker('webworker.js');
worker.addEventListener('message', function (event) {
    document.getElementById('calculation').textContent = event.data;
}, false);
// Code example 3 - webworker.js
self.addEventListener('message', function(event) {
    var total = 0;
    for (var i = 0; i < 100000; i++) {
        for (var k = 0; k < 30000; k++) {
            total++;
        }
    }
    self.postMessage(total);
}, false);

Inside webworker.js, the self keyword is used to approach the Worker’s API. In here you also see that we add a listener to the onmessage event, and use the Worker.postMessage method to send the output back to the UI thread.
You can start the worker from the UI thread (your normal HTML page or javascript) by using the Worker.postMessage method, as seen in code example 4.

// Code example 4 - Starting the worker
var worker = new Worker('webworker.js');
worker.addEventListener('message', function (event) {
    document.getElementById('calculation').textContent = event.data;
}, false);

worker.postMessage(null); // postMessage parameter accepts everything that’s serializable.

Obviously, you don’t want to stop a unicorn from running across your screen. So if you delegate the freeze function to a Web Worker, you get the result as in diagram 3.

Diagram 3 - Delegate the big calculation to another thread/worker, so the unicorn can keep running

Now click the “Via worker” button in the working example page.
Yay awesome, the unicorn can continue to run across your screen in the meantime! So yes, that’s what Web Workers are good for, making unicorns run across your screen.

No but seriously, i couldn’t think of a more interesting use-case.. But you can fill in your own use-case for a long running function that causes the UI thread to block.

Of course, you want to know in which browsers Web Workers are supported. Fortunately, on desktop browsers the Web Workers are widely supported (although, IE10+). A bit less fortunate is the support on mobile browsers, with the exception of Opera Mobile 11 no mobile browser supports Web Workers. For a comprehensive overview of supported browsers, take a look at the MDN page “Using web workers”.

A more in-depth explanation of HTML5 Web Workers can be found at the HTML5 Rocks website.

Posted in Cross platform, HTML5, javascript, web application | Leave a comment

Why i think the web as a platform is the future

Lately i have found myself thinking about why applications built on web standards are becoming more and more common. What is it that draws us all to this amazing place called the web? And why are more and more companies investing in applications based on web standards?
Personally i am really enthusiastic about the direction the “applicationscape” is heading and i  think web applications have the potential to go a (really) long way.

But seriously, why?

I think the answer lies in how people consume information. It seems we have this (subconcious?) urge to find and use information as effective as possible. And the technology of today has brought us to the point that we want this information anywhere, and we want it NOW.

There are a lot of ways to consume information in modern (internet) times: PC’s, laptops, tablets, smartphones and what not. Each of them having their own vendors and platforms but they also have their own distinctive level of speed, mobility and have different input mechanisms. Each device is used different.
However, important to note is that all these devices serve the same purpose: to serve information.

There is an important thing that, nowadays, all of these devices have in common, and that is that they have a web browser. So all of these devices have access to the web.
That is what makes the web so interesting as a platform. The reach is enormous.
Online, offline, anywhere and any time, on any device. The world wide web is a shared platform across devices already. Regardless of operating system.

An important downside of developing applications for multiple platforms is that it is time consuming. Another downside is that, for various reasons, the different applications you end up with do not always contain the same functionality. Now i can see sometimes this is because the project is on a time constraint, but an often heard reason is that to offer users of different devices the best user experience.
Now there is a big difference between offering a different user experience based on certain context parameters and designing an different application for each platform possibly limiting users access to certain functionality of your original product.

Now this is where i think the web as a platform has it’s potential and can offer more for both end-users and developers. Of course, web applications do not offer the same functionality as native applications at this time, but i think this is a matter of time. The open web standards have become really mature at present time, and i think it is time that we start embracing the web as a platform.

And what about you? I would love to hear your thoughts about the web as a platform in the comments and get some discussion going around this…

On September 17th i am speaking at the PFcongres (in dutch) about the web as a platform and what technologies you can use today to develop for it as such. If you are interested in this topic, or even if you are not (the other speakers on schedule cover amazing subjects!!) PFcongres is the place to be in Utrecht this saturday!

Posted in Cross platform, General, web, web application | 1 Comment

Easy SASS compilation in WebStorm / PhpStorm

If you are a regular reader, you might have noticed i have started using SASS a while go. If you are not, and don’t know what sass is, you can read my previous post on sass here.

Recently Webstorm/PhpStorm introduced the support for sass syntax highlighting, which is really awesome, but the compilation of SASS files is not integrated in the IDE.
There is however a way to set up Webstorm/PhpStorm to use external tools, which is perfect to make sass compilation from within the IDE possible.

Just follow the steps below, and you can enjoy the comfort of SASS compilation from within your favourite IDE.

Open the Settings panel: File > Settings

Then go to “External Tools” and click “Add…”

The following screen shows up, fill in the values you see (but point the “program” field to your sass installation path)

Now, right click on your SASS (or scss) file, and the sass tool will be right there, as you can see in the screen below.

Some additional useful tips:

  • Alternatively, if you prefer the one time compilation, or other sass parameters, just change the “parameters” field, or create some duplicates of this external tool setup.
  • If you work with “deploy to FTP” on save, make sure to enable the “Upload external changes” option.
Posted in css, Development Environment, General, Productivity | 4 Comments

My progress on the PHP on Azure project

Windows Azure LogoIt’s been a while since i’ve blogged about the PHP on Azure contest project. This post will be about the project, but not necessarily about my implementation of PHP on Azure.

As i wrote in my introductory post about the project, the main reason to participate in the contest is to put a lot of things i recently learned into practice. And so far so good.
My main focus is not to build a good application in general, but to me it is about discovering and experimenting on how to keep a PHP application and all that comes with it (search engine, multi media converter, database) as scalable as possible.

Before starting on the project i highly underestimated what is involved with building a highly scalable solution. It definitely involves more than just deploying a project on the Windows Azure platform.

This is also the main reason i haven’t blogged about PHP on Windows Azure that much yet. Since my main goal is to deploy everything on the Windows Azure platform (database, files, search engine and the application itself of course) i had a lot of stuff to look up and experiment, as i’ve never worded with IIS7 and MSSQL in combination with PHP before, leave alone with Windows Azure (or any other cloud platform).

Nevertheless, i really enjoy working on this project. As the deadline is about 1.5 months away from now, a lot of work can be done still.
More or less i feel i am at a point where i can focus my attention on building the application instead of looking up and experimenting on things (as much fun as it is, though).

In the (near) future i’ll write up some tips and tricks here on my blog, hoping to catch up on the bi-weekly blogging requirement for the contest ;-)
There’s a lot to share, i just haven’t put anything into practice yet, so it’s hard to blog about.

By the way, the other participants have shared some really interesting things about their project. You can read all their experiences on the PHP on Azure contest page.

Posted in PHP, PHP on Azure, scalability | Leave a comment

Sass – Stylesheet tool

Sass logoNow this tool you are going to read about is nothing new. In fact, it’s been around since 2007 according to Wikipedia, but i have to share it with you because it will make your life easier!

“Syntactically Awesome Stylesheets” is what Sass stands for and is a tool to make several things related to writing and deploying stylesheets easier. It is a command line tool written in Ruby, and is available as a ruby gem.

For writing stylesheets Sass has a couple of (language)features. For instance it supports variables, calculations, includes and this thing they call ‘mixins’; some sort of templates or functions.

Sass or actually, since version 3 they call them .scss, files need to be converted to normal a CSS file so you can use it like any other CSS file in your browser. This conversion is being done by the command line tool.

One of the most interesting features of this tool is that you can tell sass in what format it needs to output the css format. It can render normal (extended) css, but also in a compact or compressed way.
This is useful when you have deployment scripts for different environments.
Also, while developing, don’t worry about having to run the command every time while: with the –watch switch sass will automatically watch your scss file for changes and render a new css file each time it’s saved.

I really like tools that are easy to use, require no configuration and require no alteration of my workflow process and Sass is definitely one of them!
If you want to learn more about Sass or want to start using it right away, check out their website!

Posted in css, Development Environment, Productivity | Leave a comment

PHP on Azure contest

Lately i have been learning a lot of new things like development techniques, best practices and design patterns. Now obviously the best way to make all this information persist in my head is to bring it into practice immediately!

To start doing this i’ve started contributing to open source software projects like the Zend Framework. And besides helping to improve the quality of the framework framework you’ll  automatically become part of a large worldwide community of developers. To talk and share ideas with developers all over the world is an experience on itself and very inspiring too.

So, about Windows Azure and the contest

Now two weeks ago i was at the PHPbenelux conference where Maarten Balliauw announced the PHP on Azure contest. At first, this contest didn’t really get my attention because i wasn’t aware of what all the cloud computing ‘hype’ is all about in the first place.

After reading about Windows Azure and the concept of cloud computing in general i was surprised by it’s simplicity. It sounds like something very complicated, but in fact, from a developer perspective it is not.

From my perception running applications on Windows Azure, or cloud platforms in general is a developers’ playground full of possibilities, how awesome is that?!
How close to reality my perception will be has yet to be seen though, because i’m participating on the contest!

About the project

The project i’ll be working on for this contest is one that i’ve been playing with in my mind for a while now. It is an online (collaborative) documentation publishing platform.
Now this is actually a pretty simple concept. It’s a platform where people can collaborate on an information collection. Whether it be an online book, reference guide, manual or what other type of information you want to present online.
The added value of when presenting information on the web is that you can add something very imporant to the content: metadata. This metadata can be anything, really. You can add or embed pictures, video’s, files and links. Or, to improve interaction even more it is possible to start discussions or share the information on social platforms.

Now you might associate this concept with Wikipedia, but in fact it is nothing like it.
I’ll list some of the features of my concept, and you’ll see why it is not to be compared with Wikipedia:

  • Each information collection (or project) is presented seperately. They are all seperate projects, like with books.
  • It is possible to use a traditional publishing workflow: Writer > Reviewer > Editor
  • Content version control might be implemented
  • The look-and-feel of every individual information collection (or project) can be altered. Consider each project a seperate website.

It is just an idea, yet

For now, all of the above are just ideas. During the development my idea of the concept might change and i have to see what is possible to develop before the contest deadline.
However to me this project is a great way to put all the new interesting things that i’ve learned in the past months into practice. And for some reason, the project being an entry for a contest makes it easier to motivate myself ;-)

I think the project is a perfect fit for a cloud platform like Azure. Because with applications that *could* have a lot of users, an unknown amount of content (both textual and binary) it is really awesome that you do not have to worry about scaling options, but just focus on the application itself: It actually makes designing the application easier from the ground up!

Development platform and software

One of the first things that really caught my attention about Windows Azure, is that it comes with it’s own local development environment. You can use this environment by installing the Windows Azure SDK. I will use this environment, along with IIS7, SQL Server Express 2008 R2 and PHP 5.3 to develop the application. As you might expect, the framework i’ll use for developing this project will be the Zend Framework.

I think this setup is the best way to deploy my project on the Windows Azure platform, and to make full use of all the components it offers.

Are you in?

So that describes my project for the contest. If you’re still reading this blog post it looks like you might be interested in taking a shot at the PHP on Azure contest as well.
If so, i would like to get in contact! Let’s share idea’s, approaches and practices about Windows Azure or PHP apps in the cloud in general. Post a link to your blog in the comments or find me on Twitter!

If you want to start right away, you can find more information about the contest, and you can start right away with these Windows Azure introductionary account offers.

Over the next couple of weeks i’ll be writing some more (technical) blogposts about the PHP on Azure contest. So if you’re interested, take a regular look on my weblog, subscribe to the RSS feed or follow me on Twitter!

Posted in PHP, PHP on Azure, scalability | 1 Comment