Another language feature I'd like

@kaffiene: Ah! Again, I’ve never touched js. I’ve seen a lot of compliants against js, where it was obvious that the person wasn’t really groking prototype based programming. So my thinking was to look at SmallTalk, where there’s no chance of confusing it with any other language and you can grab all the basic concepts. Then it’s a small step to prototype based.

That simply isn’t true. It’s the browsers DOM implementations that are problematic. JavaScript as a pure language is robust and versatile. With Firebug or the JavaScript-Debugger from Netbeans you get reasonable debugging capabilities as well.

The truth is that it’s getting pretty exciting in the world of web-browser applications. I’m not really an expert, but from stuff I hear, we are not that far away from seeing the browser as a great platform for applications. I mean, why invest in Adobe AIR or JavaFX, when we will be able to do the same things in pure HTML/CSS/Javascript, very very soon, with much better support and ease of deployment?

Canvas graphics, 3d accelerated transformations with CSS, video, sound, custom fonts, client storage, worker threads, it’s all here already. Try some of the latest browser builds for some even more crazy stuff, all implemented within the browser.

Some stuff to check-out:

Chrome Experiments
Native code inside the browser
Code editor implemented as a Canvas element
Youtube in HTML5
Mozilla’s Upgrade the Web in 35 days
SVG effects in HTML
Experimental Canvas 3D
3D CSS transforms in Webkit

Javascript is getting great too if you ask me. Every browser has a great debugger these days and implementations are solid. Most modern IDEs have decent support for Javascript too, with clever code analysis for auto-completion, refactoring, etc. Since this is a game development forum and performance is always a concern, you should know there is furious competition between browsers on javascript execution performance. Javascript engines like Google’s V8 and Mozilla’s Tracemonkey are doing crazy stuff under the browser hood, many of the great tricks and optimizations we enjoy in the Java VM have already been implemented. We’re not far away from the “good enough” performance threshold.

All current prototype languages are slow. And will continue to be until they start to support a ‘make immutable’ to an object’s metatable. All benchmarks are of questionable value for generalization, but shootout games has both Lua & JavaScript (V8 & TraceMonkey) upward to x100 time (x30 for LuaJIT) slower than Java. Improvements are being made but the nature of current designs limit how far these can go. Of course performance isn’t always an issue and both V8 & TraceMonkey are designed to be embedded scripting languages, not ‘full’ languages (small runtime is very important).

(NOTE: What I’m saying isn’t 100% true, the runtime could fake around the issue and determine when invalidated, but adding a make immutable concept would be much less work IMHO and probably a reasonable thing for users).

Javascript is getting better, but its not going to replace flash anytime soon and sure wont replace JavaFX.
The big stopping factor is IE.
I had to work with Javascript lately, personally I really don’t like working with it.
So far any advance games I’ve played in Javascript are quite low performance and generally crap and I’m using chrome 3. Its going to take a while before all browsers match the speed of chrome as it is.

Yeah, there is no point thinking about making games in Javascript. Just like I wouldn’t consider making a game using JavaFX either. It’s possible in both, but the point is rich user interfaces and non-trivial applications inside a web browser.

Also, don’t forget that just like Java the language is nothing without Java’s libraries, Javascript in the browser doesn’t mean anything without HTML/CSS/whatever APIs the browsers expose.

One thing really interesting I’ve noticed about GWT is that there are two modes in which you are able to use it, first you convert your code from java to javascript, which works on all browsers.

The second mode is hosted mode, this is whats interesting, here pure java code is used (yes, hotspot optimised class files), inside the html, it does exactly the same thing that the javascript would do.

So someone like Google would just need to hook the same tech into a proper browser (like Chrome) and you’d be able to use pure Java inside the browser (not as an applet but a javascript + html replacement) to render the page (and just have a javascript fallback for browsers that don’t support it). It’d make it possible to write complete web apps in java and with all its JIT benefits, it would also clean up the mess that mixing html/javascript/etc is atm for creating web apps.

Maybe Google already has this planned for the future and building up javascript for this switch, it would explain why this mode is in GWT.

It’d be especially nice if Google managed to shoehorn Java into Chrome. It’s only another 2.5mb as it is right now…

Cas :slight_smile:

Would be nice if browsers had basic Java plugin built in.

[quote=“kapta,post:107,topic:33789”]
This would be godly indeed. It should also be relatively easy to compile a VM for Google’s Native Client. However…

[quote=“kapta,post:107,topic:33789”]
I will repeat that it isn’t the mess people think it is. Just because 90% of the internet has been doing it wrong, it doesn’t mean there isn’t a clean, powerful and even beautiful way to write web pages. And please don’t say Swing isn’t a mess. JavaFX would be nice with native support from the browser, but it remains to be seen if it will have a chance against competing technologies, starting with the HTML5/CSS3/Javascript package.

Eh…I’m +1 on all of the pro-Javascript comments in this thread, I actually really like the language, and found it incredibly easy to learn and use. Buuuuuut…

That’s the language itself. DOM interaction when you want to try to be compatible across all browsers? Yeesh. FF + IE is bad enough, but if you end up in a situation where you also want to support Opera, Safari, or (god forbid!) Konqueror, it’s enough to make you want to leave the field forever.

It comes down to this: for pure Javascript logic code, everything is nice and easy, because you write one function, and you only need to test that one function. You can use whatever browser/IDE/runtime environment you like, which usually means whichever one has the debugging facilities you’re most comfortable with built in. Once you start interacting with the browser, best case scenario you now have to run three tests instead of one, probably a lot more if you want to support ALL the versions of each of the popular browsers; add up the common versions of just IE, FF, and Safari, and we could be talking upwards of 10 different test environments, which is just a freakish multiplier to add on to your testing phase. It quickly consumes almost all of your time. And you really need to do it, too, because things actually do go wrong between them all. In which case, prepare yourself, you’re going to end up spending three hours figuring out how to code what should have been a five line function if you only had to get it right in one browser.

There may be ways to do it right, and even somewhat elegantly (though I’d dispute that - a lot of quirks effectively require browser checks in your code, which is anything but elegant) but you can’t avoid the testing overhead any way you cut it, and it’s significant. Overwhelming, even.

My advice? Avoid this crap like the plague, and find a good library that handles the cross browser garbage for you. You’ll still have to test across all the browsers you want to support, but you can do it less often because most of the issues will be handled in the library if it’s any good. Just pray that you don’t run into a situation where your library of choice doesn’t handle things right…

But yeah, Javascript itself is good stuff, I’m not particularly unhappy to see it grow. The speed will get better as time goes by, there’s a lot of money that’s intent on seeing that happen. Re: the prototype based stuff, never fear, Javascript 2.0 should have class based inheritance, as well. IIRC, it’s going to be almost exactly the same language as AS3, based on the exact same spec (AS3 implements most of the spec, but in theory JS2 should get it all).

Going back to the OP, Pnuts does this pretty nicely. It would look like this…

available = PowerupFeature.getPowerups()[available]

Pretty damned concise, no? The following are all equivalent and should show why it works in Pnuts:


powerups = PowerupFeature.getPowerups()
available = powerups[available]
available = powerups[function (item) { item.available }]
available = powerups[function (item) item.available]
available = powerups[function (item) return item.available]
available = powerups[function (item) return item.isAvailable()]

This doesn’t handle the " || DEBUG" part, but that can be done in a couple ways:


powerups = PowerupFeature.getPowerups()
available = powerups[function (item) item.available || DEBUG]
available = DEBUG ? new ArrayList(powerups) : powerups[available]

You can try Pnuts out with this JWS:
http://www.pnuts.org/articles/example/run.php

Here is some code you can copy and paste into the webstart to try:


powerups = ["stuff", "more", "something", "cowsSayMOO"]
available = powerups[function (p) p.contains("MOO")]
for (powerup : available)
   System.out.println(powerup)

x = [1,2,3,4,10,11,12]
for (i : x[function (v) v % 2 == 0])
   System.out.println(i)

Pnuts also has an aggregateMode function in the pnuts.lib module that can be useful for dealing with lists. See the example on this page:
http://pnuts.org/articles/pnutsHighlights.html

Kinda grim sytnax compared to SQL isn’t it.

Cas :slight_smile:

I don’t remember faking first-class functions being mentioned (and didn’t see in a quick scan, using generics to be easier on the eyes).

Not very useful, but quick example:


public abstract class BoolFunction<T>
{
  public abstract boolean eval(T t);
  
  public Collection<T> apply(Collection<T> dst, Collection<T> src)
  {
    for(T t : src) {
      if (eval(t))
        dst.add(t);
    }
    
    return dst;
  }
}

Define the function:


  public static BoolFunction<GameItem> itemAvailableFilter = new BoolFunction<GameItem>()
  {
    @Override public boolean eval(GameItem thing)
    {
      return DEBUG | thing.isAvailable();
    }
  };


    ArrayList<GameItem> available = new ArrayList<GameItem>(powerups.size());
    
    itemAvailableFilter.apply(available, powerups);

Not of much interest unless a function is reused. Changing to flag testing would be a better example.

[edited typo]

Wow! People are actually defending javascript… I feel like I’ve stepped into an alternate universe. I guess shit just doesn’t smell bad here :).

I seriously didn’t see that coming! I expected everyone would chime in with annecdotes like kaffiene. “crap beyond belief” was spot on. I just didn’t realize it was a debateable point. Maybe next I’ll learn the Earth is flat and there is no such thing as gravity.

It’s not so much that javascript can’t do things… it just that it is a really bad choice for doing most things, given the alternatives. The tool support, available libraries, etc. are all part of what makes the alternatives better. Coding in javascript is undeniably more error-prone and unproductive.

Why not put the effort into the more respectable platforms that are already available for web-based applications. The clear choices are Flash and Java… with Silverlight bullying it’s way in via the brute force of a OS monopoly (It may be good, but would it gain the same market share without Microsoft jamming it down our throats?)

I would much rather see the new changes in the Java 7 VM that will really improve support for other languages on the VM (invoke dynamic). Then Sun/Oracle will have a very good competitor to the CLR… something that I suspect will have a significant advantage over the CLR given that the JVM is already so wide spread.

If we are talking about new languages or language features… let’s step forward, not backwards to javascript and lisp :slight_smile:

Javascript is excellent for prototyping (pun intended), enabling users to script an application, and for ducttyping a GUI, Java is just too verbose. But Javascript sucks for nearly everything else, simply due to the lack of tools - just like Java would suck if it had no tools and we would still launch notepad.exe, vi or nano.

Back to the derail briefly. Princec, are you applying to the Google London office? I applied there a couple of months ago and got a phone call saying that they couldn’t interview because of a temporary hiring freeze. They said they’d hang on to the CV and contact my once they were hiring again, but I reckon there’s a only a slim chance of that happening.

So… just wondering whether they’re hiring there again. Google’s a nice company, and I wouldn’t want them to miss out on the opportunity to employ me :wink:

JavaScript like many other technologies just has different implementations. I think Netscape and its initial investors supported the concept of others making their own versions back in the day to spread what it could run on. You may want to consider the highly reputed jQuery.

JavaScript has been around for a while, and it has been found to run on web browsers that most people haven’t heard of or even seen over in the generic area of Asia.

I originally applied there, yes, in response to an actual job ad so they can’t have really been having a hiring freeze! Unlike you though I got the usual “no response whatsoever” reply which is what has happened for most of the other 99 jobs I’ve applied for over the last 3 months.

Cas :slight_smile:

Sorry if my replies sounded like I was evangelizing Javascript, it wasn’t my intention. I was mainly talking about pure browser-based web-apps versus what is possible (and how it is possible) with AIR/Java(FX)/Silverlight. Javascript is only a tiny part of the equation and a part that, at least imho, does the job good enough. I think I posted enough interesting links, feel free to check them out and come to your own conclusions, it all depends on what you’re actually trying to achieve obviously.

For what I do or will be doing in the next years, it certainly isn’t a “bad choice given the alternatives”. Especially if you consider the rate at which new features are being added into browsers and the consistency with which AIR/Java/Silverlight fail to offer an application development model that would make pure browser-based apps obsolete. At the very least, seeing Google working on a browser-based OS and hundreds of other companies investing so much in browser technology, I’m only going to assume that we’ll be seeing that stuff for a long time. Which is nice, alternatives always benefit the developers and I personally have no problem mixing several different technologies for different problems to make my life easier.