Some shit for your javascript: The Module Pattern

Posted by on July 18, 2015

The Module Pattern was originally defined as a way to have software modules in a programming language with incomplete direct support for the concept.

This pattern in JavaScript is used to mimic the classes concept and focuses on public and private encapsulation in such a way that we’re able to include both public/private methods and variables inside a single object.

The Module Pattern is, the most commonly used design pattern and widely accepted, you can find it in a number of large projects such as jQuery, Dojo, ExtJS, AngularJS and YUI.

To implement this pattern we will use another one know as the Immediately-invoked function expression

What? Well, this one:

(function () {
  // the code here is executed once in its own scope
})();

It declares a function, which then calls itself immediately.

So, lets watch a sample of the Module pattern:

var MyFancyModule = (function () {

  var privateMethodOne 		= function () {};
  var privateMethodTwo 		= function () {};
  var privateMethodThree 	= function () {};
  
  return {
    publicMethodOne: function () {
      // You can call `privateMethodX()` you know...
    },
    publicMethodtwo: function () {

    },
    publicMethodThree: function () {

    }
  };

})();

Now you can get an idea on how to organize your shitty code.

The Python Tutorial 2.7 - unofficial book now available

Posted by on July 7, 2015

Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs.

The Python Tutorial 2.7 - unofficial is based on a work available at https://docs.python.org/2/tutorial/index.html

This book is just a compilation and the purpose is to provide the Python documentation in multiple formats, because the web is not the only place for reading xD

This release includes three convenient formats:

  • PDF (for Mac or PC)
  • EPUB (for iPad, iPhone, and other ebook readers)
  • MOBI (for Kindle)

Now go and get your copy for free at https://leanpub.com/python-tutorial-27

Stop knocking a nail into wood using a saw and use the right language for the job

Posted by on December 17, 2014

Choosing a programming language uniquely suited to solving a particular problem can lead to productivity gains and better quality software.

We are always chasing those improvements, because, when the language fits the problem, programming becomes more direct and code becomes simpler and easier to maintain. A pleasure, because there is not an unnecessary friction.

But, the major issue appears when you try to link solutions from different languages.

To solve this, we have Apache Thrift

Apache Thrift solves problems associated with building applications which collaborate across language boundaries. In addition to normalizing data for cross language communications, Apache Thrift also provides a complete remoting framework making it trivial to build cross language networked services.

So, what exactly is Apache Thrift?

Apache Thrift is an open source cross-language serialization and RPC framework. With support for over 15 programming languages, Apache Thrift can play an important role in a range of distributed application development environments. As a serialization platform Apache Thrift enables efficient cross language storage and retrieval of a wide range of data structures. As an RPC framework, Apache Thrift enables rapid development of complete polyglot services in a few lines of code.

Polyglot services? Yes!

You can build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.

Apache Thrift allows you to define data types and service interfaces in a simple definition file. Taking that file as input, the compiler generates code to be used to easily build RPC clients and servers that communicate seamlessly across programming languages. Instead of writing a load of boilerplate code to serialize and transport your objects and invoke remote methods, you can get right down to business. Thrift definition example file:

service Calculator extends shared.SharedService {

   void ping(),

   i32 add(1:i32 num1, 2:i32 num2),

   i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),

   oneway void zip()

}

Having the Thrift definition file you can now generate the client and server side.

So, is time for you to go for a deep exploration on how Apache Thrift works so you can choose your hammer.

http://thrift.apache.org

Using PostgreSQL JSON data type like a PRO

Posted by on October 3, 2014

For those who dont know: PostgreSQL, often simply “Postgres”, is an object-relational database management system (ORDBMS) with an emphasis on extensibility and standards-compliance. As a database server, its primary function is to store data, securely and supporting best practices, and retrieve it later, as requested by other software applications, be it those on the same computer or those running on another computer across a network (including the Internet). It can handle workloads ranging from small single-machine applications to large Internet-facing applications with many concurrent users. Recent versions also provide replication of the database itself for security and scalability. Check http://en.wikipedia.org/wiki/PostgreSQL

JSON

JSON has existed for a while in Postgres. Though the JSON simply validates that your text is valid JSON, which can be really appealing at the very first moment, but really really frustrating when you have a huge collection and some of the documents contains bad values or there are missing some fields.

So to avoid this, begining with PostgreSQL 9.3 there are great features which can turn it into a NoSQL database, with full transaction support, and regarding to this article, with the ability to store JSON documents with constraints on the fields data, even defined operators for the JSON type, which give you easy access to the fields and values.

So, Less talk, more code xD

Imagine that we want to store this document:

{
    "id": 1,
    "sku": "skucode",
    "price": 58.0
}

Having:

  • id > 1 and with no null value
  • sku > 0 and no null value
  • price >= 0.0 and not null value

And id , sku should be unique.

For every document we want to store.

We just need to create a table like this:

CREATE TABLE article (
    item JSON,
    CONSTRAINT validate_id CHECK ((item->>'id')::integer >= 1 AND (item->>'id') IS NOT NULL ),
    CONSTRAINT validate_sku CHECK (length(item->>'sku') > 0 AND (item->>'sku') IS NOT NULL ),
    CONSTRAINT validate_price CHECK ((item->>'price')::decimal >= 0.0 AND (item->>'price') IS NOT NULL)
)

And for unique values, we can create some indexes like:

CREATE UNIQUE INDEX uix_article_id ON article((item->>'id'));
CREATE UNIQUE INDEX uix_article_sku ON article((item->>'sku'));

Now, if you try to insert this:

INSERT INTO article(item) VALUES('
{
    "id": 1,
    "price": 58.0
}');

You should get something like this:

ERROR:  new row for relation "article" violates check constraint "validate_sku"
DETAIL:  Failing row contains (
{
    "id": 1,
    "price": 58.0
}).

Query not active INSERT INTO article(item) VALUES('
{
    "id": 1,
    "price": 58.0
}')
When you try to insert a valid document but repeating id sku , you should get something like:
ERROR:  duplicate key value violates unique constraint "uix_article_sku"
DETAIL:  Key ((item ->> 'sku'::text))=(f) already exists.

Query not active INSERT INTO article(item) VALUES('
{
    "id": 1,
    "sku":"f",
    "price": 58.0
}')

For Homework:

Study about searching , hint: check ->> operator , indexes on expressions and about JSONB

And thats all folks.

Use Koans to learn a new programming language

Posted by on September 29, 2014

A Koan is a Buddhist idea. A Koan presents a situation, dialog, statement, story or question that must be thought about. Many of these questions cannot be answered outright, having multiple interpretations.

The goal of a Koan is not the answer, but thinking about the question.

Now, regarding to learning a new programming language.

Koans premise is that there are set of broken tests and it is your mission to repair them. Along the way, the tests are fixed by utilizing the language, syntax and culture of Test Driven Development (TDD). Learning the culture is one of the coolest parts of the Koans.

The goal is to learn the language, syntax, structure, and some common functions and libraries. The goal is not to speed through or skip around, but take your time and add more tests, break and fix again. You have to fill in that missing piece in order to move on to the next Koan. These Koans are thinking exercises, you have to understand how the language feature works before you can move on.

I like a lot the idea around Koans, because i think the real learn happens by DOING and no other way.

I think Koans are a great way to learn a new language, they won’t teach you the intricate in’s and out of the language, neither will they teach you the pro’s and con’ nor the real life use of the language, but, i think is a good way to get you confident up and running, your knowledge of the syntax differences and the general feel of a language.

With That said, I can only add a list of available Koans repositories for different languages.


The original

Ruby Koans

See also

Python Koans
Java Koans
Go Koans
Scala Koans
Clojure Koans
Lisp Koans
Perl Koans
Haskell Koans
Erlang Koans
ColdFusion Koans
Bash Koans
.Net Koans
C++ Koans
Lua Koans
Groovy Koans
Objective C Koans

Front-end languages

JavaScript Koans
CoffeeScript Koans

Statistics

R Koans

Databases

MongoDB Koans

Frameworks

Ruby on Rails Koans

Other technical tools

Git Koans
Vim Koans

Source: http://www.lauradhamilton.com/learn-a-new-programming-language-today-with-koans

If you need more, search the word “Koan” in https://github.com/ or https://bitbucket.org/