Home > Programming > Ease into it…

Ease into it…

September 5, 2010 Leave a comment Go to comments

So let’s do a quick post to get things rolling. The Python programming language has grown on me quite a bit in the past few years. My appreciation for the language grew even more when I had to dive into Javascript recently at work. There are several similarities between the two, both being dynamically typed scripting languages. It was almost humorous though, how so many of Javascript’s initial differences were annoyances. And I think there’s more to it than just learning curve difficulties. Just a few examples:

  • Curly braces and semicolons. Reduces the efficiency of a scripting language.
  • The var keyword. It just seems superfluous coming from Python.
  • for (x in myarray) {} x is assigned the array indices, [0, 1, 2, …], not the array items like I expected. Means there’s an extra line of code to actually access array items.

I imagine Javascript will also grow on me as I use it more. But I doubt it will ever even begin to approach Python’s status as a great, elegant language in my book.

Advertisements
Categories: Programming Tags: , ,
  1. September 19, 2011 at 1:04 AM

    The annoyances you stated in Javascript remind me of all the things I have learned to like about Ruby. Albeit, not python and slightly off-topic, I started learning Ruby recently after having known Javascript for several years. I found myself, out of habit really, trying to use semicolons and define variables.

    I think browsers need to assume a new standard for client-side scripting using a Ruby or Python based client-side scripting engine.

    • September 19, 2011 at 11:51 AM

      Besides running through a couple of Ruby’s web examples, I haven’t had time to really try it out. Do you prefer it over Python?

      Agreed about needing a Javascript replacement, something that’s designed with today’s (and tomorrow’s) use cases in mind.

      Maybe that’s Google Dart?

      http://en.wikipedia.org/wiki/Google_Dart

  2. September 19, 2011 at 2:19 AM

    I urge you to never use javascript’s for…in construct.
    Use a traditional for loop if applicable, Array.foreach if implemented in your browser/runtime, or another implementation supplied by jQuery/etc unless you’re fully aware of how it works.

    for…in iterates over all properties of an object, including ones set via Object.prototype or any library you use. Iterating like this over an Array is slower than a traditional for loop and keys are not guaranteed to be in any order.

    http://stackoverflow.com/questions/242841/
    http://stackoverflow.com/questions/156696/
    http://stackoverflow.com/questions/5510772/

    TL;DR: Just don’t if you don’t know what Object.hasOwnProperty() does.
    I hate to be so anal about it but most people who use that construct don’t understand it.

    • September 19, 2011 at 11:54 AM

      Ahah thanks for the info! In the time since I wrote the post I’ve started using jQuery quite a bit, so I did start using $.each(), but I still didn’t know the fundamental problem with for…in on Javascript objects. Makes sense, good to know.

      I still only know just enough Javascript to get by… a dangerous situation.

  3. September 19, 2011 at 12:01 PM

    On point to make is that Javascript needs curly braces and semi-colons, or their semantic equivalent- since JS was designed to be enhanced by, but not require white-space.

    Whereas python can get away with leaving out ; and {} by requiring \n and instead.

    I believe requiring var is their compromise for local vars vs global vs this.variable.
    I think Python requires you to do this. everywhere – does it not?

    • September 20, 2011 at 12:45 AM

      Thanks for the comment. Until you spurred me to go look, I didn’t know omitting “var” in a Javascript assignment like “x = 5” creates a global variable. So in Javascript global variables are really the default (you have to opt-out of global by using “var”), while in Python local variables are the default and you must use the “global” keyword to do otherwise.

      Seems like that Python’s default fits better with the conventional wisdom that globals are best avoided.

  4. September 20, 2011 at 10:45 AM

    Python actually isn’t dynamically typed, just so you know.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: