My Sites


Sunday, April 10, 2016

A Developer guide to design principles and patterns

A design pattern represents a set of guidelines which will help to avoid having a bad software design. The SOLID principle is the best examples for commonly used design principles. 

S - Single Responsibility Principle
O - Open/Closed Principle
L - Liskov Substitution Principle 
I - Interface Segregation Principle
D - Dependency Inversion Principle

Single Responsibility Principle - Classes/ Modules must have only one reason to change. High cohesion should be there, means class should has a single focus and single thing well done. And also a class should be loosely coupled.The single responsibility should be encapsulated by the class.Therefore strive for high cohesion and loosely coupled systems.

Open/Closed Principle - Classes/Modules should be open for extension and closed for modification. That means new behaviour can be added in future but it should not modify the existing source code and make it complex. This embrace to use or reply on abstractions/Interfaces.

Liskov Substitution Principle - Subtypes can be substitutable for their base types. It's about programming to super type. This allows us to use polymorphism in our application.

Interface Segregation Principle - Clients should not be depend on methods that they do not use. Use multiple small cohesive interfaces instead of one big fat interface. Again loosely coupled and high cohesion is embraced.

Dependency Inversion Principle - High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstraction should not depend on details/concrete. And details/concretes must depend on abstractions. Dependency Injection is based on Inversion of control. It is a design pattern which tells how one object knows about its other dependent object. Basically, it is about how components get hold in their dependencies.
Programming to interface not to the implementation.

A design pattern is a general reusable solution , which can be used to solve a problem in our application. It is kind of like a proven template.
Design patterns can be categorized to Creational, Structural, Behavioural, Concurrency and Architectural and etc.

Design pattern common usage
Designing Service Layers - Module Pattern
Overly Complicated Object Interfaces - Façade Pattern

Visibility Into State Changes / Messaging Middlewares - Observer Pattern

What is CommonJS pattern ?

CommonJS pattern in commonly used in many javascript frameworks, libraries and environments. The best example is Node.js. The objective of this pattern is to encapsulate your javascript files into reusable modules so that it can be referenced by other modules.
  • Get reference to dependency
var dependency = require('/path/to/file');
  • Declare Module
var MyModule ={}
  • Expose to others
module.exports = MyModule;

Saturday, April 9, 2016

What is Machine Learning ?

What is actually ML ?

  • We can call it as a sub-filed of Artificial Intelligence.
  • It is basically about predict future. Think like you have a bunch of raw data. Through ML we can create a model which will identify patterns that exist on those data. So when a new data set is entered, the created model will be able to recognize those patterns and predict the future.
  • There are multiple applications of ML, such as detecting credit card fraud, Determining whether a customer likely switches to a competitor. (Customer churn / loss of clients or customers), NLP (Natural language processing) , Computer vision, including object recognition, Robot locomotion , Sentiment analysis, Speech & handwriting recognition, Stock market analysis, Deciding when to do a maintenance on a factory robot.
  • Simply we can describe the process as                                                     A bunch of data --> Identify Patterns --> Create a model (Ability to recognize patterns) --> Predict Future
  • This process is iterative and should be periodically repeated if you want to get a good model.
  • Deep Blue is the first ever chess-playing computer developed by IBM, which I found interesting when it's come to real-world AI . AlphaGo is another fascinating example , which is able to play the board game Go. Nowadays the big topic is Tesla,(electric vehicles manufacturers). They use ML for the auto pilot mode in Tesla model 2/3.
  • There are plenty of open source libraries we can use to implement machine learning, such as scikit learning and Tenserflow.
  • Machine learning is the study of algorithms that learn from examples and experience.
What is a Classifier ?
  • We can call it as a function , it takes some data as input and assigns a label to it as output. Create a classifier by finding patterns in example is called Supervised Learning.Simply supervised learning is the technique to write classifier automatically.

Thursday, April 7, 2016

JavaScript Best Practices

The latest ECMAScript standard defines 7 data types:
  • Six data types that are primitives:
    • Boolean
    • Null
    • Undefined
    • Number
    • String
    • Symbol (new in ECMAScript 6)
  • and Object
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

best way to check something is available
if(typeof x !== 'undefined'){
    console.log('Exists');
}else{
    console.log('Not Exists');
}

Equality
==   If variables are 2 diffrent types , it will convet them to the same type and check
=== There will be no type conversion

Use === as default.

Node.js ENV variable
Use  node-foremon or nodemon

Tuesday, March 29, 2016

Node.js Essentials

http://expressjs.com/en/4x/api.html

    req.params
    req.body
    req.query
----------------------------------------------------------------
    app.get('/user/:id', function(req, res){
      res.send('user ' + req.params.id);
    });

-----------------------------------------------------------------
    var app = require('express')();
    var bodyParser = require('body-parser');
    var multer = require('multer'); // v1.0.5
    var upload = multer(); // for parsing multipart/form-data

    app.use(bodyParser.json()); // for parsing application/json
    app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded


    app.post('/profile', upload.array(), function (req, res, next) {
      console.log(req.body);
      res.json(req.body);
    });

Tuesday, March 1, 2016

Angular 2 + Common Issues and Fixes



The main tutorial 
https://angular.io/docs/ts/latest/quickstart.html#!#main
https://www.youtube.com/watch?v=W14_ZArh6eo

Angular2 QuickStart npm start is not working correctly.
  • make sure set access permission to /usr/lib/mode_modules.
  • make sure to run with sudo.
  • sudo npm install -g concurrently     
  • sudo npm install -g lite-server     
  • sudo npm install -g typescript

Wednesday, February 10, 2016

Angular Nodejs Session and Cookie Management

Angular Cookie
var testApp = angular.module('testAppModule',['ngRoute','ngMaterial','ngCookies','ngMessages']);

testApp.controller("TestController", function($cookieStore,$timeout,$routeParams, $q, $log,$http,$scope,$rootScope,$location,$mdDialog) {
var value =[];

value[0] = dataObj1;
value[1] = dataObj2;
$cookieStore.put('sessionCookie', value);

var cookieRelease =$cookieStore.get('sessionCookie');

});


https://docs.angularjs.org/api/ngCookies/service/$cookieStore
https://docs.angularjs.org/api/ngCookies/service/$cookies
An HTTP cookie (also called web cookie, Internet cookie, browser cookie or simply cookie) 4093 bytes

HTML 5 Local Storage

http://www.w3schools.com/html/html5_webstorage.asp
http://stackoverflow.com/questions/19867599/what-is-the-difference-between-localstorage-sessionstorage-session-and-cookies

if (typeof(Storage) !== "undefined") {
    // Store
    localStorage.setItem("lastname", "Smith");
    // Retrieve
    document.getElementById("result").innerHTML = localStorage.getItem("lastname");
} else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}


Node.js Cookie parser and Express-Session
https://www.npmjs.com/package/express-session 
https://www.npmjs.com/package/cookie-parser