Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 31
  1. #16
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,032
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Quote Originally Posted by firepages View Post
    STREWTH Bruce, you trying to say me strine is not grouse enough for yous ?
    LOL!

    Hey, to be fair, I made fun of the difference between American and British English as well, in another post. <grin/>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  2. #17
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,885
    Thanks
    56
    Thanked 539 Times in 536 Posts
    I only ever half-learnt javascript and I'm half way through half learning java, so my observations are bound to be a little more superficial than those offered so far, but here goes:

    to me, javascript seems to be a lot looser. In java, you have to declare what type a variable is before creating it:

    Code:
    int myint = 1;
    whereas javascript you just jump straight in there:
    Code:
    var myint = 1;
    which is annoying, but not so much so. Where it gets annoying is if you want to change the type. In javascript, you could do
    Code:
    var myint = 1;
    myint="fish";
    myint=true;
    which is a dumb example and something you would probably never want to do, but you could if you wanted to. Doing it in java would be a complete runaround:
    Code:
    int myint =1;
    String theint = String.valueOf(myint);
    theint="fish";
    theint="true";
    boolean thebool = Boolean.valueOf(theint);
    Even then I'm not sure that it would compile, and anyway you end up with 3 variables instead of one.

    Java has other stuff that I half understand that annoys me, mostly to do with class types (static, volatile, public, etc) and the potential for memory leaks is huge and it would be a complete nightmare to write without an IDE. But for the most part I find that alot of javascript ideas are in there (or, if the story about the guy who invented javascript is correct, it would be more accurate to say that alot of java ideas are in javascript) - like Old Pedant says, the basic building blocks are all there - for and while loops, try and catch, global (kind of) variables and locals, strings, booleans, objects, JSON, arrays, getters and setters, event listeners. Really it would be a much shorter list to say what the differences are, although of course the magnitude of those differences is another story...

  3. #18
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,032
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    See, what you find annoying I find refreshing.

    To me, strong typing is a major feature, and one that avoids some of the idiocies that happen with JavaScript.

    One of my favorites:
    Code:
    var flag = "false";
    if ( flag ) 
    {
        alert( "flag is true" );
    }
    In JavaScript, you *will* see the "flag is true" from that code snippet.
    In Java, it wouldn't even compile.

    But each to his/her own poison.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  4. #19
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,885
    Thanks
    56
    Thanked 539 Times in 536 Posts
    that's a very simple example - I know you code in notepad, so I guess it may seem more likely to make that mistake but any decent code editor will make it clear as you type that flag is a string and not a real boolean. And really, that sort of hand-holding seems excessive if it's only designed to prevent newbie mistakes... you can't even log a variable in java without casting it to a string first... but like you say, each to their own. Maybe as I get more comfortable with java I'll start enjoying its quirks.

  5. #20
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,032
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    ???? I also code in Visual Studio, which has a quite strong JavaScript parser.

    But I don't see how any code editor for JavaScript could detect that error!!!

    Consider a slightly longer example:
    Code:
    var flag = true;
    if ( Math.random() > 0.5 ) { flag = "false"; }
    
    if ( flag ) 
    {
        alert( "flag is true" );
    }
    50% of the time, the code will work as expected. The other 50% it will say "flag is true" when in fact flag has been set (incorrectly! but that's my point) to "false".

    And in any case, the code is *NOT WRONG* in JavaScript!!!

    It is PERFECTLY LEGAL to even code
    Code:
    if ( "false" ) { alert("the string false is seen as non-false by JS"); }
    *ANY* non-null, non-blank string is seen as true when used in any conditional. (To me, it is weird that "" is seen as false; in any other language there is no semantic difference between a blank string "" and a non blank string "x". JS really does have some truly odd quirks.)
    Last edited by Old Pedant; 06-06-2013 at 08:29 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  6. #21
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,885
    Thanks
    56
    Thanked 539 Times in 536 Posts
    Quote Originally Posted by Old Pedant View Post
    But I don't see how any code editor for JavaScript could detect that error!!!
    what I mean is that (in notepad++, which is what I use) if I do this:
    Code:
    var flag = false;
    false is formatted blue, bold, italic which is what the program uses to signify a keyword

    whereas if I do

    Code:
    var flag="false";
    "false" is formatted light grey, which is what the program uses to denote a string

    so unless you're typing without actually looking at the screen, it's pretty hard to make that mistake

    On a possibly related note, I admit I've never got my head around the concept of truthiness, so I guess js has plenty of quirks that I just overlook or have come to accept

  7. #22
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by xelawho View Post
    On a possibly related note, I admit I've never got my head around the concept of truthiness, so I guess js has plenty of quirks that I just overlook or have come to accept
    or quirks that are overblown by old-fashioned coders who think strong types are actually desirable. I guess strong types are good for a compiler, but that's just another outdated cog in the user-application wheel AFAIK. Modern machines have enough speed to provide luxuries to the coder unavailable prior to 1989, when most languages were developed using strong-types for high performance.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  8. #23
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,032
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    RndMe and I will never agree on this, so I won't bother commenting on his viewpoint.

    Xelawho: Everything you say is true, but how does that help in finding the error if the if ( flag ) is 150 lines later and in another function and and and???

    The point is, a JavaScript editor can tell you about the SYNTAX of what you are doing, but it's ability to interpret the SEMANTICS is very limited. Simply because the semantics can change as the underlying type of a variable (or expression!) changes during runtime.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  9. #24
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by Old Pedant View Post
    The point is, a JavaScript editor can tell you about the SYNTAX of what you are doing, but it's ability to interpret the SEMANTICS is very limited. Simply because the semantics can change as the underlying type of a variable (or expression!) changes during runtime.

    consider the example of $("#main").attr("title"); a 100 page site means this can be 100 different things. How could an IDE tell which one, unless you are working "live", in a web console from as single page. compiled programs never have to worry about running on different pages, or pages that don't exist yet. same goes for CSS: "#main div { color: red; }" does not guarantee that a given page's main div's text will be red, since inline styles can over-ride that.

    That blows a compiler's mind and is the main reason i think such languages are outmoded. It's not because it's bad or anything, but because i think future dev is web dev. Web dev needs data, presentation and behavior coupled loose to play well with CMSs, SEO, and other endeavors completely unrelated to programming itself.

    Think about adding a "new screen" to an EXE; that doesn't make sense without recompilation, but we now live in a world of dynamic page generation. This is unarguably a good thing, but the crux is that loose/late languages are pretty much required to handle all the moving pieces of a modern application's UI and to allow development teams of specialists to maximize their skill-set efficiently.
    Last edited by rnd me; 06-06-2013 at 10:24 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  10. #25
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,032
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    I think you are underestimating what compiled programs *could* be capable of, based on what they do today. But that's neither here nor there: Compiled code has different purposes, and I'm not advocating it for use in browsers (or at least not with the current state of compilers).

    Again, we just have different viewpoints. I see advantages to both compiled and scripted; you apparently don't. It's okay. We do things differently, we see things differently.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  11. #26
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,593
    Thanks
    0
    Thanked 645 Times in 635 Posts
    It is the loose typing in JavaScript that is the cause of many of the errors that JavaScript beginners make - perhaps the most common being that they forget that they need to convert form field values from strings to numbers before trying to add to them since the JavaScript default is to convert numbers to strings where you have '3'+3 giving '33' where if they already know PHP they'd expect the strings to be converted automatically to numbers in that situation so that '3'+ gives 6. Both languages are loosely typed and give different answers to the exact same calculation so no wonder beginners get confused.

    The solution is to treat JavaScript and PHP and other loosely typed languages as if they were strongly typed - never use == and != comparisons and always cast values to the desired type before using them.

    +('3')+3 or '3'+(''+3)

    or if you prefer the longer ways of casting in JavaScript

    Number('3')+3 or '3'+String(3)
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  12. #27
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,032
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    No argument, at all, Felgall. But the original point of discussion was basically the problem with the semantics of conditional expressions in JS. To wit:
    Code:
    if ( "false" ) { alert('"false" is true'); }
    More to the point, the problem is that you can't rely on
    Code:
    if ( x ) { alert("x is true"); }
    making semantic sense, because unless x has a true boolean value, the truth-ness is not obvious. All of the following are treated as false:
    false
    0
    null
    ""
    (did I miss any?)
    And all other values are treated as true.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  13. #28
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,885
    Thanks
    56
    Thanked 539 Times in 536 Posts
    It seems you could add -0, NaN and undefined to the list, going by the docs.

    I'm still not buying the "it's good because it stops beginners from making mistakes" argument, by the way - if that were at all valid training wheels would be mandatory on bicycles and we would all be drinking beer out of sippy cups.

    Reducing work for the compiler sounds a bit more compelling, although I don't know enough about compilers to tell if that's really necessary these days.

  14. #29
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by Old Pedant View Post
    I see advantages to both compiled and scripted; you apparently don't.
    that's not fair or true. when i started programming i was intent on strictness and hardware compilation. i panned .net because it "only" compiled to an intermediate representation, not to a lower level like assembly. I don't know assembly code, and it doesn't interest me too much, except when analyzing performance. i took great pride in picking out the right types for the job and saving every nickel of ram and cpu.

    Over the years i had a change of heart for a variety of reasons; faster machines, better interpreters, JIT, and the device universality of JS that can't be beat by any single compiled language.

    I still think there are lots of places for compiled and strict code; security, making browsers, OSs, games, high-perf apps, 5-9 applications, apps built by large or diverse teams, and many more. But, i do think that most everyday applications can be "done" in JS, and that good apps don't always need to be compiled anymore. i'm not saying it's easier, more idiot-proof, or faster, just that with enough time and rigorous testing, JS CAN make a good end-user-centric application, and doing so will be getting easier from now on.


    Ironically, javascript now runs primarily on the assembly level in ion monkey, chakra, and V8, so we use strong types and compiled code without thinking about it, and that's the best of both worlds.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  15. #30
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,032
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    A nice summary, RndMe.

    And apologies for seeming to be unfair. Wasn't my intention. Was just my impression of what you were saying.

    I remember the first JIT compilers for Java. They were pretty horrible. But oh man have we come a long way since then. In roughly 17 years. The entire industry has done a paradigm shift and now JIT can often compete head to head with true compilers. So I am *NOT* arguing from a performance view, at all. My like of strongly-typed language for *CERTAIN* applications has little to do with performance.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


 
Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •