Bending jQuery to my will

Like many people, I have a vanity domain name, www.stuart-grassie.co.uk. Many moons ago I used to host my blog on there, but for a variety of reasons I bought this domain and switched my blog here. So I put up a basic index page, listing a few interesting factoids about me, and what I do. When I say a “basic” page, I mean basic – black text on a white background, almost as if it was just few words typed into a text file – although there was semantically correct and valid xhtml behind it.

The other day I came across a link, 30 Examples of Extreme Minimalism in web design and it inspired me to do a small make-over on my vanity domain name site. Which I think has turned out fairly ok. Athough that isn’t the point of this blog post.

As there is not a massive amount of information (after all it’s just a “here I am, here’s some information about me, here’s how you can contact me” site, all of it is on one page. I’m using jQuery to hide everything, until the user clicks a link to see the information they want. I used jQuery UI, to use a couple of basic slide in/out effects. The actual effects are “clip” and “blind”, using “toggle” to turn them on/off. All the other content apart from the “frontpage” is hidden.

Initially, I had it working so that you could click a link, and the requested “page” would transition into view, the “frontpage” would transition out of view, then I had a <span><back</span>, which I made react to mouse clicks, using jQuery, and you could then click back to the “frontpage”.

<div id="main">
Maybe you would like to read this <a id="examplelink" href="#example">example</a> page.</div>
<div id="example">
 
This is some example content.</div>
$(document).ready(function(){
    $("#example").hide(); // Hide the example on page load
});
 
// detect when the link is clicked, and toggle the two<div>'s
$(function() {
    $("#examplelink").click(function() {
	$("#example").toggle("clip", {}, 500);
        $("#main").toggle("blind", {}, 500);
	return false;
    });
});

In my excitement to get this learned and done (I’ve never used jQuery before), I’ve lost the original code, but that’s essentially what I had, without the code to detect when the fake back button back is clicked – all it did was to run the toggle again, which returned the user to the “frontpage”. I had to to do it this way, because the browser forward/back doesn’t work.

Clearly, this is not an ideal situation, because it has broken one of the cardinal web design guidelines, by breaking the users browser, by rendering the forward/back buttons inoperable.

I’ll show you what I did next time.

Share this post:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
Posted in Coding | Tagged | Leave a comment

Adding logging to an application

Nearly every application I have ever used has produced some sort of log output, usually to record details of exactly what the application was doing when an error was encountered. To be perfectly truthful, it is not something that I have ever really given very much consideration to in either my own or work applications, there have always been more pressing things to attend to, such as meeting (unrealistic) deadlines, or implementing some interesting new feature. I’ve used log4j before in a work application, mostly because someone else had already done all the configuration.

However, the application that is currently occupying the main focus of my attention at work had (almost) no logging in it what-s0-ever, but given the nature of the application, it can be very difficult to debug exactly what is happening internally using breakpoints. Again, the nature of the application doesn’t really lend itself well to showing MessageBox’s all the over the place.

MessageBox.Show("Log message");
 
// or..
 
Console.WriteLine("Log message");

How many times have you done that, or the equivalent, in an application? If you are like me, then you’ll have done it hundreds of times, probably in the same application. It’s really quick and easy to do, and can be very useful. You just have to remember to take it out of the Release build. What a pain.

A better way

Using a proper logging library (as I have now discovered) allows you much greater freedom. Freedom to not have to (overly) worry about leaving in Log.Debug(“Some message”) all over the code. Freedom to be able to configure what is logged, where it is logged to and how it is displayed. Freedom to receive concrete data on how your application is behaving in the real world, which will enable you to find and fix bugs faster than you would have thought possible.

There are a multitude of logging libraries available:

Now, while it would probably be an interesting exercise to write a small logging library, the world doesn’t need another one, so I immediately discounted that option.

I’ve used System.Diagnostics here and there, and an ex-colleague wrote a small logging library based around using Trace and Debug, but that is getting dangerously close to writing your own logging library, so again that’s out.

The Logging application block from the Enterprise Library just seems too weighty, and too much of a pain to configure (I can probably be proved wrong though), so I discounted that as well.

The object guy’s logging library seems pretty good, but the documentation and examples kind of suck, and it doesn’t look like it’s been updated in a while, so after playing with it a little bit (and not really liking it all that much to be perfectly honest), I discounted that as well, although it does seem to have a lot of fans on Stack Overflow.

NLog has plenty of documentation, and seems pretty straightforward to use, and it too has a lot of fans on Stack Overflow, but again, it’s not been updated in a while, so I discounted that too.

Log4Net to the rescue

Log4Net is part of the Apache Logging Services project, along with Log4j, and Log4Cxx. It is a C# port of Log4j, and works amazingly well. I am not going to write about how to set it up and configure it, as others have done that already, and the documentation is excellent.

I’ve read here and there on some blogs, a few message boards and some email lists, that log4net is difficult and time consuming to set-up, and that it is hard to use. Which is nonsense, because after ten minutes (which included downloading the latest version) I had my first small test application up and running and outputting debug messages to the Output window in Visual Studio. Pretty swish indeed.  I did spend some time reading about the best ways to use log4net, and I still do, but I didn’t find the initial set-up to be inordinately difficult at all.

On the whole, I am impressed with it’s ease of use, I’m now starting to use it in my own personal projects, and it is proving to be invaluable at work.

Share this post:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
Posted in C#, Coding | Tagged , | Leave a comment

Writing a generic plugin manager in C#

Update:

I have written another post on this subject with some code examples here: http://temporalcohesion.co.uk/2009/11/02/more-on-the-generic-plugin-manager/

Update 2:

There is another follow on post here: http://temporalcohesion.co.uk/2010/03/17/even-more-on-the-generic-plugin-manager/ where I talk about what I’ve learned.

One of my current long term hobby project is a map/world editor that I’m writing for Andy’s (work in progress) game engine. It is still in a quite rudimentary stage, however, I have gotten an important aspect of the application into a fairly mature state already, and that is the plugin system.

I don’t want to get into too much detail over things that are available via some simple searches on google, so don’t expect too much code. There are some good articles here, here and here, which delve quite deeply into this, and how to develop a plugin framework in general. I developed my plugin framework based on the code in this book, which incidentally is a very good book.

Also, in my own google searches on this subject, a lot of the articles I found are getting to be four years old, in a few instances even older. Not that this automatically makes them invalid as sources of information, it’s just that they may not reflect best practices in modern C#. (Not that I am claiming to be a Jon Skeet-like C# guru). Also, quite a few of the ones I found make use of an XML configuration file to hold a reference to the plugins. I’m not a big fan of this type of thing, I much prefer convention over configuration.

Why Generic?

I’ve written a plugin system for the Editor because I want to provide a way to make it easily extendible in the future, without having to change the core application, to enable it to perform actions that are above and beyond it’s core capabilities. Byond that basic requirement, there are a couple of other reasons to make it a generic plugin framework.

Firstly, I’ve made it generic because I wanted to make a distinction between different types of plugins, such as a type of plugin for resources, and a type of plugin which adds functionality into the application (such as tools, UI widgets etc).

Secondly, and perhaps most important (to me, anyway), is that I’ll never need to write a C# plugin framework again, for any of my personal projects. I’ve had a go at writing a plugin framework before, a few times for none-work projects and once for a project at work, and I finally realised what my problem was – reinventing the wheel each time.

The plugin framework I’ve written ensures that I don’t have to worry about this the next time one of my pet projects needs a plugin system. The Editor will in fact have two types of plugins available, one to extend the types of data sources it can load in resources from, and another to provide additional functionality, like tools.

I’m fairly pleased with the way that it has turned out, and it works quite well in practice.

Share this post:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
Posted in C#, Coding | Tagged , | 4 Comments

Building the Project Euler framework, part 3

In part 2, I showed you an improved, although still pretty basic problem runner framework for Project Euler. I did leave out some things though, and I’m going to try and explain them now.

Firstly, I haven’t really shown how I use the Problem interface. You can see it in part 1 of this series of posts. In Eclipse, you can create a new class in a package, which should bring up the “New Java Class” dialog. Give the class a name – for Project Euler problems I’ve chosen to name them “One”, “Two”, “Three” etc. You can then add an interface that the class is to implement, click ‘Add’, and type in Problem. You can also choose some methods to stub out, tick ‘public static void main(String[] args)’. Click Ok, and you should get something like this:

package co.uk.temporalcohesion.euler.problems;
 
import co.uk.temporalcohesion.euler.interfaces.Problem;
 
public class One implements Problem {
 
	public String answer() {
		// TODO Auto-generated method stub
		return null;
	}
 
	public int id() {
		// TODO Auto-generated method stub
		return 0;
	}
 
	public double time() {
		// TODO Auto-generated method stub
		return 0;
	}
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
 
	}
 
}

I can hear you asking why am I including then main(String[] args) function if we already have a class that is capable of running the problems (the main Euler class)? Well, what I do to create is to create a Euler object in the problems main method, and get it to run the problem, like this:

        /**
	 * @param args
	 */
	public static void main(String[] args) {
		new Euler().run(1, true);
	}

So the problem class is running itself using the Euler object, which knows how to find the problem, instantiate it and run it. I find doing things this way is easier when working on the problem, as you can simply run the problem as a java application, and it will output the result in a standard format we are expecting to to the console in Eclipse.

Thinking about it, you might be wondering – what’s the point of all this, it seems a little excessive for something that can be done fairly easily? Well – I’ve done it like this because the whole point of me doing the problems on Project Euler, is to practice problem solving and become more comfortable in my use of Java. So I think what I’ve done is pretty valid in that regard.

Share this post:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
Posted in Coding, Java, Project Euler | Tagged , , | 1 Comment

Building the Project Euler framework, part 2

In Part 1, I showed a basic problem runner framework for Project Euler, however there are a number of ways in which we can improve it. For example:

  • How can we run a specific problem?
  • How can we hide the answer, but still run the problem?
  • How can we avoid manually adding problems to the List of problems?
  • Not really to do with the framework, but how can we automate building everything?

I’ll demonstrate some ways that we can do all that, except for the 4th option, which is handled by Ant.

Improving the framework

The first thing that we can do is to add a utility function that handles showing the answers, this way we only have one place in the code that we need to update when we want to change how the answers are shown.

private void showAnswers(Problem problem){
		System.out.println("Problem: " + problem.id() + ". Answer: "
				+ problem.answer() + ". Time: " + problem.time() + "s");

}

To run a specific problem, we need to overload the run() function to access the problem we want, and show the answer.

public void run(int i) {
		try{
			Problem problem = (Problem) problems.get(i);/* problem list starts at 0 */
 
			if (problem != null) {
				showAnswers(problem);
			}
			else {
				System.out.println("There doesn't appear to be an answer for problem " + i);
			}
 
		} catch (IndexOutOfBoundsException e){
			System.err.println("There doesn't appear to be an answer for problem " + i);
		}
 
	}

As you can see, we get the specified problem out of the list, and use our new showAnswers() function to display the answer.  I’ve tried to include some good error checking – we might try to get a problem that doesn’t exist.

In order to prevent the answer from being shown, we can add a boolean parameter to the run() and showAnswers() functions.

private void showAnswers(Problem problem, boolean showAnswers){
		if(showAnswers){
			System.out.println("Problem: " + problem.id() + ". Answer: "
					+ problem.answer() + ". Time: " + problem.time() + "s");
			}
			else {
				problem.answer(); /* we still need to work out the answer */
				System.out.println("Problem: " + problem.id() + ". Time: " + problem.time() + "s");
			}
	}
 
public void run(boolean showAnswers) {
		for (Problem problem : problems) {
			if (problem != null) {
				showAnswers(problem, showAnswers);
			}
		}
	}

Dont’t forget to change the overloaded run(int i) to run(int i, boolean showAnswers). This way we can control exactly  whether to show the answers when we run all the problems, or to show the answer if we run a specific problem.

One thing remains to do, and that is to correctly parse the command line arguments to control whether the answers are shown or not. We want to handle something like this:

C:\development\euler>java -jar ProjectEuler.jar 42 -noanswer

Where 42 is problem 42, and -noanswer clearly specifies not to show the answer. We’ll also need to handle all combinations of this as well, such as:

C:\development\euler>java -jar ProjectEuler.jar 42

Which should show the answer. I’m not going to show my code for parsing the command line arguements, I’ll leave that as an exercise for the reader, as I believe that it is adequately covered elsewhere on the internet, and in any number of Java books.

The more astute among you will notice that I’ve not mentioned how we are going to avoid manually adding problems to the List of problems. I’ll cover that next time.

Share this post:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
Posted in Coding, Java, Project Euler | Tagged , , | Leave a comment