logo

Learning to Code Properly

For ages now I've been trying to find the time to learn JavaScript. That statement might surprise some of you who I know consider me as something of a JavaScript "guru". Bear with me.

As with most languages I'm self-taught and have learnt by the modification of example code. Undoubtedly this isn't the best way to learn. At some point you need to go back to basics.

Sometimes I think my problem is that I misunderstand too much about JavaScript to ever be able to truly understand it. Is there any way back or do I already have too many bad habits!?

<analogy quality="medium">I guess it's a bit like learning to speak English by coming to live in a Northern working-class town in England (like one where I were born). You'd pick it up fairly quickly but you'd learn some bad habits along the way, such as not using the word "the", saying "Am going te pub" or "Working down pit". You'd probably use double negatives such as "I haven't got no money" and way too many clichés, such as "At the end of the day I turned round to him and said..." which do nothing but pad-out what you're actually trying to say.</analogy>

My accent and way of speaking has changed considerably since I left home. Mainly due to my stint down south where they tend to talk proper (like what the Queen does). Having a job in "business" means you need to communicate effectively with people. This means talking clearly and concisely, which I do do now. This is especially useful when dealing with international clients on the phone.

However, although my speech has improved I still don't know the basics of my first (only!) language. My education didn't really dwell on the grammar side of things. Although I do consider myself something of an expert on the proper placement of apostrophes I still don't really know what a past participle is or what a split-derivative means. Although I can write in a way that I think is easy for others to read I don't know if it's correct.

Take this question from an online quiz at the BBC as an example:

Q. Which sentence has the adverb in the wrong position?

  1. Suddenly I felt sick.
  2. I suddenly felt sick.
  3. I felt suddenly sick.
  4. I felt sick suddenly

Now, to me, all of the sentences work (in that they make sense and convey the message to the reader) and I'm not sure which is correct or exactly why or what an adverb is for that matter.

Writing code is the same in many ways. Especially with JavaScript. There are numerous ways to achieve the same thing. Chances are most ways will work but only one way is the "proper" way. Coding the proper way means it's more likely to be understood by a greater number of people and make you seem a much cleverer person. Nobody wants to be the Vicky Pollard of the coding world do they!

What I want to do is learn to code JavaScript properly. More on that next week...

Comments

  1. I tend to write javascript in a "ay up me duck" fashion - unless doing enough to justify an object orientated style.

    For anyone who lives outside of the middle/east of england (where Jake and I live) - "ay up me duck" is real english for most people round here.

    • avatar
    • Jake Howlett
    • Fri 7 Nov 2008 06:33 AM

    Anybody who doesn't know what Steve is on about obviously can't speak proper English!

  2. I can't recommend enough Douglas Crockford's video series in the YUI Theater:

    http://developer.yahoo.com/yui/theater/

    Specifically, "The JavaScript Programming Language" and "Advanced JavaScript"; "An Inconvenient API: The Theory of the DOM" is also very good, but the first two are extremely enlightening: he doesn't just describe the core underlying principles, he actually explains why the various techniques most of us have been accustomed to are not as effective as convention would suggest.

    One quick but amusing example: the still-common convention of surrounding one's script tags in HTML comments started as a workaround for a bug in (I think... been a while since I last watched these videos) Netscape 2, and really hasn't been necessary for about a decade, yet a surprising number of web developers still adhere to that convention without wondering why... they've just seen so many other people do it, so there obviously must be a good reason for it. Doug provides numerous other examples with regard to the structure and behavior of the language itself. For anyone looking to revisit their underlying assumptions about JavaScript, these videos are an invaluable head start.

    • avatar
    • Jake Howlett
    • Fri 7 Nov 2008 06:53 AM

    The "Crockford talks" were going to be one of the references I linked to next week Tim. I agree. Very useful viewing.

  3. A book that helped me a lot was Pro JavaScript Techniques by John Resig (he is also the author of the JQuery framework, although the book is framework-agnostic).

    • avatar
    • PaulE
    • Fri 7 Nov 2008 09:06 AM

    HHumm

    "I felt suddenly sick" - that's the wrong one.

    Self taught programmers are a bit of a bane for me :( ... only a few weeks ago I ended up having to debug a 1000 line bit of LotusScript where every variable was global.

    • avatar
    • Ron Yuen
    • Fri 7 Nov 2008 10:17 AM

    Split derivative ???

    Is that where the bank gets one part of the proceeds and the Financial Advisor gets the rest (:-)

    Leaving you with sweet f.a. (and I don't mean financial advisor ...)

    Or were you, perhaps, refering to the most famous split <infinitive> in SciFi History

    'To Boldly Go...'

    Best wishes on your I Must Learn Javascript Venture. arguably the most misunderstood and probably the most ill-used language in IT history. Hope you get back alive.

    Just one teeny word of advice Grasshopper ...

    Don't ever submit your code to DougC's jLint. Or if you do, at least remember to take a stiff tot first.

    The resulting torrent of output endlessly iterated is surely designed to humiliate, demonstrating that God is not only a geek who hates programmers but probably learnt his trade in Guantanamo.

    You will not only be left requiring dental surgery following all those molars grinding, but it's likely to keep you working for the rest of your life to pay The Therapist.

    Regards

    PS Wo'ts rong wiv self tort programerz ? I are one.

  4. Seriously -- if it works for you, go with it. ;) Don't worry about what's more "proper." Your users aren't going to see the code, anyway, and as long as it's _fast_ enough for them, you're probably coding it wonderfully, anyway (bad code is very often slow -- that's the only real objective criteria I use, personally).

    For instance, I developed a lot of respect for Jack Slocum over at ExtJS.com when I noticed that was using tables for his organization in the computed HTML that the Ext library produces, instead of the fancy DIV-based floating garbage that everyone tries to push as "proper" HTML. :P Tables work, they're still part of the spec, and from my own personal experience I have a damn sight easier time working with them than I ever had with DIV tags.

    Use what works, and don't worry about propriety. ;)

  5. As odd as it may sound, I thought that the book "AJAX for Dummies" covers the formatting of Javascript quite nicely. The few chapters dealing with Javascript are fairly short but to the point.

    I'm like you Jake, I was born and grew up in the South (USA) where we use many phrases or slang terms. Working for a Global Organization which is headquartered in the North which typically speak proper, I realized that I had several communication improvements as well.

  6. You might also ask 'how do I code LS correctly'?

    Is it better to create OO-like structures, to create classes, to store code I'd like to reuse as agents, something else?

    I think the programming/English analogy is pretty good. 'Correct' depends on context and intent. If you're sitting with your buds in a bar (or is that blokes in a pub?), you're NOT going to speak the Queen's English; in the U.S. you'd most likely speak the outgoing President's drawl or something Palin-esque. If you're giving a speech to a large gathering of programming peers, you'd most definately make the lingo more upscale.

    Same with code. If you're writing a small project that's unlikely to need either scaling up or much multi-person collaborative development, do what you want. If you're working with a team, develop your own internally consistent dialect and stick with the rules. If you're doing Open Source, try and stick to more global norms.

    Having said this, I understand where you're coming from. Being self taught means not having the understanding of the 'good English' and a language that allows you to communicate with less than proper structure doesn't help you get to where you want to go. It would be nice to understand when dialect A is 'best' and when 'English Standard' is preferred and WHY.

    Good luck - I'll be watching how this goes.

    Fumbling towards the light...Doug

    ps - early on when I was just getting into using Notes (v3.2) my boss watched a consultant build a small demo app for us. She was able to understand that A) the thing was most likely written in C and B) how it was implemented. I thought she was nuts. As time passed, it's become abundantly clear that she was right on both counts...I'm destined to nibble at the edges (and yet still do nice/useful work).

  7. Crockford's good, yes, but I'll point you to Eloquent JavaScript:

    http://eloquentjavascript.net/contents.html

    There's a lot more "there" there than it may seem at first glance, and the writer isn't afraid to treat JavaScript as it really is: Scheme with curly braces. I was rather expecting my eyes to glaze over (and the early chapters looked like they would fulfill the prophecy), but what they actually did was fly open like a cheap window blind will on a day when you really, really need a midday kip. Highly recommended.

  8. I have found various books and articles from Sitepoint quite helpful.

    http://www.sitepoint.com/books/library/

    http://www.sitepoint.com/books/javascript1/

    http://www.sitepoint.com/books/jsant1/

    http://www.sitepoint.com/books/dhtml1/

    http://www.sitepoint.com/books/jsdesign1/

    http://www.sitepoint.com/books/ajax1/

    They publish in hard cover and/or PDF format and usually provide code examples.

    I look forward to your next post on the subject. Thanks.

    • avatar
    • Jake Howlett
    • Mon 10 Nov 2008 05:47 AM

    Thanks for the links and feedback chaps. Will process all in good time. I'm looking forward to reading the eloquent JS link Stan!

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Fri 7 Nov 2008

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content