code-tastic!

12.14.2006

Rails and Ruby and other such nonsense

I've fallen head over heals in love with Ruby on Rails. It not only is a joy to use, but is making me such a better software developer.

I first started programming when I was oh 10 years old or something like that. BASIC. Good ol' BASIC. No not, Visual Basic (yuck!!), good ol' gwbasic (also a Microsoft product). It taught me the bare essentials of programming: variables, branching/conditionals, looping, yadda yadda -- you know, the serious basics. I think most would agree you can only go so far with BASIC.

I then moved onto C (many years later), where I started learning things like memory management, libraries and linking, compiling, basics of how a computer works (C isn't called "the portable assembly" for nothing)

I didn't stay with C long as I got wind of this "object oriented programming" and was curious. Interestingly enough someone on a newsgroup pointed me to a paper on Apple's website that explained what OO was and also included a brief intro to Objective C. The paper was very enlightening, but I decided to pursue C++ instead of Obj C because it was the language at the time (some would say I made a poor choice, I might agree).

Wow, C++ was a breakthrough for me. I bought (and cherished) Koenig's Accelerated C++. That book was impossibly hard for me at the time but I stuck with it. Read the chapters over and over, did the examples and played with them until I understood what was going on. That book made me a man as far as programming goes :) To this day I get it out and read it from time to time, along with his other book, Ruminations on C++. I seriously put Koenig up there with the Gang of Four, and Kernighan and Ritchie for software greats, the man knows his stuff, knows how to express it, and really understands software development at a very rich level.

It was with C++ that I learned about modularity, the idea that "classes represent concepts", what it means to be a data type (still my favorite thing about C++, you can integrate with the language just as strongly as the language features themselves), why parts of your program should know as little as possible about other parts, etc etc etc.

Java was next, and I was in love. Java will always hold a special place in my heart. I think I liked Java so much because it lets you focus on the problem at hand and the nasty upkeep and bookkeeping that lower level languages force you to do is gone. You're left with a beautifully clean surface on which to express your ideas. I also loved the quality of Java's development tools (still second to none to this day), Ant was very refreshing after dealing with make for so long. Eclipse redefined what an IDE should be to me, etc etc.

Funny thing is, Java didn't teach me very much. Probably what I took away from Java more than anything is the value of writing readable code. Expressive variable and method names may take longer to type, but it's so worth it. You never have to stop and remember what some terse method name means or why this function call has that odd side effect. Clear, english based names really got its start in Java (as far as I know) and I think that's a very valuable contribution to programming.

I moved on to .NET which I still code in to this day (for my profession, not for fun). .NET hasn't taught me much either, it's basically Java in Microsoft clothing. Don't get me wrong, .NET is very good for what it's shooting for: safe, rapid software development focused on the Windows platform for the corporate world. .NET is fantastic for that. Better than Java. Better than anything else out there. Period. But C# pretty much is Java, and it still keeps one in that now old-school OO mentality. Where functions are passive and data rules. Where types are strong and restrictive (oh I hate generics in .NET now that I code in Ruby!!!) Someone out there wrote a nice journal entry about how verbs are second class citizens in Java and C#, and I couldn't agree more.

And of course there are many other languages I've learned, played with, seriously used over the years. Perl, Python, Scheme (great stuff), various assemblies, Visual Basic (not so great stuff), javascript, etc etc...

Now I'll talk about my current darling language: Ruby! Yup, I'm one of those guys, and you should be too :) It all started when my girlfriend wanted me to write her a web application where she could record her daily diet and exercise. Sure! No problem! Looking in my programming toolbox I grabbed the best tool I had for the job: Tomcat and Java. JSP is great! Servlets are great! Right? Luckily, I had also decided this little app I was making for my girlfriend would be a good candidate for learning some AJAX stuff. So I bought a book on AJAX and started reading it. It mentioned, and highly praised, Ruby on Rails. Hmmm, sounds interesting.

I worked through a Rails tutorial on Apple's website and was very impressed. I decided to rewrite the foodlog webapp in Rails. At this point the Java based foodlog was up and working, but it was buggy, took a good while to write, and not extensible at all really. In contrast, the Ruby version took about 10 hours to write (keep in mind, my first ever exposure to both Rails and Ruby), and it was almost completely bug free, MUCH MUCH MUCH cleaner code, highly extensible, offered true user authentication and logging in (the Java version didn't even come close to that) and quite simply blew away the Java version. Just obliterated it.

Since then I have put out another webapp using Rails. It's a simple test app that I used to get a feel for what it'd take to write real webapps in Rails from start to finish. There are numerous bugs and issues in that site, and the hand written javascript is mostly to blame. I didn't realize Rails offered full featured dynamic javascript generation with their RJS system when I made saidsecrets. And I am currently working on a very extensive, powerful and full featured webapp (for reference, has more functionality than Myspace) that is coming along amazingly fast considering I'm the only one writing it. To put it bluntly, Rails rocks.

But the thing is, Rails and Ruby are making me a much better programmer. More so than any of the languages in my past. Rails emphasizes DRY, "don't repeat yourself", and "convention over configuration". Rails -- and Ruby -- more than anything I've ever worked with, are teaching me how to make components that are incredibly flexible. Components that isolate areas of change to one single location. How to see the commonality in the various aspects of a system and take massive advantage of that. Granted, Ruby's insanely dynamic nature (you have to see it to believe it) is a major reason why all this is possible, but the general concepts apply back to my C# I write from 9 to 5 very nicely. I'm learning the power of functional programming, and wondering how I ever got by without it. I'm starting to realize strong typing is not necessarily a good thing. I'm learning that when code can almost read like its english, it's very easy to maintain and understand (my Java lesson taken to another level, essentially). I'm learning how important it is to make your API as easy to use as possible, and how making the interface to your API easy means the API itself is stronger because of it. I'm learning the power of testing, not just unit but functional, integration and stress testing too. I feel like an extremely conservative, never-takes-chances person that's gone skydiving or whatever and suddenly is seeing the world in a whole new way. Ruby and Rails have done that to my software development world.

But probably most of all, Ruby has really hit it home for me that different perspectives (ie, different languages and more importantly different types of languages) are essential to becoming a good developer. A bold statement here, but someone who's only ever really coded in one language? Not a good software developer. So does your job, project, school, whatever call for coding in Java or C# or C++ or??? Do yourself a favor, write a game in Lisp, or a website in Ruby , or an emulator in assembly. Guaranteed you'll be a better developer and understand your language and technology of choice better.

10.30.2006

obfuscation woes

So I wanted to get some inspiration and ideas on how to implement some stuff in javascript. Nowadays, who stands out as being really good at Javascript? Yup, Google.

So I thought I'd take a look at their Javascript code. This is the mighty Google, right? This should be some elegant-ass stuff! :) Or, um, this

Google Javascript sample

Now that's fun stuff to dig through. Why Google? Why? Why must you obfuscate your code? Share with the rest of us!!

Oh well...

9.27.2006

3D user interface

This is an interesting demo on a possible 3D interface for an operating system, modeled even more on the real world desktop than current OSes.

http://www.youtube.com/watch?v=RK_WLVO-TgA

It definitely seems to have a lot of impracticality to it. For one, the documents are merely icons, their names would be useful. But later in the demo when they manipulate windows and open files instead of icons, it gets more interesting.

9.04.2006

IntelliJ IDEA, open source?

The very expensive but very nice Java IDE, IntelliJ, is now open source.

Sort of.

http://www.jetbrains.com/idea/opensource/opensource.html

Basically if you can jump through their hoops, they'll let you use IntelliJ for free for a year, for non-commercial development.

I've never actually used IntelliJ myself. I worked as an intern at a company where all the developers had IntelliJ. But me being the lowly intern was "stuck' with Eclipse :) From what I could gather it seemed extremely Eclipse-like. Or maybe it should be said Eclipse is very IntelliJ-like. I dunno.

I was always impressed with IntelliJ's GUI widgets. IntelliJ is written in Java, yet they use their own proprietary widgets which are really slick. I wish Sun would revamp Swing and use SWT, IntelliJ and other current Java GUI frameworks as templates to go by.

Anyway, maybe once I can get JASEL to meet their requirements I'll grab an OS license and give it a whirl.

People may fight over which technology/language is the best, but when it comes to IDEs, Java wins hands down :)

Thank you Mr. Finite State Machine!

It seems all the general purpose finite state machine libraries out there are large and very general, for obvious reasons. None that I found seemed like they could slip into my game engine smoothly and with a small footprint. So of course the answer became to roll my own.

So I whipped up a tiny, very primitive and currently very error prone (need to work on that) FSM system. Now in my little bomb blast demo, the bob-omb wannabes just go out and do their thing! The code is soooo much cleaner and more powerful. I love it when you write a little bit of code and it ends up doing a ton for you, always feels good.

So I started by defining an XML document that contains my FSM definition.


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jasel-fsm>
<state name="walk">
<transition action="force-explode" env="" to="exploding"/>
<transition action="clicked-on" env="" to="drag"/>
<transition action="timed-out" env="" to="exploding"/>
</state>
<state name="drag">
<transition action="force-explode" env="" to="exploding"/>
<transition action="let-go" env="free" to="walk"/>
<transition action="let-go" env="wrong-pen" to="exploding"/>
<transition action="let-go" env="correct-pen" to="defuse"/>
<transition action="timed-out" env="" to="exploding"/>
</state>
<state name="exploding">
<transition action="exploding-timed-out" env="" to="exploded"/>
</state>
<state name="defuse"/>
<state name="exploded"/>
</jasel-fsm>


Every state has 0 or more ways to transition to another state. The transition is defined by an action that took place and the environment it took place in. If a state has no transitions, then it's a final state and that's what the bomb will be defined as once it arrives there.

So for example, the little bomb dude is walking along. The user clicks on him with the mouse and begins to drag. That causes the "clicked-on" action to take place, sending him to the "drag" state. Now that he's in this state, he knows he can't hit anything, his animation knows what frames to draw to show he is being dragged, etc etc.

Then I defined a StateMachine class that captures this XML in a Java object. The most notable method in this class is next(String action). The fact that my actions are defined as strings is one area that makes this package error prone. I'm currently exploring better approaches.

next(String action) basically looks to the current state's transistions and see if any of them match the action and the current environment (set by a setEnvironment() call). If they do, a new state is arrived at, otherwise we stick with our current state.

dumb code!


This leads to really dumb code inside my Bomb actor class. Good "dumb"! For example, if the Bomb's timer has ran down and it's time to explode, he just does ...


if (countdown < 0) {
stateMachine.next("timed-out");
}


He could care less what state he is in, or if even that call to next will lead to a new state. He just knows his fuse has gone all the way down and his time to live is over. The state machine just "does the right thing", which either causes him to stop walking and explode, or if he is being dragged, "fall" off the mouse and explode.

The Bomb's animation class is much "dumber" now too. Which is great. He just grabs the current state from the state machine and draws what ever frames are associated with it.


To see this code (like I said, it's raw stuff!) it's probably easiest to just poke into JASEL's CVS repository. Here:

http://jasel.cvs.sourceforge.net/jasel/jasel2.theater/src/jasel2/theater/fsm/


I should play with fuzzy state machines and all those other fun AI devices I know little about.

8.28.2006

Fixing MySQL/PHP on OS X 10.4.4+

This is more a note to myself than anything, but others may find it useful.

OS X is very cool because it comes with Apache, MySQL and PHP installed out of the box. You just have to enable PHP by editing /ect/httpd/httpd.conf and uncommenting the two PHP module related lines.

But PHP can't see MySQL because Apple screwed up the path to its socket in /etc/php.ini. To confirm you have this problem, try to connect to MySQL from PHP, such as:

$link = mysql_connect('localhost', 'username', 'password')
or die('Could not connect: ' . mysql_error());

?>


if you get no output at all, you're good. An error message, obviously you're not good.

To fix, run this in a shell to fix the path to the MySQL socket for PHP

sudo mkdir /var/mysql
sudo ln -s /private/tmp/mysql.sock /var/mysql/mysql.sock

Then restart Apache, the easiest way to do that is via the personal web sharing control panel in system preferences.

8.27.2006

JASEL goes live!

This weekend marked my first release of the JASEL game engine. JASEL stands for "Java and Shmups Equal Love". It is a game engine/library with the intended focus to make the creation of shoot-em-ups easy. By shoot-em-up I mean games like R-Type, Gradius, and Raiden.

It uses LWJGL for OpenGL, OpenAL and controller input support. It's (obviously) written in Java, Java 1.5 (aka Java 5) and so far has proven very cross platform friendly.

I've been making a lot of demos, working on getting a full CMS based website going for it, and have even got some other developers out there playing with the code and giving feedback. Maybe my vision of a really nice Java game engine will be realized, and even better, my vision of an end user "Shoot-em-up Maker" where non-programmers can make shooters with ease.

JASEL's Sourceforge page

8.19.2006

Initial musings on a project

So I am considering taking on a fairly big project. Due to its size, I am researching possibilities of existing platforms and techs to make its creation easier.

For a general platform, I really only know of NetBeans and Eclipse. Now I know first hand that NetBeans can be stripped down and used as a generic application framework for just about any GUI based application. I understand Eclipse can too, but I know less about this aspect of Eclipse.

Netbeans -- last I checked -- was built with Swing. This hits me as both an advantage and disadvantage. I dislike Swing in general, it's slow and awkward for the end user to use. But development wise I know it very well and I always found Swing rather well designed. I'm pretty sure Eclipse would force me to SWT, something I've never used before and something that just by its nature strikes me as lower level than Swing. I dunno.

Both of course force me to Java, which is ok. I like Java quite a bit and my target OS of OS X integrates very well with Java. No problems here. But do similar frameworks exist for any other languages? What about a generic .NET application framework? I wonder what SharpDevelop was built on.

But there is another reality pushing me to Java. I have a large body of code from a previous project that I'd be using here. In fact, barring some minor adjustments, I'd even say the entire low level framework is done already. Provided I go with Java, so it makes sense to do. I also considered both Ruby and Python for various reasons. .Net, eh, I have a Mac. I dunno, I'm sure Mono is great, but for some reason I just don't want to bother with it.

How advantageous is it really to use a generic framework like this? Both Netbeans and Eclipse are very complicated products. How much effort must be expended on learning their way of doing things, and am I better off just using that energy to roll my own? I suppose it depends on how much of their functionality I really need.


Not much of a post, but this blog is just getting started. Details and my findings on tech related to this project will keep coming, in more detail as I progress.


 
}