CoffeeScript

During the holidays i had the chance to try out this "converter" beast and i must say that i really enjoyed it.

For those who don't know what coffeescript is here is a short summary of it: 

CoffeeScript is a little language that compiles into JavaScript. Underneath all those awkward braces and semicolons, JavaScript has always had a gorgeous object model at its heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.

The golden rule of CoffeeScript is: "It's just JavaScript". The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa). The compiled output is readable and pretty-printed, passes through JavaScript Lint without warnings, will work in every JavaScript implementation, and tends to run as fast or faster than the equivalent handwritten JavaScript.

 

Neat eh? Well yes it is - basically it converts your coffescript to javascript(and even passes the Lint test). Many people(the pessimists) are complaining that its hard to debug, but thats not true if you know javascript, because the compiled javascript is definitely readable, so don't be lazy learn javascript first before diving into coffee or wait till there are better debugging tools available(this should happen soon actually). 

 

My experience with coffee was smooth like butter:

* Code writing felt like python(because of the INDENT logic).

* Even though coffee ads Classes, but underneath it, its all javascript objects.

* Most of the logic operators were borrowed/are similar from/to ruby (i.e:  is - == , isnt - !== , true yes on - true, ...).

  * Features like array/object comprehension, destructive assignment, splats,...  - python has them all.

* Some other cool features like the "Existential Operator" - "?"  that checks if variable is defined and not null.

Note: For me coffee felt like javascript, python and ruby combined, and i know those languages quite extensively(well except ruby) thus i felt like being in cosy, warm place - home, but you may not!, as there some people who can't get over the "curly brace syndrome" i had that illness once myself.. 

 

When i started meddling with coffescript i began using classes right away, but i quickly ran into some "problems" or to say better, i had questions about how the classes work, as the documentation was not that clear.

Questions:

1. Does coffee support/expose private variables?

2. What about static variables?

 

Answer:

Well, i actually knew that coffee does support them, because after all coffee is just javascript, so what can javascript do, coffee can too, it just that i wasn't sure how, but i figured it out quite simple - by compiling coffee file :)

Heres is an example of how the variable scoping works in classes:

class Animal

#static variable // note here how the '@' is used

@name3 = 'Anna'

#private variable

name2 = 'john'

constructor: ->

#instance variable

@name = 'rex'

 

move: (meters): ->

console.log "#{@name} moved #{meters} meters"

and in Javascript this would look like:

 

var Animal;

 

Animal = (function(){

function Animal(){

//instance variable

this.name = 'rex'

}

//private variable

var name2 = 'john';

//static variable  

// if in coffeescript '@' is used inside the class, but outside a method it refers to the class rather then "this"

Animal.name3 = 'Anna'

Animal.prototype.move = function(meters){

console.log(this.name + " moved " + meters + " meters");

}

return Animal

);

 

So what i actually tried to show you here is that "@" does not always mean "this".

 

So.. Like i said i really enjoy drinking coffee, you should try it too, but be careful coffee is quite addictive, once you try it, it is hard to stop, because you can buy it anywhere - be it browsers, server side(nodejs, rhino), cross platfrom development(titanium appcelerator, phonegap)

 

 

 

So what i've been up to lately?

Well apart from working and being happy scout at blubolt were we hack the sh*t out of php, solr, memcahced, nginx, mysql and various scalable deployment technics, i've been diddling around with few "edge" technologies like nodejs, redis, mongodb.., on my own.(oh and hacking some stuff in C just so that i remember the bits and bytes - and who doesn't like being near the "metal")

 

So i guess i should give you my opinion on the tech stack that i just mentioned, alrighty:

 

* Php ->

There are many people who hate it, think its old, newbie language, but thats actually opposite of that in my opinion.

Its not old - its mature, newbie language - easy to start, but hard to master.

With all the 5* introduced features - namespaces, closures, traits(upcoming in 5.4), call type hinting, other..,

Sure as any language it has its own hiccups mainly needle, haystack problem.

On web development front, php is on par or maybe even ahead of C#/Java, i love its PhpDoc and the IDE support is just amazing.

Php - mature modular oop language, best suited for web development(it was made for that) from small to enterprise level.

* Nodejs ->

The hot new stuff that everyone is talking about. It recently reached 0.6v so its still quite young.

So whats nodejs? Basically its Javascript on steroids, no, no its actually javascript on server side powered by googles V8 engine(chrome).

Were it really shines is - it has evented I/O as all the core methods are async, not to mention that you can both code client and server side in js.

People have already started using it in web development building sinatra(mustache) or mvc(geddy) like frameworks, but in my opinion its not very suited for that(well unless you use some "sugar" or other libraries that help you deal with the nesting), atleast not yet. 

I think the best things to develop with node are various internet and file management services in example - irc bots, rest web service, site scrawlers, websockets.

P.S: As nodejs(V8) is still Ecma3 with some Ecma5 features i suggest looking at some "sugar" - coffeescript.

* Nginx -> 

Nginx is a free, open-source, high-performance HTTP server and reverse proxy. - Don't know what else to say, thats absolutely correct.

Its written in c, supports custom modules(geoip, tcp, ...), and deals with the 10k problem just fine.

Basic and most useful functionality = spawn workers and proxy request.

* Solr ->

Google search at your finger tips, well almost.

Solr open source search server - was originally developed by CNET and then passed to apache.

If you have small, medium, big, any size haystack, if you need to find a needle in that haystack and you need to do that often - solr is here to help you.

Index the haystack with various parameters, query/search for your needle - be happy - its that easy simple, written in Java, open source.

* Scala -> 

Scala, scala oh dear scala why are you running on JVM? Well actually it makes sense, you can use the wast java libraries and combine that with scala you get one powerful rapid development environment.

Scala is a programming language that is - type safe, can do both oop and functional programming(feels like crossover between java, javascript and python), currently runs on JVM(and thats the only thing i hate about scala, but some people love scala for that).

 

* Go ->

Google's new programming language Go. Roots lie in C, is compiled language, that means it will compile to an executable. 

Runs super fast(also fast compile times), has various concurrency mechanics(channels, goroutines, defers) that means its very scalable, its statically typed, feels like a dynamically typed(but its not), interpreted language.

I really liked it(maybe even more then Scala(no JVM, ye!)), but the problem for me was, that mostly i do web development, and in web dev you need rapidness and flexibility, so even though Go feels like dynamically typed language it isn't(and you can forget that sometimes), i would use Go for any low level app that requires very efficient computer resources use - basically for task that you would normally consider using C/C++ i would use Go.

Go has great potential, so i won't say that i wont use it in web dev ever, because i might, and some people are already doing it.

 

* redis/memcached -> 

Well as you all probably know memcached is the de facto tool for in memory data caching and one of the first things you use to make your app more responsive/scalable.

We could say that Redis is the younger more cooler little brother of memcached, but by that i just mean that it is younger project and by no means is less stable then memcached.

Redis expands on memcached caching idea and adds more features to the field.

 

The key features that i like:

* Pub/Sub messaging

* Some data types(list, hashes, sets, ...)

* Sorting

* Can easily implement job queue

* In disk - permanent store

So my verdict is - if you need any of my mentioned features use Redis, else use Memcached.

 

* mongodb -> 

MongoDB document based NoSql database - still generally young project(even if it hit v2 recently).

Actually haven't used that much, but from my point of view the key features are:

* Replication

* Sharding

* Read, Write speed (need to be aware of write locks), in optimized cases you don't even have to use caching(memcached, redis) 

Probably best case scenario for mongo is when building something similar to analytics system, were you need to do many writes and reads, without changing the data and then doing a map-reduce on that data for statistics. 

Also you should be aware(read upon) mongodbs shortcomings, like for example if you have small data sets and large data sets(that are several times bigger then your ram), mongo can get slow its due to fragmentation and minimum cache granularity.

Other example is that if you delete entries from mongo it actually marks them as freed/deleted, but it does not compact the data on the fly, thus your data ends looking like a house full of rat holes and the only thing you can do about it is to take down your db and run "repair" on it, but in most cases thats not an option.

From quick glance mongodb can look very shiny it has good documentation, many libraries, its documents are bson(binary json, one more win for javascript), replications, sharding.

But as with each database it requires maintenance and knowledge of its pitfalls.

* mysql/postgresql ->

Those two are quite similar(and distinct), both are ACID, and both are rmdbs.

More and more companies are currently switching from mysql(as it was acquired by oracle) to postgresql or other solutions(nosqls).

Thus i also would like to say, that when possible, Im using postgres instead mysql.

I won't go into the details of comparing them(as actually Im not that big of an expert), but in many ways they look and feel the same.

Few features of Postgres that i like and that are probably the most noticeable from mysql:

* Procedures and procedure storage(plsql, other scripting language support)

* Schemas - this is cool, as it allows to store your tables in more modular way.

* Has better user access control then mysql - basically you can have various roles with certain access permissions.

 

Note: before using any technology you should always check if there are developments tools available.

Out of Ram

Last week i was thinking that we humans have really bad memory. 

Our memory is like computer Ram, you know/remember what you are doing now, what you did yesterday, but just go back three days back in your mind and the memories start getting very vague.

Sure we flush our Ram to our hard drives(brain) but only snapshots, snapshots being - most important abstracted events.

As an example lets take my blog, i started it late 2010, i know that i shared some of my code here, but what exactly i couldn't recall, without opening the blog page and viewing it.

So yeah our memory sucks.. and its no mystery at all, as we all know that, but maybe haven't thought about this in depth. Just imagine if you could remember each books content that you read in your life, that would be simply awesome, would make us really smart, what advances could we archive then?, but the sad reality is that we are forgetful, so the only thing we can do is research this so that hopefully one day we make these wonder drugs, who will add more Ram to our brains so that we can recall every moment of our lives, thus expanding our lifespan mentally.

 

Conclusion of this article is that - I want more RAM!,

But because i am not getting any, will resume writing this blog.

Javascript game engines

Past half year or so i have been reading a lot about javascript game programming, as you know in software/tech world time goes fast, so there is no surprise when a new or updated software or some sort of hi-tech product pops out quite quick, that leads to my main theme of this post.

what happens when browsers get fast, when they evolve, and mobile devices with integrated smart OSes lies in peoples pockets? you guest right, products/applications are born,  and not some simple programs, but games. To create a game takes lot of effort, developers need to know math's good, especially trigonometry, to know sin, cosine, tangent other functions, and the designers need to create the game worlds art(pixels, map layout, models..), and don't forget the writers that make fun and engaging story for your game, not talking about management that brings it all together. Well my point is made lets look at what makes javascript web game programming easier.

On what you are about to read  is just my opinion after trying develop or studying the fallowing:

GMP Javascript Game Engine - is a fast, free, javascript game engine, dual licenced under GPLv2 and MIT licenses. It’s small, simple, and easy to learn and use.

  • A ready-to-go, self-booting game loop
  • Easy-to-use mouse and keyboard input managers
  • Robust classes for organizing your game code and making all your game widgets and sprites
  • Complete API documentation, and a user manual with tutorials
  • Plugins to help with game development (coming soon)
  • Templates for game components and even entire games (coming soon)

Personally i liked this one the most of all the javascript game engines i tried, though it doesn't support some functionalities that other engines do, but its very nicely programed, runs fast and has good documentation.

Akihabara - is composed of a number of libraries that use HTML5 canvas tag and some standard hooks.

  • Gamebox module is complete and compact enough for making games.
  • Gamecycle module provides a complete generic game cycle.
  • Toys module provides lots lots of common routines during the game developing.
  • Help module provides some Javascript-specific functions, such object copying, randomizing functions, string/array handlers and the  akihabara function, that automatically sets a comfortable preset of configuration.
  • Tool module provides simple developing tools.
  • Trigo module provides some math stuff for moving objects in a direction or following a round path.

This one is really powerful, if you want to create something complex, that supports sounds, has tools for easier mobile programming, then this game engine is for you, the big minuses are that this project is quite young still lacks documentation and the code is bit a mess, but all in all it comes second in my engine choice.

Thats it these are my top choices if i would develop a game in javascript i would chose one of them, here are some links to other mention worth engines:

gameQuery - gameQuery is a jQuery plug-in to help make javascript game development easier by adding some simple game-related classes. It's still in an early stage of development and may change a lot in future versions.

Render Engine - The Render Engine is a cross-browser, open source game engine written entirely in JavaScript. Designed from the ground up to be extremely flexible, it boasts an extensive API and uses the newest features of today's modern browsers.

And here are some javascript games:

Javascript quirks and scope

Though i like to think that i am quite adept at javascript understanding sometimes i forget the quirks it has.
Just today i was programming and wanted to call a simple function editPost, then when testing what i wrote i get an error saying "editPost is not function", damn i said to myself thinking that i mistyped somewhere, but as it turned out i used editPost as a global variable in another function, there i forgot to add the 'var' and it became global. I think its a common mistake and sometimes its hard to maintain such code especially when working on bigger projects witch has lots of javascript lines. Sure there are methods to solve scope problems like namespacing, using OOP programming style in general, but you must apply these methods early in development in order to escape the mess later. Here's a simple example of my problem that i had:

function getPost(){
    editPost = 'not used';  //no 'var' means the variable is global scope; this type of variable is called implied global
    ...
    return post;
}

function editPost(id){
    ...
    //browser will shout an error, that "editPost is not a function
    ...
}

There is a good book about these kind of quirks that Javascript has i recommend you reading it if you are interested, its called "JavaScript: The Good Parts" written by Douglas Crockford. It's a small book, but informative and explains javascripts buggy parts like:

'' == '0'          // false
0 == ''            // true
0 == '0'           // true

false == 'false'   // false
false == '0'       // true

false == undefined // false
false == null      // false
null == undefined  // true

' \t\r\n ' == 0    // true

and many other awful and beautiful features of javascript insight-fully.

PostgreSQL dynamic table partitioning

Recently i had to work with big database, were really big tables like 200gb were not partitioned, and query performance from those tables was getting really slow. So it was decided to partition them in order to speed up database performance. So how exactly do you do this, how do you partition the table? Well i won't get deep into specifics, but basically saying you create or presumably already have a master table from witch one you derive other tables, i'm talking about table inheritance here, then create a trigger on the master table witch redirects data inserts into the right derived table and thus partitioning is born. To be more clear here are some examples. Lets say that we already have a master table 'Users' that contains (id, user_id, login, password, desc) columns. So to derive-create table that inherits all master tables description we do this:

CREATE TABLE users_by_id.user_1 (
    PRIMARY KEY (id, user_id), CHECK ( user_id = '1' )
) INHERITS (public.users);
CREATE INDEX user_1_index ON users_by_id.user_1 (user_id);

So here we created a child table that inherits from its master table 'users', the CHECK is used by postgresql to determinate from witch table to SELECT, UPDATE... data and to unsure that data written to this table is limited to user who's id is 1 . Later we create an INDEX on 'user_id' to increase fields usage speed. Note that 'public' and 'users_by_id' are postgresql schema's, i use them to keep tables separate for better readability. Most likely now after carefully reading what i wrote and in your mind after applying it to an actual application you may have one or few questions. The questions i had were these: 1. What if i have many users and new users are created constantly is there a way to automate the process of creating child tables and indexes instead of doing it manually? 2. How do i direct database INSERT's into the right tables? The answer to these two main questions i had is quite simple. You create a TRIGGER function that on data INSERT dynamically checks if child table is created for user. If it is - you insert data into user table, if not - you create the child table for the user and after that you insert data into that table. For better understanding check code examples. Code examples here:

CREATE OR REPLACE FUNCTION users_data_insert_trigger()
  RETURNS "trigger" AS
$BODY$

DECLARE

data record;

BEGIN

EXECUTE  'SELECT tablename from pg_tables where tablename=''user_'||NEW.user_id||''' AND schemaname=''users_by_id''' INTO data;
IF data.tablename is null THEN

    EXECUTE  'CREATE TABLE users_by_id.coordinates_'||NEW.user_id||' (
    PRIMARY KEY (id, user_id), CHECK ( user_id = '''||NEW.user_id||''' )
    ) INHERITS (public.users);
    CREATE INDEX user_'||NEW.user_id||'_index ON users_by_id.user_'||NEW.user_id||' (user_id);';

    EXECUTE  'INSERT INTO users_by_id.user_'||NEW.user_id||' VALUES('||quote_literal(NEW.id)||','||quote_literal(NEW.user_id)||', '||quote_literal(NEW.login)||', '||quote_literal(NEW.password)||', '||quote_literal(NEW.desc)||' );';

ELSEIF data.tablename is not null THEN
    EXECUTE  'INSERT INTO users_by_id.user_'||NEW.user_id||' VALUES('||quote_literal(NEW.id)||','||quote_literal(NEW.user_id)||', '||quote_literal(NEW.login)||', '||quote_literal(NEW.password)||', '||quote_literal(NEW.desc)||' );';
ELSE

    RAISE NOTICE 'Insert trigger ERROR!';
    RAISE EXCEPTION 'Insert trigger ERROR!';
END IF;

RETURN NULL;
END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;

Well that wraps it up, i think the code is self explanatory and there is no need to add something.

Hello World

Blog Intro

Yet another blog, yet another blog in the wast internet sea, yet another blog knocking in your internet browsers doors...

Yes..,  like a woman giving birth to a child, this blog is born, like a child stepping his firsts steps, let me say "Hello World", as a child learning new things, finding interests.., this blog as well will focus, on my, that is Deividas Gustys interests, hobbies. Before you start judging me for these cheese "child, mother metaphors" let me say, that this blog mostly will be about  IT related  stuff, that is: programming, hardware, software, work, technologies, etc..., and it will be written from my perspective, experience , thought pattern, so if you don't agree with my opinion or have something to say please comment or write me email. For me this "blog" is exactly what it is, a "web log", its purpose is to show me what i learned, experienced and to remind me what i still new to do.., as for you imho "readers" , i hope that you find something interesting and worth time reading.

Uhhh, i almost forgot to introduce myself.

Hello, my name is Deivid'as Gustys, i was born in Lithuania one of the baltic countries, Deividas as you might guest in English is 'David' so please feel free to call me like that. Currently i am a programmer at company "UAB Ruptela", its not a very big company, but its expanding very fast, maybe the reason is that it provides vehicle tracking and monitoring services quite new and salable product, thus my work is to program maps, be it google, teleatlas, other. Our system is web based, so that makes me a Web programmer/developer. My skill-set includes php, pg/sql's, javascript, python, C#, other.  Well that is it , i kept it brief, if you like to hear more about me - ask.