Thursday, April 16, 2015

The Clean Code Talks - Don't Look For Things!


Clean Code is maintainable

Source code must be:
1) Readable
2) Extensible
3) Testable

We are going to stress on the most neglected of these attributes.

To test a method:
1) we need to instantiate an object (system under test) and all the helpers(mock providers, stubs) etc. but it turns out that we put lot of code in constructor which makes it very difficult for create object of it.

Suppose we have a pest call in our constructor. Now while testing, we actually have to make a pest call for instantiation process.

class Story{
public function __construct() {
$pestObj = new Pest();
$this->content = $pestObj->get('');

But instead of this, if I ask for what I need :

class Story{
public function __construct($pestObj) {
$this->content = $pestObj->get('’);

What I can do now is, while testing, I can mock get method of $pestObj and we wont actually have to make get request.

Now everything looks alright but look at this for a moment:

1) Story knows about a pest client and makes a GET request just for its content.

How about this:

class Story{
public function __construct($text) {
$this->content = $text;
class StoryStore{
public function __construct($pestObj) {
$this->pest = $pestObj;

public function getStory(){
return new Story($pest->get(''));

This helps because tests are about instantiating small portions of application and run them and assert some output.
And if it is difficult to instantiate an object its not just going to be difficult to test that object but also other parts that need that object.
Its a kind of transitive problem.

1) So test case has to successfully navigate the constructor each time the object is needed.
SO constructor ideally should be very simple. Just simple assignments and we are good to go.

Service Locator (context, service container):

In this system we pass the responsibility of creating very hard to instantiate objects to service locator and then in code we pass this service locator around.

class MyController extends Symfony\Bundle\FrameworkBundle\Controller\Controller
public function testAction()

and Symfony Controller does implement ContainerAwareInterface, and has a service locator role.

But the problem with service locators is that they encourage the APIs that lie.
You can't tell from looking at the definition of a class what its dependencies are because they aren't being injected,
instead they are being yanked out of the service locator

Now we need to test a class that only says I need a service locator.
But we have no idea what all we need to make the test run. we have to see the source code.
what all are needed to run the class or see where it crashes and what needs to be over ridden.
and most of all it violates the law of demeter.

Law of Demeter also known as principle of least knowledge is a coding principle, which says that a module should not know about the inner details of the objects it manipulates.
If a code depends upon internal details of a particular object, there is good chance that it will break as soon as internal of that object changes.

(Always ask for the things/references you need instead of searching for those references.)

According to Law of Demeter, a method M of object O should only call following types of methods :
Methods of Object O itself
Methods of Object passed as an argument
Method of object, which is held in instance variable
Any Object which is CREATED locally in method M

More importantly method should not invoke methods on objects that are returned by any subsequent method calls specified above
and as Clean Code says "talk to friends, not to strangers"

-> Only Ask for objects you DIRECTLY need to operate on.
-> $a->getX()->getY() is a no no
-> $serviceLocator->getService() breaks law of demeter
->Dependency Injection of specific objects we need.

Also if we want to reuse the class that require service locator then we have to supply client with all the compile time dependencies which include service locator.
But service locator contains many other services and dependencies.
So here we have a problem, in order to give a class we need to give service locator and in order to give service locator we need to give most part of our application.

Breaking the Law of Demeter is Like Looking for a Needle in the Haystack

public class XMLUtils {
public function getFirstBookCategoryFromXML(XMLMessage $xml) {
return $xml->getXML()->getBooks()->getBookArrary(0)->getBookCategory();

This code is now dependent upon lot of classes e.g.

Which means this function knows about XMLMessage, XML, Book and BookCategory.
It knows that XML has list of Book, which in-turn has BookCategory, that's a lot of information.

If any of the intermediate class or accessor method in this chained method call changes, then this code will break.

Its an understanding between you and code how to traverse the haystack and get the thing you need.
All the things in between are irrelevant all u need is the book Category

Fundamentally this is what's wrong with service locator, asking for things we don’t directly need but retrieving the needed from it.

In summary, abandon the use of new operator.
-> new operator ideally should either be inside factory or in tests.

But in some cases it is perfectly OK like instantiating a HashMap(), a collection . No need to ask for it in the constructor.
This is because they are the leaves in your application graph. There is nothing behind these. Its the end of the road.

But for services, objects that need collaboration, those need to ask for their dependencies.
Construction should be done either by DI container (symfony container), or builders or factories.

Any kind of indirections we introduce in the code makes it very hard for writing tests.
This cost becomes double the cost if this is done in constructor. As now it has to be shared by lots and lots of the tests that use that specific object.

Writing Testable Code

Flaw #1: Constructor does Real Work
Warning Signs
  • new keyword in a constructor or at field declaration   
  • Static method calls in a constructor or at field declaration
  • Anything more than field assignment in constructors
  • Object not fully initialized after the constructor finishes (watch out for initialize methods)
  • Control flow (conditional or looping logic) in a constructor
  • Code does complex object graph construction inside a constructor rather than using a factory or builder.
  • Adding or using an initialization block
Flaw #2: Digging into Collaborators
Warning Signs -
  • Objects are passed in but never used directly (only used to get access to other objects)
  • Law of Demeter violation: method call chain walks an object graph with more than one dot (.)
  • Suspicious     names: context, environment, principal, container, or manager

Flaw #3: Brittle Global State & Singletons
Warning Signs
  • Adding or using singletons
  • Adding or using static fields or static methods
  • Adding or using static initialization blocks
  • Adding or using registries
  • Adding or using service locators

Flaw #4: Class Does Too Much
Warning Signs
  • Summing up what the class does includes the word “and”   
  • Class would be challenging for new team members to read and quickly “get it”
  • Class has fields that are only used in some methods
  • Class has static methods that only operate on parameters

So now we know how to write UN TESTABLE code and wreck havoc on person writing UTs:
  1. Make Your Own Dependencies – Instantiate objects using new in the middle of methods, don’t pass the object in.
  2. Heavy Duty Constructors – Make constructors that do lots of work in them.
  3. Depend on Concrete Classes – Tie things down to concrete classes – to avoid interfaces wherever possible.
  4. Conditional Slalom – Always, always, feel good when writing lengthy if branches and switch statements. These increase the number of possible execution paths that tests will need to cover when exercising the code under test.
  5. Depend on Large Context Objects – Pass around ginormous context objects (or small ones with hard to construct contents).
  6. Use Statics – Statics, statics everywhere!
  7. Use More Statics – Statics are a really powerful tool to bring TDD Infected engineers to their knees. Static methods can’t be overridden in a subclass (sometimes subclassing a class and overriding methods is a technique for testing). When you use static methods, they can’t be mocked using mocking libraries
  8. Use Global Flags – Why call a method explicitly? Just like using flag to set a flag in one part of your code, in order to cause an effect in a totally different part of your application
  9. Use Singletons Everywhere – Why pass in a dependency and make it obvious when you can use a singleton?
  10. Look for Everything You Need – By Looking for things you are asserting your objects dominance as the object which knows where everything is. This will make the life of tester hard, since he will have to mimic the environment so that your code can get a hold of what it needs.
  11. Utils, Utils, Utils! – Code smell? No way – code perfume! Litter about as many util and helper classes as you wish. These folks are helpful, and when you stick them off somewhere, someone else can use them too. That’s code reuse, and good for everyone, right? Be forewarned, the OO-police will say that functionality belongs in some object, as that object’s responsibility.

Friday, September 20, 2013

Table size and problem of unused table space after row deletion in MySQL

Finally got some time to give some useful advice for all developers dealing with MySQL.

Problem Scenario - 

I had a table with schema like this -

I deleted some 1.2 lac records on the basis of date and when I retrieved the table size using this query I got exactly the same size of table before deleting.

After removing the 'DATA_FREE' from the same.

There I came to pause and ponder that why the free space is still unusable ?

Reason - 

'DELETE' invalidates the record and size of the records used to occupy data. it may be reused later. This problem is quite similar to de-fragmentation of disk drive in windows to improved the disk efficiency.

Also, this scenario is common with the tables using variable length rows having column types like 'VARCHAR', 'TEXT', 'BLOB', 'TEXT', 'VARBINARY'.

Solution - 

Meanwhile this cannot be stated as a problem cause according to MySQL guys - "Deleted rows are maintained in a linked list and subsequent INSERT operations reuse old row positions".

However if you want to get that space now for peace of mind and soul, so you can run 'OPTIMIZE' on the table like - 

Remember 'OPTMIZE' works only on 'MyISAM' and 'InnoDB' engines. So this can be done in manner when ever you deleting the records from the table either manually or by CRON job place one more query to get this done.

Addon -

Posting the query to get the size of all the tables with sorting on the basis of size with exact data and after removing data free.

Thanks and will be posting more ... 

Friday, March 23, 2012

Installing node.js on Ubuntu 11.10 or Ubuntu 12.04

Node.js - A server side JavaScript framework highly scalable works on the back of the giant beast V8 JavaScript Engine from Google.

Always favor to do some compiling to get the best - 

1.) Download the latest from node.js website - 

bharat@ProBook:~/node/node-v0.6.13$ wget ""

2.) Dependencies to be resolved first - 

bharat@ProBook:~/node/node-v0.6.13$ sudo apt-get install g++ curl libssl-dev apache2-utils

3.) Unzip the downloaded item – 

tar -xvf node-v0.6.13.tar.gz

4.) Final installation - 

bharat@ProBook:~/node/$ cd node-v0.6.13

bharat@ProBook:~/node/node-v0.6.13$ ./configure

bharat@ProBook:~/node/node-v0.6.13$ make

bharat@ProBook:~/node/node-v0.6.13$ sudo make install

and its done here.

Tuesday, November 8, 2011

Java Interview Questions

--> This is one of the important post that I decided to give it to you all, it contains most of the questions asked (worth asking) to me in various technical job interviews. This is not a one time post, I will be regularly updating it like any other wiki page, so you might encounter some new questions and facts once you come back again.

[Q-01] What are the use of static variables also state the ways how it is different from a constant ?

[A-01] Static variables are class variables, as local variables cannot be declared as static. In most of the cases 'static' is accompanied by 'final' as by definition it should be used as CONSTANTS, some very special cases includes the statistics-gathering for that class where non-final-static member variables are used to deal with rare and specific conditions like counting the number of instances created.

[Q-02] Give the code for singleton pattern implementation, also take care of multi-threaded environment.

[A-02] The code are given below for the same:
public class Singleton
   private static Singleton singleInstance = null;

   private Singleton()
      // Exists only to defeat instantiation.
   public static Singleton getSingleInstance()
      if(singleInstance == null)
              synchronised (Singleton class)
                      if(singleInstance == null)
                             singleInstance = new Singleton();

      return singleInstance;

 [Q-03] What are the two methods that every object class overrides ?

[A-03] 'hashcode()' and 'equals()' are the methods which overrides object class. 

[Q-04] What property will a Class and a Method acquires when subjected to association with the 'final' keyword ?

[A-04] A final Class cannot be subclasses or extended while a final method cannot be overridden, in case of final member variable it is counted as constant. 

[Q-05] How many types of exceptions do we have in java, what are the differences between them?

[A-05]  Here we have following types of exceptions:
Checked Exceptions
Environmental error that cannot necessarily be detected by testing; e.g. disk full, broken socket, database unavailable, etc.
Virtual machine error: class not found, out of memory, no such method, illegal access to private field, etc.
Run-time Exceptions
Programming errors that should be detected in testing: index out of bounds, null pointer, illegal argument, etc.
Checked exceptions must be handled at compile time. Run-time exceptions do not need to be. Errors often cannot be.

[Q-06] Can we stop the execution of finally block in exception handling ?

[A-06] Generally a finally block is something meant to be executed irrespective of the occurrences of the exceptions, but however it can be by passed if we irreversibly terminate the execution of the program like this - System.exit(1);

[Q-07] Discuss multiple inheritance in java ?

[A-07] Some object oriented languages, allow a class to inherit from more than one unrelated class. This is called multiple inheritance which java eventually does not supports and is different from the multi-level inheritance in this section. Most of the things that can be accomplished via multiple inheritance can be handled by interfaces in Java.

[Q-08] Why java cannot have multiple inheritance ?

[A-08] The simple answer to the big question is that it will add too much complexity with a very little benefit, in terms of literal basics, we can say that "Classes" in java represents "Noun", whereas "interfaces" represents "adjectives". A child cannot have more than one father, but they can have multiple behaviors (interfaces). A specific problem is associated when we used multiple inherited classes object which is known as "Double Diamond Inheritance Problem". 
The following example explains more about it... 
class Base {
    protected String text;
class SuperOne extends Base {
    protected SuperOne() {
class SuperTwo extends Base {
    protected SuperTwo() {
// Impossible multiple inheritance class; just to explain the problem
class Derived extends SuperOne, SuperTwo {
    public void printText() {
What would you expect that the method printText() prints? Should it print "one" or "two"? Are there two instances of class Base that are part of the instance of class Derived? You don't have this problem with multiple interface inheritance, because an interface does not contain data. Multiple interface inheritance is something completely different from multiple class inheritance, and I don't see any big problems with multiple interface inheritance.

[Q-08] What is JVM memory types ?

[A-08] Java has two types of memory when we talk about JVM, that is Heap Memory and Non Heap Memory.

# Heap Memory 
                          Its a shared memory where multiple threads share the same data, class instances and arrays are stored in heap memory.
# Non-Heap Memory
                                  It comprises of ‘Method Area’ and other memory required for internal processing. So here the major player is ‘Method Area’. As given in the last line, method area is part of non-heap memory. It stores per-class structures, code for methods and constructors. Per-class structure means runtime constants and static fields.

[Q-09] Explain the significance of 'this' keyword in java programming ?

[A-09] 'this' refers to the current instance of the method in which it is used. The following are the scenarios which uses 'this' keyword in java programming - 
# 1 - To specifically differentiate between instance variable to arguments or other local variables.
protected String _userName;

public void setUserName(String _userName)
        this._userName = _userName; //extensively used in case of writing getters/setters
# 2 - To pass the current java instance as parameter.
# 3 - To return the current java instance.
ClassName someMethod() {
        return this;
Note: Should be used carefully while dealing with inner class context, which returns inner class instance rather than outer class.

# 4 - To get the handle of current class. 
Class SampleClass = this.getClass(); // this methodology is preferable in java
Though this can be done by, Class className = ABC.class; // here ABC refers to the class name and you need to know that! 
As always, java this is associated with its instance and this will not work in static methods.

[Q-10] Elaborate the association of keyword 'static' in java programming ?

[A-10] Lets take it as in term of parts starting from -

# static variables
  1. variable that belongs to a class not to an object or instance.
  2. They are initialized only once at the very beginning, these are initialized first before any instance variable.
  3. A single copy to be shared by all the instances.
  4. It can be accessed directly by class name without any need for creation of instances.
#static methods
  1. method that belongs to class not to an object or instance.
  2. It can only access static data, cannot use instance variables.
  3. It can only call static methods not any instance method.
  4. It can be called using class name no need for object creation.
  5. It cannot refer to 'this' or 'super' keywords in anyway.
  6. It can be shadowed by another static method in a subclass, as long as original method is not declared as final.
  7. A static method cannot be overridden by a non-static method (instance method).
  8. Static method is implicitly final in nature.
A main method is always static in nature because it must be accessible for an application to run, before any instantiation should take place.

# static block
The static block, is a block of statement inside a Java class that will be executed when a class is first loaded in to the JVM. A static block helps to initialize the static data members, just like constructors help to initialize instance members

[Q-11] Why Strings are immutable (Objects) ?

[A-11] - First of lets understand what the term immutable stands for. In generic terminology immutable objects are the ones whose every modification creates a new String (Object)or more over an immutable object is something whose state cannot be modified when after it has been created.

- Referenced Variables - In a string pool facility a same string can be referenced through many “referenced variables” for example “sampleStr” can be denoted by two String objects Str1=”sampleStr”; and Str2 = “sampleStr”;  in case of mutable scenario changes in sampleStr for Str1 will be reflected everywhere which is not required.

- Security Threat - Strings are passed as the parameters for various secure methods if make it mutable then anyone can gain access or modify it, that leads to serious vulnerabilities.

- Sync issues - immutability offers safe handling of strings in multi-threaded environments, needn’t to synchronize the string operations externally.

- Caching hashcode -  Being immutable Strings caches its hashcode and don’t re-calculate them again and again making it fast and usable as hashmap key, thus guarantees hashcode to be same on multiple occurences.

- Class Loading -  Strings is used by class loading mechanism and have profound and fundamental aspects of security with it.

[Q-12] Why Char arrays are preferred over Strings for storing passwords?

[A-12] - Since strings are immutable they might be present in string pool till garbage collector truncates them, thats why the probability of their reuse by someone who have access to memory dump (as if the password is stored in plain text). java itself recommends the same. printing of plain text on consoles and logs is pretty high as in case of char array only memory location gets printed.

[Q-13] What is the difference between String Builder and String Buffer classes ?

[A-13] - String Buffer’s all public methods are synchronized which make it thread-safe but at the same time slow. String Builder is not synchronized thus fast, else everything is nearly same. These are the used to get rid of String class immutable problem which overwhelms the memory heap on various operations over string.

[Q-14] Why use of == comparison operator in comparing Strings are discouraged ?

[A-14] - Because ‘==’ just check the if the compared reference variables points towards the same string in the java heap (reference equality) and since strings are immutable thats why it gives sense to the fact that two string literals in the string pool set refer same string object which gives sense that ‘==’ comparison is almost wrong way to do this.

[Q-15] Significance of Runnable interface ?

[A-15] - Implementing runnable interface is one of the ways to create java thread, public void run() is the only method that needs to be overridden for the same.
- Runnable provides the means for the class to be active while not sub-classing it.
- Runnable should be used by any class whose instances are intented to be used by a thread.
- Runnable interface should be used when you are planning to override the run() method not any other Thread Class method.

[Q-16] What is an Adapter Class ?

[A-16] -  Adapter Class is normal class that acts as an Adapter between the Interface and the Class implementing it, such as it only contains empty implementations of all the interface methods.
Benefit - Concrete class using adapter class needn't to implement all the methods unlike implementing the interface. Thus reducing code length and code complexity.
Example - Generic Servlet acts as adapter class for Servlet Interface.