Saturday, 2 June 2018

Living is like Coding

Our Life is also just like our #codes.
1) You miss your base conditions(exercise and hygiene) , one day your code (life) will throw a null pointer exception or will show a very abrupt behavior (Medical Issues).

2) You run into an infinite loop (thinking about past) , your code will get stuck their forever and will surely show a StackOverFlow error ( you will go into depression)

3) You don't debug ( act without thinking or dont check your word) your code is going to fail and is not going to meet your client's expectation ( is going to hurt your near and dear one's).

4) If your code's complexity ( attitude) is bad or average , it will not be liked much. Less people will favor it.

5) And If you give up on your coding (life) , then ultimately you are going to lose your job (You will lose your happiness) .

So ,
while( (--LIFE) > 0) {
StayHappy();
SpreadHappiness();
CarryPositiveAttitude();
AlwaysHaveHope();
FightForYourDreams();
LoveLaughLive();
LoveEveryone();
ValueRelations();
NeverGiveUp();
ThinkThenAct();
GiveTimeToNearDearOnes();
MakeFriends();
ShareMemory();
CareForEachOther();
KnowImportanceOfFamily();
AndAlwayLoveYourSelf();
}

A very neat and clean code for living life.

Facade Design Pattern

Facade Design Pattern ??
Lets understand this in very layman language.

Suppose you are very lazy . You have to open lights of a room , television , ac , oven and etc. 
What you can do is that you can
1)First get up switch on the light.
2)Then switch on the tv , look for the remote open it.
3)Go to your kitchen switch on your oven.
4)Search for ac remote and then you have to press the button.

That means you have to do the task in 4 steps. But as I said you are lazy ,  you don't like these many steps . So you go out in a market and purchase a 'MAGICAL REMOTE'.

Now , the purpose of this remote is that , once you press on button of this remote , your ac , tv , lights , oven etc. is switched on automatically. 
You did not take above mentioned 4 steps . Just one click and your work is done.

This is basically how Facade pattern works.

Take example of goibibo or makemytrip.

You book a package and you just select a package and on the backend 
1) your flight
2) hotels rooms
3) cabs
4) tourist spots

All are booked by clicking on the package. This is facade pattern.

References :
http://techytuts.in/facade-design-pattern-with-real-world-example-booking-system/

Keep Learning , Keep Growing and Keep Reading.

Thanks alot.

-Anurag Srivastava

Sunday, 1 April 2018

Testing is an Art

Introduction

Imagine one day there is a guest who is about to come at your home for dinner.

You cooked the dinner and you did not taste it or check it whether it is tasty or not and when your guest arrives at your home , you serve him the food.

The food taste was bad and salt was too much in the food , the guest was not able to finish the food and he felt bad about the food which you served him. And he went back to his home angrily.
Next time if you call him to your house he won't be pleased to come.

Same was goes the testing, if you provide your client with a product which developed properly but testing was not done nicely , your client will also become unhappy and will be angry with you or even can sue you for this.

So, comes the big question What is testing ,How it is done and How to approach a testing problem?

What is Testing?

To be very honest , Testing is an art and Tester is an artist or you can say finding bugs in an is a magic and Tester who finds it is a magician.

Testing is very important part of any kind of development of product be it software , hardware or anything.
eg. You need to test your app before uploading on playstore.
      You need to test the building walls when the construction is taking place and also when it is done.
      You need to test the medicines and injections before giving it to the patient.

All of the above mentioned things can fails and result to abnormality if the testing is not done properly.

How Testing is done?

There are multiple ways of doing testing  which ranges from manual to automated , black box to white box testing.


1)Manual Testing or Black Box testing : 

Just testing something for its functionality is called Manual testing where you don't check the logic or anything , you just check whether the functional behaviour or output of the testing product is correct or not.
Suppose you have to test a product in which when you give that product any image , it transforms it into black and white image.

The manual test case for such a product will be just you are giving it an image manually and checking whether obtained image is black and white  or not.

If it is then this test case is passed else this test case will fail,

2)Automation testing : 

Just automating the test cases where manual effort required is very less , where you can write some test scripts  which will run and perform the test cases.
Now for the above mentioned product , you write some scripts which by itself takes the image from a folder and uploads and somehow check for the grey-scale content will be automation script for it.

Tools/Technologies for doing automation testing : 
a)Appium
b)UI Automator
c)Selenium and etc.

3)Unit Testing or White Box testing : 

Testing something on code level is called White box testing where you  check the logic.
The tester has the knowledge of the internals of a system and knows how the system is implemented. The tester uses this knowledge to develop test cases that will examine the control flow, information flow, data flow, exception and error handling as well as coding practices of the system.
The unit test case for the above product can be , checking for the functions at the code level whether it is returning correct value or not.

eg. For doing calculator unit testing you will send 2+2 to function and check whether the function is returning 4 or not.

How to approach a testing problem?

Many a times in interview or in general discussion you could be ask how will you test Product X then how will you tell or how you should proceed.

It is quite simple go step by step.
Like if I say you have to give a feedback or you have to test a food , then what will you do ?
I guess you will do following ?
1)Check for the look.
2)Check for the smell.
3)Check for the oil quantity.
4)Check for taste.
5)Check whether it is hot or cold etc.

Same way you can approach a testing question of any product.

Every Product comprises of following properties :
1)U.I. or look
2)Functionality
3)Reliability
4)Performance(stress , boundary value analysis)

You have to classify your product on the basis of above mentioned 5 points and you can write test cases.

Below I am providing you one example .

Test cases for Bluetooth speaker (eg. JBL)

1)UI or look
a)Check whether JBL is printed on it or not and there should be no spelling mistake.
b)Check whether the name on scratching does not go away.
c)Check whether the color lets say black if proper or not.
d)There should be no stain on the speaker
e)Check for the shape whether it is of black color or not.

2)Functionality
a)Check whether is getting easily connected with device or laptop or not.
b)Check whether the song is played nicely or not
c)Check whether there is no sync issue.
d)Check whether it is easily getting disconnected or not.
e)Check whether on clicking next button pause button play button all are working or not.
f)Check whether you can receive call and whether the call is successful not.
g)Check whether it can get connected to multiple devices or not.

3)Reliability
a)Check whether the material used in it is harmful or not.
b)Check whether it does not have sharp edges.
c)Check whether it does not get short circuit.

4)Performance
a)Stress/Negative test case
i)Keep on changing the tracks or continuous click on next and check whether song displayed on phone and played on the speaker is same or not.
ii)Keep on connecting and disconnecting and check whether it is behaving properly or not.
iii)Take it to a very hot temperature  and check whether it does not melt.
iv)Make it fall from small height and check whether it does not fail.

b)Boundary value
i)Check for the range in which it is played , take to the edge and check whether it is working or not or it is not working in within range also.
ii)Check for the mentioned duration that whether on full charging speaker if working for that many time or not.
iii)Check whether on keeping idle in how many time it turns off.



Above is just few testing steps for Bluetooth speaker , it can be many more as Testing is a never ending process.

You can think of your own.

But one thing I can guarantee you that if you classify your products properly then you can write its test cases very well and this can help you in any discussion or interview just go step by step.

NOTE :

As I told you Testing is a never ending process so you can classify your product in many more parts , not only in above mentioned parts.

The above mentioned parts were just for your understanding.

That's it for today guys . Hope you liked it.
Please let me know if anything is wrong , missing or not clear.

Next time I will be covering types of testing and few more test cases for few more products that can help you.

Keep reading.
Keep learning.

Thanks a lot for reading.
Stay Connected.

-Anurag Srivastava

Monday, 5 March 2018

Java can be mischievous

Umm all JAVA lover would have come to a point where they would have found some crazy stuffs in JAVA.

This article covers few of them. Let's start point by point.

1)Ever heard 2+2=5 ??

  Well Java can defy simple mathematics. What we have been taught at school can be easily faked by     java.
   Yes with the help of  Java you can indeed prove 2+2=5.
   But how ?
   Well the ans is simple with the help of Reflection (most of the mischievous things in Java are             basically done by Reflection).

   Let's look up the code for it .?

  import java.lang.reflect.Field;


  public class ByteEg {

public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub

  Class cache = Integer.class.getDeclaredClasses()[0];
        Field c = cache.getDeclaredField("cache");
        c.setAccessible(true);
        Integer[] array = (Integer[]) c.get(cache);
        array[132] = array[133];

        System.out.printf("%d",2 + 2);

}

  }

   What happened actually?
  "array[132] = array[133]" , this is the most important line. This is where magic happens.
   First understand cache concept in java . In java for int , a cache array is created from value -128 to    127.

  This means cache[0]=-128 ,  cache[1]=-127 ... ... ......cache[128]=0

  Thus , cache[132] is actually int 4. And what we are doing here cache[132]=cache[133].
  This means the value 4 which is at 132 is replaced by value which is at  index 133 (i.e. 5).

  Thus if we wanted to prove 3+3 = 8;
  We have to change cache value of 6 and set it to 8.
   cache index for 6 is 134.
   array[134] = array[136]
 
   This will give result as 8.

   A generic code will be something like this.
   int sum=num1+num2;
   array[132+sum]=array[132+sum+shift]

  eg, 2+2=6 , here sum is 4 and shift is (6-4)=2
*******************************************************************************

2)Does X/0 in java always give Divide by zero exception??

   Well if your answer is Yes for this question , then my dear friend you got it wrong .
 
   Try something like this in java
   System.out.println(5/0.0)
   
   For the above case there will be no runtime exception . You will get a proper output.

    And the output is "Infinity" printed on console.
    
    In case of double value java has a predefined value as Infinity.

  Also you can try ,
   System.out.println(0/0.0)

  The output will be "NaN"  
*********************************************************************************

3)Comments that can actually execute :


   This can be shocking as many will be thinking how on Earth this can happen that comment can           execute.

  For those people I have an example here ?
  
  public class Testing {
    public static void main(String[] args)
     {
         // the line below this gives an output
         // \u000d System.out.println("comment executed");
      }
  }

 Output for the above example 
  comment executed.

 Actually what happen here is that, the above code got changed into 
 public class Testing {
    public static void main(String[] args)
    {
        // the line below this gives an output
        // \u000d
        System.out.println("comment executed");
    }
}

The reason for this is that the Java compiler parses the unicode character \u000d as a new line and gets transformed into above code.

*************************************************************************************************************

Hope you would have learn new things. In future I will share more on Java interesting facts.

Till then you can try to solve the following questions?

1) int a=5;
     if(a<10||a/0==0)
     {
     System.out.println("Hi");
    }
    else
    {
    System.out.println("Hello");
    }

a)Hi
b)Hello
c)Runtime Exception
d)None of these

*********************************************************************************

2)  if(a>10&&a/0==0)
     {
     System.out.println("Hi");
    }
    else
    {
    System.out.println("Hello");
    }

a)Hi
b)Hello
c)Runtime Exception
d)None of these

*****************************************************************************

3) class A
     {
    private void function()
    {
   System.out.println("Inside A");
    }
   }

  class B extends A
{
protected void function()
{
System.out.println("Inside B");
}
}

class C
{
public static void main(String args[])
{
A ob = new B();
ob.function();
}
}

Predict what will happen on running the above code and why ?

*******************************************************************************

Thanks a lot for reading.
Stay Connected.