Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,786 members, 7,810,051 topics. Date: Friday, 26 April 2024 at 07:24 PM

Coffeescript - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Coffeescript (1258 Views)

C++, Ruby, Coffeescript: A Visual Comparison Of Language Complexity / Coffeescript - What Is Coffeescript? (2) (3) (4)

(1) (Reply)

Coffeescript by kadeerna: 2:59pm On Jun 06, 2012
Languages that follow the C (C++) influenced syntaxes have been becoming more and more of a pain to write in, thanks to the rapid introduction and uptake of languages with easier semantics to understand, and easier to write on the fingers.

The Javascript langugage used in billions of web pages today derives many of its constructs from C. In other words, it is a pain to write today.


function() {
var string = "Hello, World!";
alert(string);
};


What is wrong with the above block, a lot!.
- function <- what is that? It won't kadeerna, it won't be anything else.
- { ... } <- OK?
- ; <- Don't even get me started on this one. Follow the brouhaha over a single missing | left out in the comments on Github for the Bootstrap css library project
- alert(...) <- (.)_(.)
- var <- Resharper (in VS), and Webstorm don't gimme a break on this one

Compare the above written block to its equivalent in CoffeeScript.


() ->
string = "Hello, World"
alert string


Does that look sexy or what?

Coffeescript is a language (extension to javascript) for that brings Ruby styled constructs and ease of coding to the language we all love, Javascript.
The language which drops a lot of the syntactical noise of Javascript compiles down to easy to read, compiled, and optimized Javascript which is read,
and executed by the browser in client side usage or a Javascript engine like V8 in server-side usages like NodeJS environments.

You cant try it out in you browser here: http://coffeescript.org

Noise like function keywords, parentheses, braces, semi-colons, var are eliminated allowing the developer code their application logic in sweet memories of last nights .... and not of tracking down a missing ;. In the speak of the languages' creators, "CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way."

Quite a lot of Ruby style constructs have been imported to almost seamlessly supplement the Javascript language in a new way.

For example,


teen = true if age < 16 && age > 12 # inversing assignments and declarations to make it better readeable

# creating an object
object =
prop1: 1
prop1: 2
func_prop: () -> 3

greet = (name) -> alert 'Hello, ' + name # initialising a list and calling a function on its members
greet name for name in ['kadeerna', 'seun']

_alert = (number) -> alert number # who doesn't need this, a countdown timer. BLAST OFF!
_alert number for number in [3..1] # range [Top...Bottom]


# some further advance looping and comprehension
people = [{ name: 'kadeerna', gender: 'm' }, { name: 'finegeh', gender: 'f' }]
_alert = (name, gender) ->
salutation = if gender == 'm' then 'Mr' else 'Miss'
alert "#{salutation} #{name}."
_alert name, gender for {name, gender} in people

# destructuring assignment
[nairaland, twitter, facebook] = ["kadeerna", "abdulkadirna", "abdulkadir.abdulkadir"]

# destructuring of nested assignment
structure = [
{ name: "kadeerna", posts: [ "NHibernate Brief", "Buidling Large Scale Apps with Backbone.js" ] }
{ name: "seun", posts: [ "Following Page And Less Annoying Notifications", "New Content Counters And Trending Topics Cleanup" ] }
{ name: "pc_guru", posts: [ "Dell Laptop For Sale", "Interswitch Api Pdf" ] }
]
records = [name, posts:[]] = structure
console.log records

# object orientation as right as possible
class Person
constructorsad@name) ->

addressSelfsad) =>
alert "Nothing O."

class Manager extends Person
addressSelfsad) =>
alert "I am #{@name}, a Manager."

class Teller extends Person
addressSelfsad) =>
alert "I am #{@name}, a Teller."

manager = new Manager "seun"
manager.addressSelf()

teller = new Teller "kadeerna"
teller.addressSelf()


Ok, thats plenty for 1 post me thinks. You should get the gist by now. CoffeeScript is simply amazing. It is indentation based too using whitespace to delimit blocks of code.

It had reduced LOCs of our projects by as much as 1/3 in one instance. It is readable, easy on the eye, easy to hack, strongly in development. I left out a lot of really time saving features of the language.

(1) (Reply)

C's And C++ Base / Game Developer Needed For A Project / Withdrawal Issues On Fiverr - Payoneer

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 13
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.