Tuesday, December 3, 2019

Clean code practices with S.O.L.I.D. Principles

Writing code is the thing which most of software engineers do on daily basis but only few of them write clean code i.e. a code that is easy to understand, manage, scale and amend. Those people who write clean code are the most sought after developers and also the ones who has least number of production issues.

While there are many techniques of writing clean code but the most famous one is following S.O.L.I.D principles. It basically has 5 principles:
  • Single Responsibility Principle
  • Open/Closed Principle
  • Liskov Substitution Principle
  • Interface Segregation Principle
  • Dependency Inversion principle
We will try to learn more about these principles with the use of code examples from Python. Read through each of the below principles.

1. Single responsibility principle       “…You had one job” 
            — Loki to Skurge in Thor: Ragnarok

A class should have only one job.  If a class has more than one responsibility, it becomes coupled.  A change to one responsibility results to modification of the other responsibility.


2. Open/Closed principle
      Software entities (Classes, modules, functions) should be open for extension, not modification.


3. Liskov Substitution Principle
         A sub-class must be substitutable for its super-class.  The aim of this principle is to ascertain that a sub-class can assume the place of its super-class without errors.  If the code finds itself checking the type of class then, it must have violated this principle.


4. Interface Segregation Principle
         Make fine grained interfaces that are client specific clients should not be forced to depend upon interfaces that they do not use.  This principle deals with the disadvantages of implementing big interfaces.


5. Dependency Inversion Principle
         Dependency should be on abstractions not concretions
            A. High-level modules should not depend upon low-level modules. Both should depend upon abstractions.
            B. Abstractions should not depend on details. Details should depend upon abstractions.

There comes a point in software development where our app will be largely composed of modules.  When this happens, we have to clear things up by using dependency injection.  High-level components depending on low-level components to function.

Friday, August 17, 2018

getElements method in nightwatch js


Nightwatch js does not have getElements methods to locate multiple elements but it has provided with WebDriver methods specified in the WebDriver Specification. These methods can be used to create your own getElements method. Below is one such implementation

To achieve this, we have to set selector strategy which can be done by examining locator string. Below is my implementation

    /**
     * It will return the slector strategy for the given selector string
     */
    getSelectorStrategy: function (locator) {
        if (locator.startsWith("//")) {
            return "xpath";
        } else {
            return "css selector";
        }
    } 

Using above method, elements method can be implemented as below
const {client} = require('nightwatch-cucumber');

 /**
   * This will retrun array of elements for the given selectors
   */
   getElements: function (locator, callback) {
       var selectorStrategy = this.getSelectorStrategy(locator);
       return client.elements(selectorStrategy, locator, function (result) {
           callback(result.value)
       });
    }

Tuesday, July 24, 2018

Simple HTTP server on Node JS

Ever needed an HTTP server just to test out something. If you are working on Node platform then you can do so very easily by following the mentioned steps.


1. Simply paste below mentioned code in a file server.js
var connect = require('connect');
var serveStatic = require('serve-static');
connect().use(serveStatic(__dirname)).listen(8080, function(){
    console.log('Server running on 8080...');
}); 

2. Run this server.js via command prompt
node server.js

3. This will start a HTTP server on port 8080 and pickup the content from current directory

 This is quite useful when you want to see some static content using a server

Thursday, June 28, 2018

JSE Coin - Web minable coin

JSE Coin is the latest entrant in the cryptocurrency market and it has focus on  only one thing - to make web based payments simple and easy.

Can this be mined - yes it can be, but it not actually work in the way traditional mining works. The miner will solve a mathematical problem which will give you a ticket to lottery. Each lottery will give you chabce to win some JSE coins. So in a way you are mining the coin but in reality you are not.

I am still not sure how this mining will work when they will run out of the coins to distribute as it only seems like a marketing scheme.

One thing which is great about this coin is  that it is minable using web browsers like monero on coinhive. However tbe scripts that are doing moning does not got blocked by most of the major firewall and antivirus programs. So, you can monetize your web traffic using this coin.

For now it seems promising but can really say how it will perform in future. As still going through ICO process.

In case you are interested, you can avail a free account using below link. Once you have signed up, you can start mining using their web miner program.

Link to sign up - https://platform.jsecoin.com/?lander=3&utm_source=referral&utm_campaign=aff62467&utm_content=