P is for Programming

Sharing code with Pastebin and jsFiddle

January 27, 2011

As a software developer you will quite often have the need to share your code with others.  While this sounds straightforward, the details are the issue.  Ever tried to share code with Skype

Let’s try it – how easy is this code to read?

static void Main(string[] args)
{
var index = 0;
foreach (var result in Hello())
{
Console.WriteLine(result);
index++;

if (index >= 10)
{
break;
}
}

Console.ReadLine();
}

private static IEnumerable<string> Hello()
{
while (true)
{
yield return "Hello World";
}
}

As C# developers we’re used to Intellisense and syntax highlighting – there’s no getting around it.

Pastebin to the Rescue

Take a look at the same snippet with Pastebin.

Pastebin

There is no registration, it supports pretty much every language you can think of and you can share your snippets with a simple URL.  It even tracks corrections or amendments.  Every developer should be aware of it.

Sharing Web snippets with jsFiddle

Where Pastebin falls short is with the sharing of Web snippets.  While Pastebin can certainly highlight and share Web snippets, this is quite often not sufficient.  Have you ever tried to help someone with a styling issue?  Tried to explain a certain JavaScript effect that is going awry?

That’s where jsFiddle comes in.  Here’s the official line:

JsFiddle is a playground for web developers, a tool which may be used in many ways. One can use it as an online editor for snippets build from HTML, CSS and JavaScript. The code can then be shared with others, embedded on a blog, etc. Using this approach, JavaScript developers can very easily isolate bugs. We aim to support all actively developed frameworks – it helps with testing compatibility.

I came across jsFiddle while trying to debug a styling issue – I had an issue where a list of items were displaying perfectly in Internet Explorer, but in Chrome or Firefox the items were being cut off.  I proceeded to ask for help on StackOverflow, but while I could explain my issue, I couldn’t really show it. 

Check out my example on jsFiddle.  What I find really cool is that you can view this example in Chrome or Internet Explorer and actually see the difference.

Do you know of any similar tools?  If you do, leave a comment.  Happy coding.

Tags: PatternsPractices

  1. Rohland says:

    One I use often is http://jsbin.com. That said, I hadn't heard of jsfiddle.com before, looks awesome and includes ExtJS support :)

  2. Jaco Pretorius says:

    Very cool, thanks

  3. Elijah Manor says:

    Yeah, I use jsFiddle all the time. It has some nice features like JSLint, embedding features for your blog, if you have an account you can have customized URLs, you can version your code, etc…

  4. RC1140 says:

    Personally prefer gist , from github (https://gist.github.com/). This usually means everything is source controlled by default as well

  5. BigAB says:

    I think you should have embedded the jsFiddle example just like you did the pastebin one. Just to show others it can be done.

  6. BigAB says:

    @RC1140 You can have both at the same time!
    http://doc.jsfiddle.net/use/github.html

  7. Krzychu Kula says:

    You can try jsdo.it (I sow somewhere a how-to for embedding code)

  8. Jaco Pretorius says:

    BigAB – yeah I didn't embed either the Pastebin or jsFiddle examples (Pastebin one is a schreenshot) – it's a little tricky to get right with Blogger. But you're quite right – you can embed the code with both frameworks – very cool

  9. Jaco Pretorius says:

    @Krzychu – very cool! It doesn't allow you to post anonymously, but the registration is really easy (OpenId, woot). Very very nice.

  10. Cardin says:

    And I was still thinking that the only way to post on a blog was to use SyntaxHighlighter (WordPress). o.O Pastebin looks really good for places like StackOverflow.

  11. Jaco Pretorius says:

    @RC1140 – that's a very nice alternative to Pastebin. https://gist.github.com/799983