I’ve been learning Arduino recently (as a hobby venture rather than a commercial programming one) and from a programmer’s perspective, I’ve been very impressed with what can be done with such a simple bit of kit. Working within it’s relatively constrained environment has forced me to think in different ways from the usual big enterprise system world that I’m used to spending my days in, and that’s been a refreshing change.
Some early prototyping
My first project has been some hardware for camera shutter control plus a simple state machine implementation to fire the shutter at regular, precise intervals for time lapse photography. Earlier this month I wrote up some basic getting started details in an arduino camera control post over on my photography blog, aimed at photographers interested in going down a similar route. In the week or so since I wrote the original blog post, the little piece of software has grown steadily via half hour coding bursts here and there to allow me to shoot all sorts of increasingly complex scenarios which would have been both error prone and time consuming to do manually.
I think this illustrates quite neatly one of the joys of being able to program even a simple device like this. It shows that the value of being able to code isn’t just to earn a living from it (although that’s nice). Often the more immediate benefit is that you can simply build stuff which helps you out, whether that’s a few scripts to automate tedious stuff in your day to day job or to help you get something that needs to be done accurately right every time.
The end result of this is that more of your human brainpower can be used for what it’s really great at – dealing with more creative and unexpected tasks rather than having to repeat the same thinking time after time. While obviously different people have different aptitudes and coding’s not for everyone, there seems to be increased interest at the moment in basic-level programming as a useful general business skill and I think that’s a good thing. Personally I wouldn’t want to be without it.
There’s an interesting guest post over on the Help Training Courses blog today on the Future of Social Media in Business. There are some useful insights there that certainly fit with my experiences of using social media to build good, positive business relationships rather than spamming people with endless broadcasts. The key part of the message in my opinion is this:
Remember the power of social media is not in broadcasting, it’s about relationship building.
…which to me means Don’t see Twitter as an advert on the radio, see it as a networking event. If you do that, you start to realise not only how to use social media in your business but also who needs to be involved. The who as well as the what is one of the keys to a successful strategy.
Much has been written about the subject of refactoring code over the years including some very well known work by Martin Fowler. There are many resources on the ‘what’ and ‘how’ of refactoring out on the web so I don’t intend to go back over those aspects of the topic here other than to mention that if you’re new to the concept, refactoring is essentially improving the internal design of software without changing its external behaviour.
One thing that’s traditionally been difficult to explain or understand though, and remains that way, is the question of ‘why to refactor’. Putting work into something without changing what it does can seem like it’s going to be a big load of wasted effort. At best, it often looks like a speculative investment of time with intangible payback. Experience shows though that refactoring can pay big dividends, avoid wasted effort and create consistent direction, focus and efficiency on a project.
Martin Fowler writes that refactoring makes changing software easier in the future. That’s very true but to those new to the concept (or non-programmers) it can still be difficult to explain how that happens. It sounds a little bit ‘magic’.
For me it simply comes down to this though. Writing code isn’t only about solving a problem. It’s also about describing the solution. Really great code not only functions well but explains to other programmers what the problem was that caused the code to be written and how it was solved. That’s where the ease of change comes in. In contrast, if code is incomprehensible, often the reasons behind it aren’t fully understood when changes are needed. That tends to encourage either starting again (wasted effort), large amounts of careful tracing through the code, or unintentional breaking of parts of the system when changes are made without fully understanding their impact.
Of course, you can write comments and supplementary documentation around code and they both have their place. Documentation can easily get out of step with code without extreme care though and it’s difficult to test completeness or accuracy of the written word. Well written code doesn’t have that problem. The documentation can’t get out of step with the implementation because the implementation is the documentation.
Of course, pragmatism and business forces do count in the real world and sometimes refactoring isn’t justified, for instance on a soon to retire system. Often though, taking working code and making changes which help to teach the current and future teams about the nature of the problem it solves is well worth the effort. And just like other forms of documentation, refactoring is worth doing before the original learning and knowledge gained by the very process of creating the solution is lost and has to be re-learnt all over again. Used wisely, refactoring’s a valuable investment in team knowledge and ongoing clarity of thinking.
Facebook has changed the way it’s like button for third party websites works over the weekend to post a full feed story rather than just a simple ‘Recent Activity’ one-liner. I’ve been testing that out via implementations on a couple of sites I’ve been working on and from a site owner’s perspective, this change seems a positive move. ‘Likes’ are now much more prominent in a user’s feed and seem better at attracting further follow-up likes from friends. From a facebook user’s point of view, I see this as an improvement too. If I’m clicking ‘like’ on something I’m wanting to share it with my friends and a full story in my feed seems a better way to do it.
Since the new like button functionality automatically tries to include an image, I found it particularly useful when used in conjunction with the og:image metadata from facebook’s open graph protocol. Using og:image allows you to specify a thumbnail to include in the story and avoids the situation where a random and sometimes inappropriate image is chosen for you.
Implementing both the like button and the open graph metadata are straightforward jobs and there’s a simple guide in the Facebook Like Button documentation.
I reckon some of the best times working as a software designer and programmer are when you manage to make something really quite hideously complex look absolutely trivial to the end user. In this case, some fairly hairy fuzzy-searching, de-duplicating and difference-spotting code for the Help Training Courses Magical Auto Search, running behind a user interface that amounts to entering your email address…
A recent TV ad for Amazon’s Kindle was a bit of a ground breaker as far as I’m concerned in that it actively pushed the cross platform nature of the company’s software as a mainstream selling point. While those of us in the techy community have long seen the benefits of software that doesn’t just run on one OS, I don’t think I’ve seen that used in a TV marketing campaign in this way before. Of course, in this case there’s still the catch that you’re tied to one organisation’s DRM but Amazon aren’t alone at the moment in recognising that there’s a growing demand for software that isn’t limited to just one Operating System. I’m guessing that’s particularly true in the mobile market but it’s becoming more significant for desktop software too.
Looking round the office I’m in at the moment, there are:
2 x Apple Macs running Mac OSX 10.6
2 x Windows 7
1 x Windows XP
2 x Ubuntu Linux
2 x Blackberries
2 x Android phones
1 x iPhone
That diversity would have been really unusual, even 5 years ago but it’s getting more and more common. I think that’s a good thing for end users.
I regularly get asked if one OS is better than another and while I’m happy to advise and don’t tend to sit on the fence, the only honest answer I can give is that they all have different strengths and weakness. Knowing what those are and making an informed choice can sometimes bring big productivity benefits. It’s about gaining awareness of options available and using the best tool for the job and I think more an more people are become aware of those choices.
Of course to make that choice, we need to be not tied to just one operating system in the first place and that’s where cross platform software really helps. As a simple example, I recently helped a couple of users who’d decided to move to Macs (one from Windows Vista and one from a Linux desktop) and wanted to move their mail. Fortunately, both were using a cross-platform email client (in this case Mozilla Thunderbird) and a quick copy of a few files meant they were set up in seconds, with mail settings, passwords, email and inbox totally in tact as if nothing had changed.
I do understand though why some folks think that restricting choice makes the world a simpler place and that’s true to an extent, just as only allowing one brand of beer to be sold would make choices simpler on a night out or only building one type of car might make third party spare parts cheaper. But the reality is, different products suit different people and situations. In the software world, cross platform applications have a big part to play in supporting that choice and the productivity benefits that can follow. At the moment I increasingly bump into situations that suggest to me that everyday technology users are realising those benefits and factoring them into their decisions.
I’m working with Help Training Courses again at the moment on the next phase of the web site. We have some interesting new features lined up to make it even easier to find the right training at the right price.
Included in the first batch of these changes is a new facility to allow training providers to add early booking discounts in addition to the current last minute training deals. The last minute discounts option has been very popular, with some great deals appearing on courses such as ITIL, Access, Adobe Photoshop and Prince2. Feedback so far suggests the new early booking discount feature will be just as popular and creates even more opportunities for training providers to fill courses and for students to find a bargain.
I upgraded my main development desktop to Ubuntu 10.10 a few days ago and my first impressions are of a solid, fast and generally pleasant to use release which I’d highly recommend a look at if you haven’t already. I value a productive and quick work machine and have been using Linux as my preferred development environment for three years or so now for its rock-solid reliability and responsiveness.
Ubuntu 10.10 in operation with the cairo dock
Ubuntu has long been one of the easier Linux distributions for the desktop user to get up and running with and the Ubuntu team really have made the setup process as easy as possible. Once you’re up and running, the debian package management for installing additional software is excellent too and the out of the box look and feel is clean and intuitive.
If you’re interested in giving this latest Ubuntu release a try yourself, take a look at the Ubuntu web site.
There are plenty of fancy tools for performance testing Java web applications but in addition to thorough load and volume testing before an application goes live, sometimes it’s useful just to leave simple perfomance monitoring running full time in a production environment. Monitoring the time the server takes to execute requests on your site can help you spot trends that let you anticipate and deal with problems caused by load, volume, unanticipated usage patterns or simply a database in need of some tuning. A proactive approach to performance tuning can bring big wins in customer satisfaction too, allowing you to pre-empt and avoid issues and keep your site running smoothly for your users.
Fortunately Java Enterprise Edition provides an easy mechanism for intercepting every request on the way in and out of the application in the form of a Servlet filter and it’s really simple to use that to create a performance logger. The basic concept here is to capture the time as a request enters an application, then again as it leaves and log the difference between the two. We can do this in the servlet filter’s doFilter method as follows. I’m using a log4j logger to handle the output but obviously any other logging mechanism would work just as well if you prefer something else:
long startTime;
long endTime;
String path = ((HttpServletRequest) request).getServletPath();
startTime = System.currentTimeMillis();
chain.doFilter(request, response);
endTime = System.currentTimeMillis();
//Log the servlet path and time taken
perfLogger.info(path + "," + (endTime - startTime) );
To finish off the code, I’ve added a couple of configuration options for more flexibility. For starters, I prefer to have a little more control over which URLs get logged than the standard servlet url-pattern mechanism allows. For instance, I may want to only log requests containing the word ’search’ anywhere in the path. The standard servlet url-pattern mechanism doesn’t allow this so I’ve added a parameter ‘url-filter’ to allow full regular expression matching. A second parameter ‘log-category’ sets the log4j logging category to use. Wrapping this into a full example servlet filter we get:
package com.actuanceconsulting.perflog;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
/**
* A simple filter to log the time taken to execute a request. Logging is carried
* out via log4j to give the flexibility to add other data (such as current time)
* and format the log as required.
* */
/*
* Copyright John Patrick, Actuance Consulting Limited 2010
* http://www.actuanceconsulting.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* For a copy of the GNU Lesser General Public License,
* see <http://www.gnu.org/licenses/>.
*/
public class PerformanceLogFilter implements Filter {
/**
* An optional regular expression to use as a filter for the servlet patch.
* Gives more flexibility than the standard servlet url-filter.
* All requests are logged if not specified.
* */
private static final String URL_FILTER_PARAM = "url-filter";
/**
* An optional log4j category to use. The fully qualified class name
* of the filter will be used if not specified.
*/
private static final String LOG_CATEGORY_PARAM = "log-category";
private Logger perfLogger;
private String urlFilter;
public void init(FilterConfig config) throws ServletException {
String logCategory = config.getInitParameter(LOG_CATEGORY_PARAM);
if (logCategory == null) {
this.getClass().getName();
}
perfLogger = Logger.getLogger(logCategory);
urlFilter = config.getInitParameter(URL_FILTER_PARAM);
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
long startTime;
long endTime;
String path = ((HttpServletRequest) request).getServletPath();
if (urlFilter == null || path.matches(urlFilter)) {
startTime = System.currentTimeMillis();
chain.doFilter(request, response);
endTime = System.currentTimeMillis();
//Log the servlet path and time taken
perfLogger.info(path + "," + (endTime - startTime) );
}
else {
chain.doFilter(request, response);
}
}
public void destroy() {
//Nothing to see here
}
}
To get the filter to run, we need to configure it in the web.xml file as follows:
In this case I’ve set my regular expression for the ‘url-filter’ parameter to only match paths containing ’search’. For completeness, here’s the log4j config I used too…
…and that produces output as shown below. The CSV format of this output allows for easy analysis of the results but obviously you can choose whatever format suits you best.
And that’s all there is to it. It’s a simple bit of code but it can really add value when run over a period of live operation. Being able to see historical performance and spot trends can be a powerful diagnostic tool.
The Actuance Consulting blog is written by John Patrick. With over 18 years
experience in a variety of technical and lead roles in the IT industry, John has a passion
for making computer systems relevant to people and a genuine help rather than a frustration.