Pair program with me! profile for carousel at Stack Overflow, Q&A for professional and enthusiast programmers

4/10/2013

Git aliases for bash and zsh

Tired of typing long shell commands when using git ?

One of my favourite long example is git log --oneline --graph --decorate - when you want to  see what is happening in you git project ( branches, merging... ) It is very, very long command, that is hard to type, but there is a better solution.

Depends of type of shell you are using - you can use aliases for often used git commands.  Alias is a shortcut for longer, and more complicated command. Example of built in alias is ls which executes "ls --color_auto".

To see a list of your aliases just type alias in terminal.
To temporary create alias, type for example alias server="Python -m SimpleHTTPServer"

If you want to permanently store alias, open your .bash_profile or .bashrc file and paste same line inside.

So to change git long --oneline --graph --decorate type alias gl=" git long --oneline --graph --decorate" and you are on.

If you are a bash user here is a link with bash aliases, that can be copied and pasted in your .bashrc file.

Since I am using ZSH ( Z shell), here is a link with zsh aliases.
You'll have to place them in your zshell config file.

Happy Coding.



4/07/2013

How coffeescript makes life easier

There are really awesome people in JS world. One of them is Jeremy Ashkenas - creator of Backbone.js - great frontend mv* framework; Underscore.js - utility library that makes functional programming possible in js  and Coffeescript - small language that compiles directly to js.

Coffeescript is basically a dialect of js which adds a syntactic sugar to language. As we know javascript suffers from his bad parts http://www.youtube.com/watch?v=hQVTIJBZook, which made this expressive language unpopular to traditional programmers - especially those who are educated in classical OO way. That includes global variables, prototype inheritance, that keyword...

Coffeescript tends to bridge that gap, and liberate expressive power of JS by eliminating it's bad parts. In my opinion, it completely succeeded. It is best to write some code, in order to understand.

First, variable. No need for var keyword anymore:

var a = "Hello"
// becomes
a = "Hello


Next, function keyword. Function is one of the most used keyword in js, so it can become hard to type it over and over again. In coffeescript , there is fo function keyword. You just have to do this:

function fn(){
    // some code
}

// becomes
fn = () ->

// and if there is no arguments

fn = ->


Prototype keyword is also avoided:

// instead

Fn.prototype...

// now we use

Fn::some_prop

Objects are simplified:

// No curly braces around object properties:
myObject =
    prop: "some_prop" // needs proper usage of indentation

Boolean
// true becomes yes:
a = yes

// false becomes no:
b = no

No parentheses:
// in most cases there is no parentheses
console.log "Hello Coffee"

In coffee, statements are expressions, so you can write:

console.log if is true // expression is value

There is much more to discover. Writing code in coffee is like driving a new car after old cluncer. So take a next step in your developer life, and start to compile coffeescript, that's fits perfectly with node and backbone ( coincidence ? )

Happy Coding.

Understanding express.js middleware

Huh;

I struggled real hard to understand these concepts, which can be confusing, due to node async nature.

Let's start with wiki definition:
In its most general sense, middleware iscomputer software that provides services tosoftware applications beyond those available from the operating system.
Translated to express, middleware is a layer that do all the things beside basic those that node http module offfers.
Express uses connect functionality for that purpost.  In the beginning express and connect were separated ( connect was dependency ), but lately express includes connect by default.
So here are some important points to understand, when dealing with middleware and routing in general in expressjs :
  • Node.js itself offers an http module, whose createServer method returns an object that you can use to respond to HTTP requests. That object inherits the http.Server prototype.
  • Connect also offers a createServer method, which returns an object that inherits an extended version of http.Server. Connect's extensions are mainly there to make it easy to plug in middleware. That's why Connect describes itself as a "middleware framework," and is often analogized to Ruby's Rack.
  • Express does to Connect what Connect does to the http module: It offers acreateServer method that extends Connect's Server prototype. So all of the functionality of Connect is there, plus view rendering and a handy DSL for describing routes. Ruby's Sinatra is a good analogy.
And final tip - it is very important  to include middleware functionality in the right order in your config. file. Here's a link to help http://www.senchalabs.org/connect/

Happy Coding.


4/06/2013

Vim bundle for coffeescript

Hello internet :)

I see that this blog is a bit rusty so let's give him some fresh air.
Along the work, the reason for my inactivity is that I discovered some great resources online that teach about ( take a deep breath, please ):

- testing ( oh yes I am becoming a real developer )
- coffeescript  ( now I don't have breath )
- mongodb ( let your data dance )
- advanced vim tutorial
- html5 cool features
- expressjs, jade...

I'll write about this later, but for now here are some tips for installing coffeescript plugin on vim. First go to git and pull this to your .vim/bundles folderhttps://github.com/kchmck/vim-coffee-script.git

Now the problem ofcourse is that settings for these great plugin are avaliable if you use pathogen plugin installer. But if you  use Vundle, which is more advanced that you'll have to make some slight tweeks in your .vimrc  Along with suggestions on forums, how to finally start you coffee syntax in vim, I couldn't do it.
And finally, magic happened - one more breath please: just put manually this line in your .vimrc file ( in the bundle list ) and it should work:
Bundle 'vim-coffee-script'
Happy Coding