Java[Script] Online Learning

I’ve been doing the codecademy.com JavaScript classes, and following along with Code Year.  I don’t really need to know how to, say, code a function in JavaScript.  But JavaScript has intrigued me for a while now, particularly after I got over my early animus against it, back when I felt it’s name was just a confusion for Java, and a dangerous new development. [I have been online for a very long time.]  I think it makes an interesting choice as a first language,  much like BASIC when I was first learning programming, JavaScript is there on every machine available to the budding programmer.  And also on their tablets, smartphones and any other device which has a graphical browser.

It’s also just good for me to see how other people program– perhaps especially those who know their code and style will be visible.  In my current position, I manage a single other tech employee, and while the county does have other programmers, we are organizationally separate from them.  IT professionals (or as I usually cal l us “Geeks”) learn from each other.  It’s hard to get that kind of training from college, because just a  few years out of college and the tech has changed, the best practices have altered, and things are new again.  We have to always keep learning, and we do that primarily from each other.  Thankfully there’s an Internet full of us geeks out there to learn from. so I’m not completely isolated.

A friend pointed me to CodingBat which has both Java and Python coding puzzles and practice.  I’ve been working through the Java ones, which highlights even more of the differences it has with JavaScript, even without considering object models or language style and ethos.  I have been fighting with Java’s substring on several of these (often going about the code in a different, if equally efficient method) but I’ve hit several that really need to make use of substring.

In PHP, which is the language I’ve spent the most time in, substring takes three parameters: the string, the start index, and the length of the substring you want.  (Strings aren’t objects in PHP like they are in Java and JavaScript.)   This seems reasonable to me.  I want the four characters starting with the second one, in php I say

substring($str,1,4);

And if I pass in the string “Hello World”, I get “ello” out.

JavaScript opts for a different implementation, taking the first index, and the final index.  So in the same example, but in javascript I’d get something that looks like this:

str.substring(1,5);

That seems reasonable to me, start and finish.  It’s like an interval in math (although a closed one).

Java pretty much confounded me though, I tried both of those and neither worked, so I finally did what I should have started with, and searched the docs.  Now I’d looked at it, but it was just substring(startIndex,endIndex), which matched my understanding so I hadn’t looked further.  But when I read it today, I realized  that endIndex is actually not inclusive in the Java implementation.  Which means that the proper code for Java is:

str.substring(1,6);

Of course, in Java I also have to make sure my string is at least 7 characters long before invoking that. JS and PHP will do something intelligent (returning “”, for instance, or just returning what is there, and not necessarily 4 full characters).   Which makes me wonder sometimes at the mindset of the Java people.  I’m not saying boundary checking is bad — it’s necessary.  But it could have been in the function, and built into the language instead of elsewhere.  I guess this makes sense because substring length = endIndex – startIndex. But somehow it seems counter-intuitive to me.

Ultimately though, it’s what it is.  Languages all do the same thing, or so my college COBOL professor said,  “Input, Processing, Output.  Learn that and you have the key to all languages.”  Most things are just syntactical sugar, so program with your help docs handy and you can do pretty much what you want with any language.  You’ll just stumble occasionally, which is one more reason why you keep learning and testing your skills.