This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Learning JavaScript and am thoroughly confused by the overall design Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Learning JavaScript and am thoroughly confused by the overall design" Watch "Learning JavaScript and am thoroughly confused by the overall design" New topic
Author

Learning JavaScript and am thoroughly confused by the overall design

Steve Stevenson
Greenhorn

Joined: Apr 12, 2012
Posts: 21
Not knowing any internet scripting languages I decided to learn JavaScript. Programmer job postings always request it and a headhunter recommended I learn it so I bought a book. I was expecting a Java-lite experience but it appears to be anything but! My problem is that I repeatedly learn something new, my mouth drops from the horror of its design, I blow off my reaction as inexperience, and then my open-mindedness is punished with learning how yet another core programming concept has been bastardized in JavaScript.

At this point I've learned something so horrific I am forced to post and ask: Does this language ever make any sense? So far it seems like the designers grabbed some C++/Java keywords and digitized some bubble gum and popsicle sticks and voila! JavaScript was born! I am so thoroughly confused by the overall design that I can't even tell if there even is a design.

The first shocker was the loosely-typed language design. Aside from making things appear less threatening to new programmers does this have any actual benefits? If you know enough about your variable to be able to name it then shouldn't you know what kind of data it will hold? Shouldn't you even know that before you create the name? So I blew off my reaction as inexperience only to learn about functions: any function can return whatever it wants whether it's a string, number, nothing, or even another function! This can all happen even within the same function! The function's parameter list does not specify data types so there's no protection against accidentally passing in arguments in the wrong order.

After that I learned that you don't really need to end a statement with a semicolon because the parser will know what you mean, and that if you misspell a variable name then you have nothing to fear because JavaScript will help you by creating a brand-new variable for you IN THE GLOBAL SCOPE because that's what you really wanted. Since JavaScript really has no such thing as data types it really doesn't need all the pomp and circumstance in creating a variable properly anyway!


The straw that broke the camel's back came yesterday and has halted my progress: Objects. Apparently objects are also generic "var" data types and your "class" (I use that term loosely since it doesn't appear to be a keyword) is "defined" completely on the fly: if your object needs a new member variable then voila! Just refer to it and it will magically appear out of thin air!

So instead of doing something like this crude pseudo code:




In JavaScript you can do this, right?:


I mean, is that actually right?
That's gonna work great for those times people misspell things and suddenly have girl.sibling, girl.silbling, girl.sidlnig all living in the object.


The deeper I get into JavaScript the worse it seems to get. I honestly don't know if it's possible to make decent, sensible, maintainable code with JavaScript or even if I want to continue learning this language and possibly put myself into a position where I must maintain some code. It certainly helps explain why websites can be so error prone. Am I missing something here? Is there an elegance to JavaScript that will eventually become apparent or is it really as sloppy and dangerous as it appears?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60737
    
  65

Let me just say that if you approach JavaScript from the point of view that it is somehow "Java lite*" you will be very confused. It is its own language and all those properties that seem to violate your sensibilities make it a powerful and flexible language.

I'd highly recommend starting over without the perception that JavaScript has anything at all to do with any other language built for a completely different purpose. Keep an open mind and you might find that, for the purpose for which it is intended, JavaScript is an elegant and powerful language.

I'd also highly recommend that you should learn how to couch a discussion in reasonable terms rather than just bad-mouthing things you clearly do not understand. Your post comes across as rather unprofessional.




* Changing the name of the language from LiveScript to JavaScript back in the mid 1990's was a huge mistake as it gives people the idea that it's somehow related to Java. Except for a syntax influenced by C syntax, they have very little else in common.



[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30050
    
149

Most of the things that horrify you are features of scripting languages in general. Search for weak typed and dynamic languages. Examples include Python and Ruby.

This isn't bad; just different. Scripting languages aren't rare or a one off. They are an important concept to understand. Also, learning different types of languages makes you a better programmer because you learn to think differently about problems.

And your reaction is inexperience. Don't let that bother you. Think of it is learning a new programming concept rather than being horrified. This also shows you want to be a software developer not just a "Java programmer". The former has flexibility.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Steve Stevenson
Greenhorn

Joined: Apr 12, 2012
Posts: 21
Bear Bibeault wrote:Let me just say that if you approach JavaScript from the point of view that it is somehow "Java lite*" you will be very confused. It is its own language and all those properties that seem to violate your sensibilities make it a powerful and flexible language.


That is how I started, but the huge differences became apparent so quickly that I researched the issue just to see how the language even got the name "JavaScript". I even wondered if there had been some sort of a legal dispute over this name, so it was interesting to read how it was designed to complement the internet experience that Java was providing web browsers.


I'd highly recommend starting over without the perception that JavaScript has anything at all to do with any other language built for a completely different purpose. Keep an open mind and you might find that, for the purpose for which it is intended, JavaScript is an elegant and powerful language.


At this point my problems with the language don't have anything to do with the fact it isn't Java-Lite. It has to do with the fact that the language seems to openly embrace things that other languages specifically forbid and forbid partially because of the bugs that it can introduce. For example, I'd be curious to know the benefit of creating a new variable in the global namespace when an undeclared variable is used, and why that benefit cannot be gained with the use of safer syntax. I'd also like to know how that benefit (if there is one) outweighs the problems that can be caused with a simple typo. A lot of the odd stuff in JavaScript can be done in other languages, but only with proper safeguards in place.



I'd also highly recommend that you should learn how to couch a discussion in reasonable terms rather than just bad-mouthing things you clearly do not understand. Your post comes across as rather unprofessional.


That's a highly simplistic view on the post that I wrote. "Badmouthing" and "critiquing" and two related yet different concepts, and had I wanted to badmouth the language I would not have articulated numerous examples of the problems I have encountered with it nor would I have openly admitted (I forget how many times) that I am new and that I may be missing things. Also keep in mind that it is also rather unprofessional to remain silent and not speak out against something you see as being problematic. Yes, I may have been a little passionate, but so far the only response I've received addressing my issues with the language has been an answer to the most superficial thing I brought up: the name.



* Changing the name of the language from LiveScript to JavaScript back in the mid 1990's was a huge mistake as it gives people the idea that it's somehow related to Java. Except for a syntax influenced by C syntax, they have very little else in common.



I read somewhere that the name "Mocha" was considered, but at any rate at this point I've gone beyond having issues with the name and wonder how the loss of long-established programming practices and safeguards can be a good thing. I loved that picture by the way!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Steve, I have the same basic feelings as you. I am very big about doing things that makes it easy to understand the code and maintain it, and some of the things that can happen in JavaScript because of its "design" allows for some bad things.

That's why Eric told me to read "JavaScript the Good Parts" (Which I finished last night for the first read through. I need more than one read through). Because there is some great power in how Javascript is designed to help you write web applications, in which Javascript was built for. So as Bear says it is designed that way because it is best for what it is used for in web applications. I can imagine if Javascript was built like a statically typed language and like Java then things like closures, function callbacks, asynchronous code wouldn't be as simple as it is in JavaScript.

And most JavaScript programmers know to avoid creating global variables. You know it is also easy to create global variables in Java and it is just as bad an idea.

Basically, I have problems because I have to think differently and sometimes hack to get something to work like I want in JavaScript, where I don't have those issues in Java. And that JavaScript sometimes makes me write code that I cringe at because I would never ever want to do that same design in Java. But if I was using Pascal, which it type safe, I also would use a completely different design.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Steve Stevenson
Greenhorn

Joined: Apr 12, 2012
Posts: 21
Jeanne Boyarsky wrote:Most of the things that horrify you are features of scripting languages in general. Search for weak typed and dynamic languages. Examples include Python and Ruby.

This isn't bad; just different. Scripting languages aren't rare or a one off. They are an important concept to understand. Also, learning different types of languages makes you a better programmer because you learn to think differently about problems.

And your reaction is inexperience. Don't let that bother you. Think of it is learning a new programming concept rather than being horrified. This also shows you want to be a software developer not just a "Java programmer". The former has flexibility.



I think one of my big problems is how through my years with C++ and now Java I have become overly aggressive in protecting my data to ensure accidents can't happen. Not only do those languages require you to explicitly defining your variables, you can even lock them from being edited (final, const, etc), fine tune the range of data (i.e. unsigned), and use the data types to prevent inappropriate data from being passed from function to function. I love how those features can virtually eliminate an entire category of potential bugs, and it does scare me that those safeguards are gone in exchange for yet-unknown benefits. I know scripting languages are basically compiled on-the-fly by web browsers so there have to be differences, but I just don't know if it's wise for so many long-established safeguards to be abandoned because the code is written in a scripting language.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I suspect you may be more in agreement with what Mark Twain said about this, namely "Java is to JavaScript like the lightning is to the lightning bug."

However many of your questions seem to be of the form "Why did the language designers do something in the way they did?" And questions like that can only be answered by finding the language designers and asking them. You won't find any document which rationalizes those decisions. Sure, you'll find people who defend them after the fact by making up reasons why the decisions were good ones, but that's just confabulation. In other words that type of question is rather futile. Or to put it another way, namely my former boss's frequently-used explanation, "It is what it is."

To answer one of your questions, though, yes, it is possible to write good JavaScript. There are plenty of examples out there. But of course Sturgeon's Law applies here as it does anywhere else, and you will find that 90% of JavaScript code is indeed crap. Just as 90% of Java code is, and 90% of HTML, and so on.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60737
    
  65

Jeanne addressed your last point. Is JavaScript your first scripting language? Your first loosely typed language? Your first language outside of Java? The things that seem to be horrifying you are not unique to JavaScript, but are properties of most loosely-typed scripting languages.

The goals of such languages are brevity and flexibility.

It'd take far too much time to justify each and every reason that scripting languages are as loosely constructed as they are, or to give examples of the benefits that they provide. That's what books are for. I don't know what books you are using, but I'd recommend Head First JavaScript, JavaScript: The Good Parts, and my own book Secrets of the JavaScript Ninja (written with John Resig and currently in early access prior to publishing) in that order.

To a use pop culture reference: "Let go, Luke." You need to let go of your preconceived notions to use the Force.
Steve Stevenson
Greenhorn

Joined: Apr 12, 2012
Posts: 21
Mark Spritzler wrote:Steve, I have the same basic feelings as you. I am very big about doing things that makes it easy to understand the code and maintain it, and some of the things that can happen in JavaScript because of its "design" allows for some bad things.

That's why Eric told me to read "JavaScript the Good Parts" (Which I finished last night for the first read through. I need more than one read through). Because there is some great power in how Javascript is designed to help you write web applications, in which Javascript was built for. So as Bear says it is designed that way because it is best for what it is used for in web applications. I can imagine if Javascript was built like a statically typed language and like Java then things like closures, function callbacks, asynchronous code wouldn't be as simple as it is in JavaScript.

And most JavaScript programmers know to avoid creating global variables. You know it is also easy to create global variables in Java and it is just as bad an idea.

Basically, I have problems because I have to think differently and sometimes hack to get something to work like I want in JavaScript, where I don't have those issues in Java. And that JavaScript sometimes makes me write code that I cringe at because I would never ever want to do that same design in Java. But if I was using Pascal, which it type safe, I also would use a completely different design.

Mark


Thanks! Now I know I'm not alone with being bothered by some of these things!
Thanks for the suggestion on that book too because I am going to order it. It's always a gamble when you buy these kinds of books online because the reviews on Amazon are often limited and could have been written by the author's mother! Right now I'm reading Wrox's "JavaScript 24 Hour Trainer" and have found it to be laid out well, easily understandable, and the video lessons are helpful too, but it's always a good idea to hit these topics from another angle to get some information from another point of view.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60737
    
  65

Paul Clapham wrote:and you will find that 90% of JavaScript code is indeed crap.

Methinks the man is overly optimistic.

JavaScript is one of the most widely used languages on the planet, yet one that is misunderstood by the vast majority of people using it. Its abuse ratio is likely higher than most other languages. I feel bad for it at times.

Another factor that tends to confuse people coming from languages such as Java is that JavaScript, despite having objects, is not an OO language. It's a functional language with objects. And Java dudes seem to have a heck of a time grokking functional concepts for some reason. I know; it took me quite some time to "get it" not having come from a functional language background.
Steve Stevenson
Greenhorn

Joined: Apr 12, 2012
Posts: 21
Bear Bibeault wrote:Jeanne addressed your last point. Is JavaScript your first scripting language? Your first loosely typed language? Your first language outside of Java? The things that seem to be horrifying you are not unique to JavaScript, but are properties of most loosely-typed scripting languages.


I have to be honest here and say "I don't know". I really don't!
I used an old Basic language back in the 80s, learned Pascal (which was strongly typed from what I remember), and then went into the C++ thing where I was for a long time. I seem to remember doing something with a loosely typed variable and believe that may have been my little detour with Visual Basic or the time I had to write a couple simple ASP pages. At any rate, if I had used a loosely-typed language it has been virtually wiped from my memory and effectively leaves me in the same position as someone who has never used one.


Bear Bibeault wrote:
The goals of such languages are brevity and flexibility.

It'd take far too much time to justify each and every reason that scripting languages are as loosely constructed as they are, or to give examples of the benefits that they provide. That's what books are for. I don't know what books you are using, but I'd recommend Head First JavaScript, JavaScript: The Good Parts, and my own book Secrets of the JavaScript Ninja (written with John Resig and currently in early access prior to publishing) in that order.


You actually responded to the thread while I was mentioning this. I'm using "JavaScript 24-Hour Trainer" and think it's great, and then after that I'm going to read "JavaScript: The Good Parts" (So don't spoil the ending for me).



To a use pop culture reference: "Let go, Luke." You need to let go of your preconceived notions to use the Force.


Yea! I think next time I open the book I will need to take a nice long deep breath!
Steve Stevenson
Greenhorn

Joined: Apr 12, 2012
Posts: 21
Bear Bibeault wrote:
Paul Clapham wrote:and you will find that 90% of JavaScript code is indeed crap.

Methinks the man is overly optimistic.

JavaScript is one of the most widely used languages on the planet, yet one that is misunderstood by the vast majority of people using it. Its abuse ratio is likely higher than most other languages. I feel bad for it at times.


That's the main reason the language scares me. With a lot of that stuff I mentioned it would only be dangerous if people abuse it, and I'm afraid of being thrown into a project with abused code. I've been in that situation with strongly-protected and regulated languages like C++ and it's awful getting stuck with some spaghetti code. If JavaScript is abuse-prone it... just kinda scares me.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

When you do get put in the position of debugging some horrid JavaScript code, just remember: a JavaScript debugger is worth its weight in dilithium.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60737
    
  65

Steve Stevenson wrote:and I'm afraid of being thrown into a project with abused code.

The sad fact is that if you are thrown into a project with a lot of legacy JavaScript code, it's highly likely to be horrid. Sorry about the reality check.

The good news is that things are catching up. With libraries such as jQuery and the new importance placed on client-side code, not to mention the infusion of JavaScript onto the server using platforms like Node.js and V8, JavaScript is being treated with new respect.

The situation will get better.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Do not try to make JavaScript act like another language. People have been doing that and they make it worse than it actually is.

JavaScript is not that bad and if you use modern day browsers it is even easier to debug. Also modern day browsers support strict mode which will catch issues with misspelling variable names, force semicolons, and other things.

Eric
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

OH, and using something like JSLint to check your code for bad stuff, you can say JSLint is like a compiler to flag bad code to catch a lot of the runtime errors that could happen.

Mark
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
JSHINT is nicer than JSLINT. IMHO.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60737
    
  65

I do like the configurability of jshint.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Eric Pascarello wrote:JSHINT is nicer than JSLINT. IMHO.


Are they different? Meaning do they show different types of errors, or is one just an easier version of the same thing?

Also, in my IDE IntelliJ, it has both and runs both on the code.

Mark
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
jshint is a fork of jslint.
Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
I've been learning Javascript these past few weeks. I can definitely see where you are coming from. But so far I like it.

There are a couple of things that confuse the heck out of me about Javascript. I will be posting a topic about one of those things shortly. But I do enjoy the freedom it gives me. It's like Javascript trusts me , while Java really really doesn't. If I want to stick two completely different types into the same array it lets me. No need to spend the time creating an interface. And yes for big complex programs with a team of devlopers I can see why this could cause problems. But If I want to knock out a simple game within a couple of hours, Java can sometimes seem like's worrying to much. It's feels like Java is nagging at me like "Oh Neil! What if you call a method and the object doesn't have it! " While Javascript is like "Whatever man, it's your code. Do what ya want. I might not even bother to tell you if it messes up."

Oh and I love how easy it is to get my programs out there. I'm just writing silly little things as practice, but it's so easy to just stick what I've wrote on a website.

I'm glad I learned Java first though.

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"If I want to stick two completely different types into the same array it lets me."
You can always do that in Java too. You don't have to use generics or type safe collections.

"No need to spend the time creating an interface."

Actually, interfaces are so powerful and gives us more abilities that you wouldn't get without them, so this is actually a plus for Java.

Mark
Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
Mark Spritzler wrote:"If I want to stick two completely different types into the same array it lets me."

Actually, interfaces are so powerful and gives us more abilities that you wouldn't get without them, so this is actually a plus for Java.

Mark


Really, that's interesting. Could you give an example?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Neil Cartmell wrote:
Mark Spritzler wrote:"If I want to stick two completely different types into the same array it lets me."

Actually, interfaces are so powerful and gives us more abilities that you wouldn't get without them, so this is actually a plus for Java.

Mark


Really, that's interesting. Could you give an example?


Well, I could give a whole dissertation.

Interfaces allow us to swap out implementations without having to change code. It also allows us to do many of the Design Patterns that are out there.

A client layer should only be talking to a lower layer through an interface. This hides complexity, allows for a small little understandable api to be available. It allows is to easier write unit tests where we can mock or stub out the lower interface layers by providing a fake implementation. We can now easily do dependency injection, which again allows us to change implementation without changing code and to be able to add features to that code without you having to write that code. Things like Proxy based AOP.

Those are just a few examples.

If you want to discuss it more. Post in a different thread in a forum about Java. So we don't hijack this thread.

Mark
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60737
    
  65

For anyone interested, in chapter 4 of Secrets of the JavaScript Ninja (available in early access) I do a comparison in the beginning of chapter 4 on sorting arrays using imperative Java with interfaces, and using functional JavaScript.

Both are different ways of solving the same problem, neither of which I find inherently superior to the other; just different for dissimilar language types. The functional approach is expectedly more compact (though no less readable, imho).
 
 
subject: Learning JavaScript and am thoroughly confused by the overall design
 
Similar Threads
Design Patterns Explained: what it's about
Problem in javascript variable declaration.
java intranet collaborative draw board
A EJB3 vs EJB2 question
Collections Question