Search This Blog

Thursday, August 30, 2012

ROR day ??

I implemented an authenticating system for my sample rails project. Learnt more about controllers . Will update more info in future !

Wednesday, August 29, 2012

ROR - things i think u must know!

database.yml
Although rails follows 'convention over configuration' you need to configure things like database before working on a project. database.yml is the database configuration file for any Ruby on Rails project. It is present under the config directory of your project root folder.
So what is yml ? its a file extension of YAML files.
YAML --> YAML ain't markup language. Yes , it is a recursive acronym :)
However when it was being developed YAML meant yet another markup language. It was then changed to current name to stress the importance of data rather than markup.
In nutshell its just a human readable data serialization format. NOTE: it uses strict spacing. I missed a space and managed to fix the error only after an hour !

RESTful way!
In rails we do applications that are RESTful.
A resource is generally associated with entities which can be created,read,updated,deleted ( CRUD operations ).
I will explain what is RESTful with an example,
suppose you have a website (www.vehicles.com) that deals about different vehicles.
Now,
www.vehicles.com/lorry/wheels/2
This is RESTful. It clearly means you are accessing the 2nd wheel of a lorry.
the same can be implemented as,
www.vehicles.com/getvehicleaslorry/parts/getpart?id=2
It looks horrible and it is certainly not RESTful.

In Rails RESTful way comes naturally. You tend to have a controller for a resource and actions like create,destroy follow.

Models and ActiveRecords.
Every model inherits from ActiveRecord::Base.
Understand that ActiveRecord is a module. Its like a namespace. Base is a class inside it.
The ActiveRecord::Base class gives methods to access the records/fields of the table corresponding to that model using objects of that model itself.
For example, we have a users table which has the field name.
Now in the model User.rb,
user=User.new(:name=>"raj") 
will create an object user.
user.save
will save the object as a record in the users table.
Instead of two separate statements you can do
User.create(:name => "raj")
It will save create a record in user table having name "raj"



Tuesday, August 28, 2012

ROR falling deeper , hoping i could climb up

I feel am learning something about Rails continuously. But at the end of the day the clarity is missing. I would now tell one thing which i understood , which i feel everyone must know before learning rails.
The MVC architecture. Rails allows you to build applications that follow MVC architecture.
M--> Model --> contains business logic and database interactions
V--> View --> contains code for front end
C--> Controller --> contains the flow of the application. Links view and models.

So what happens when a HTTP request arrives??
  • First it hits the routes.rb file where we have our routes specified
  • After reaching routes it knows which action of which controller to hit. It hits that action.
  • The controller then interacts with model if necessary and then view corresponding to that action is hit. View is rendered and displayed to the user !
More about rails in future posts !

Friday, August 24, 2012

ROR day 2

The amazing thing i was mentioning in last post is "Scaffolding" . Not just scaffolding, but how it adjusts so nicely .
I used,

rails generate scaffold stickynotes

what Rails did is create Stickynote.rb as model and stickynotes_controller.rb as controller. I was wondering how did it automatically made the model as singular. I decided to experiment and did this,

rails generate scaffold bus
and i half expected it to create a model called bu. But it made the model as bus.rb and the controller as buses_controller.rb. Great work pluralizing!!

It also underlines one important point. In rails its all about conventions. 
Into my second day in learning rails i feel the learning curve is pretty steep. And the reason for that according to me is the stress on conventions. There are lots and lots of conventions adopted and to be followed. 
Will post more about concepts in posts to come !

Thursday, August 23, 2012

ROR introduction

Today was consumed in installing rails and making it to work. Was amazed by the power of Rails framework ! Will tell the reason for my amazement tomorrow. Stay in touch.

Friday, August 17, 2012

Ruby final day

No sessions today. Just more and more reading about Ruby's syntax and few concepts. Went through tutorials here, http://ruby.learncodethehardway.org and finished 41 exercises.
Will post more about ruby if i come across anything interesting and worth mentioning.
See u in next post! 

Thursday, August 16, 2012

Ruby -metaprogramming,send,self and stuff !

Function overriding and overloading:


Function overloading
I will tell how the interpreter sees this code,
  • inside the class we have a function called 'f' defined in line 2
  • now the same function is defined again at line 5. So the previous definition of function 'f' gets overridden completely.. in the sense the previous definition is lost !! Note that this happens even if the number of parameters vary! In C both functions will exist.
  • In line 8 the function 'f' is redefined once again . So obviously the previous definition (in line 5) is forgotten or lost. 
  • So when you call f from your main program, the 'f' defined in line 8 gets executed.
Wondering how a.f() in line 15 got executed??
That's because 'f' in line 8 has a default parameter . When you have a default parameter you can call the function even without passing any value for that parameter. However if you specify some value the value you sent will override the default value.
so line 15 produces "raj" as output and line 16 produces "kumar" as output.

Ruby doesn't support function overloading like C++. However Ruby has the following implementation which is similar to function overloading,
def fun(*args)
end
this function can be called using any number of arguments and any type of arguments.
args is an array which holds these arguments. In a way this is function overloading.
Function overriding happens the same way as in other languages like C++ or Java.

Self object explained:


self object in ruby
self is always unique at a given point of program execution. It holds the current object of execution. Understanding self object clearly will give you deeper insight on ruby itself. Without sound knowledge about self it is difficult to understand how a completely object oriented language like Ruby works. self is analogous to this keyword in C++. I wrote above program to see the value of self at various places in a simple ruby program.


Output of self object demo
From this output we can see that, inside a class the self object can be a the class itself or the particular instance depending upon where it is used.
Inside an instance method self is the instance object
Inside class method or body of class it is the class itself(here A)
In the main program and methods outside classes, self is main object.

Send method in Ruby: 
send method and opening a class
send is a way of calling methods. It takes the method name as the first parameter and the method's arguments as remaining parameters.
In the above example, function 'f' is called using send.
Tip: self.class.send can be used to call the class method from an instance!


Output of send demo

Meta programming:


method_missing --> a function which gets called when a method is not available in the class. For this to work for class methods define method missing as class method . 
In the code i have shown above i have defined method_missing both as class method(using self keyword) and instance method.
In the instance method, method_missing , dynamically new methods are created!
In this example,
  • mul_3(4) in line 16 will pass the control to method_missing in line 5
  • now a new lambda is created which does the multiplication
  • define_method is called using send in line 10, which defines a method of name m and block l.
  • finally the created method is called in line 11 using send. a is the argument passed.
Note: define_method is an inbuilt method which is used to dynamically create methods. It can be used as class method only and not as instance method

Tuesday, August 14, 2012

Ruby deeper insight

Had a busy day exploring ruby. All my explorations in next post! 

Monday, August 13, 2012

Ruby - more concepts


Module:
Simple module implementation
Here both the module and main program is in same file. In almost all cases they won't be !! Modules are like libraries. You will be having it in separate file and including it whenever you require that functionality. To include a module you need to use 'require' keyword. 
require filename_with_correct_path
where filename is the file which contains the module you are including.

Arrays and Hashes:


Arrays,hashes and looping through them
Arrays -- same as arrays in other programming languages. Heterogeneous in nature.
Hash -- Key value pair. Key and values are separate arrays! Its also heterogeneous. 
The above program shows various ways of accessing contents of arrays and hashes. Output is given below.


Output of arrays hashes program
Looping techniques:
Looping techniques
For C programmers, Ruby's looping techniques may seem complex. But its not the fact. They are very simple and powerful once you get hold of it. Above program shows you various simple looping techniques applied commonly.

Collection,mapping and array initialization:

Collect,map and arrays
Collect and map are very similar, except the fact that collect returns an array while map returns enumerator. Initializing an array is also demonstrated and it can be painless unlike in C or C++. 
Output of collect,map
Class and inheritance:
Class,inheritance
@varname - varname is a instance variable => accessible through instance of class only
@@varname - varname is a class variable => accessible through class only using scope resolution operator
unless you specify accessor or write methods of your own inside class the instance variables remain inaccessible outside the class. 
class A < B =>class A is inheriting from class B => B is superclass of A
super calls the method of superclass having the same name as the method inside which super is used.
Output of class,inheritance
Blocks:
Simple code demonstrating the use of blocks

Output of the above block program

Proc and lambda:


Sample program demonstrating how proc and lambda work
The above program helps in understanding how to use proc and lambda. Using it in optimal way depends on programming logic of the user.
There are 2 main differences between lambda and proc. They are,
  1. Proc acts as inline function. So a return statement inside proc would mean returning from the calling function itself.A return inside lambda however returns control to the calling function
  2. A proc can work without throwing any error even if the number of arguments doesn't match. A lambda will throw an error saying wrong number of arguments.
In the code i have given above , remove the commented lines and try understanding the errors thrown for better understanding.

Friday, August 10, 2012

Ruby day-2

I need to make clear the difference between dynamically typed and a dynamic language.
Both are different. A dynamically typed language allows operations like adding number to a string etc... whereas a dynamic language allows you to add functionality that static languages allows you to add only before compile time.

Today i saw more tutorials on Ruby. Learnt about file operations like opening,reading,renaming,closing files in Ruby. Syntax is absolutely simple and easy when compared to other languages like Java. Learnt a little bit about modules too.
Will be posting more soon.

  

Thursday, August 9, 2012

Ruby Introduction

Ruby is an object oriented , dynamic programming language which is designed in a way to use it easily. It's created with an aim of making programmers more productive by allowing them to concentrate on logic rather than implementation and syntax.
By object oriented i mean everything is object in Ruby. Even primitive data types of other languages like numbers are objects in ruby.
For example, you can actually do,
5.to_s
It returns "5" which is a string. 'to_s' is a method which acts on the number(object) 5 and returns a string!!
You can verify what am saying by typing the following command,
5.class
It returns the class name of 5, which is 'Fixnum'
"hello".class would return class name as 'String'

By the way did i tell you to use irb(interactive ruby shell) for trying out these commands? 
It's very helpful in exploring ruby as a beginner.
There are tons of websites providing you tutorials on Ruby.

Most programmers are strong only with the syntax of C. Sadly , Ruby's syntax is very different from C's. Today i was just trying out variable initialization, getting and setting values, arrays, loops, conditional statements in Ruby. It will take some time for getting used to the syntax. I will work with some interesting program and keep posting more about Ruby in coming days.
See you in next post! 

Tuesday, August 7, 2012

Javascript more concepts

Javascript engines:
    I hope you have read my post about browser engines earlier. Modern browsers have separate javascript engine(for rendering Javascript)  and rendering engine (for rendering html content).
Latest Javascript engines are,

  • Carakan -- used with Presto , developed by Opera for its browser
  • V8 -- used with webkit in Chrome, developed by Google
  • Nitro -- used with webkit in Safari, developed by webkit developers and apple
  • SpiderMonkey --used with Gecko in Firefox developed by Mozilla

IE has always lagged behind in its performance when compared with other browsers. It had no separate JS engine before release of IE9 which uses a JS engine called Chakra.
It is to be noted that each engine’s implementation varies and it directly affects the speed and performance of the browser. Read more about browser wars. They are very interesting.

Objects in Javascript:
    You might have used arrays,strings,date in Javascript. Those are built in javascript objects. Javascript supports object oriented programming. But it is somewhat different from other object oriented programming languages. Here i will explain the reason with a simple program, see the picture below for code.

Code demonstrating basic object usage

  • The function Human is like a class
  • Parameters passed to the function are like parameters to constructors.
  • name,age,sex are data members of the class
  • show is a method of the class

Note:

  • Human is an object and human1, human2 etc are instances of the object
  • In the above example we can declare an instance of an object even without specifying parameters. For example, human3=new Human(); Now name,age,sex will be undefined for this instance.
  • You need to associate a method to an object (Here look at the code, this.show=show )
  • You can access datamembers and menmber functions using dot operator (for example, human1.age=40 )
  • The keyword “this” refers to the owner of the particular object/function.

Timers:
    Events can be executed based on timing in javascript. This is a core functionality of javascript.
we have two functions helping us out,

  1. setInterval(function,time in milliseconds)
  2. setTimeout(function,time in milliseconds)
first one executes the given function after every time interval you have given.
second one executes the given function only ONCE after the time interval you have specified.
Stopping a timed event is pretty simple.
    clearInterval(timerVariable)
    clearTimeout(timerVariable)
timerVariable is the variable in which return value of setInterval or setTimeout is stored.
For example,

myTimer=setInterval(function{ alert(“hello”);} , 1000); //alerts ‘hello’ every second
clearInterval(myTimer); //stops alerting

JS libraries:
First i will list some popular JS libraries

  • jquery (the most popular one !!)
  • prototype
  • script.aculo.us
  • MooTools
  • underscore.js
  • backbone.js

A JS library has collection of functions (written in javascript.. understand that the libraries are nothing but JS files!!) which help you achieve some complex task(when you code it yourself in JS..) easily.

jquery has many functions that allow you to decorate your page or make it more fancy
backbone.js allows you to implement mvc architecture in large javascript files
backbone.js uses underscore.js
underscore.js has many functions which allows you to write functional code in JS very easily.


Usage of these libraries should be avoided whenever possible, because they make your code slow.. you can’t tweak little things and i personally think it makes you feel that you are not in control of your own code.
Always remember that native javascript functions can achieve whatever you have done using some JS library.
See you in next blog!

Monday, August 6, 2012

Javascript solutions to day2's questions

Today i would discuss about the solutions i came up with for the 3 problems i discussed in previous blog,
Crashing a browser:
The first idea i got is to write a simple recursion and crash the browser. This technique works great for C programs. But it never worked for me in crashing browsers. May be the javascript engines were too clever to avoid crashing!
Then i tried infinite loop .. which worked well in crashing the browser. In case of firefox the browser crashed, whereas in chrome only the tab running the code crashed. Other tabs were working fine. This is because chrome uses different threads for each tab.
However both firefox and chrome gave me the option to stop the script running as it is crashing the browser !!

Pattern:
Animation using sprites ! I have coded that already and that seemed a perfect answer for pattern problem. In addition to that i tried working with ‘canvas’ which is a new functionality added in html5.
It allows you to draw beautiful patterns.
What i decided to do was allow the user to draw his own pattern in the canvas.I used event handlers to find the mouse position and then draw line tracing the pointer. Event handling is an area which has many cross browser compatibility issues. It took some time to find suitable functions that worked well both in chrome and firefox.
Finally i put the animation and this canvas on the same page and did little coding which makes the animation to stop whenever the user draws and resumes when user stops drawing and comes out of canvas.

My own programming language:
It turned out to be my most favorite question after i finished it. I decided to create a language similar to assembly language. Remember x86 language? which uses instructions like mov,add,sub etc...
I used js object which allows you to store key-value pairs for storing variables and their values.
I consciously put effort to improve code-reusability and modularity. But then realised that without conscious efforts too, i would have done the same refactoring. I still firmly believe designing code is all about the programmers instincts. Good programmers do it in a better way !
will upload codes in a separate post.
See you in next post !

Solutions (programming language)


File: proglang.html

<!DOCTYPE html>
<html>
<head>
<title>My programming language</title>
<link rel="stylesheet" type="text/css" href="proglangstyle.css"></link>
<script type="text/javascript" src="proglangscript.js"></script>
</head>
<body>
<div id="codearea">
<h3>Enter your code here</h3><br>
<textarea id="code" rows=10></textarea>
</div>
<div id="outputarea">
<h3>Output</h3><br>
<textarea id="output" rows=10></textarea>
</div>
<div>
<input type=button id="run" value="Compile and run"></input>
<input type=button id="check" value="check variable values"></input>
<input type=button id="clear" value="Clear all"></input>
</div>
<div id="rules">
<h4>Syntax rules</h4>
<p>
More like assembley language.<br>
Data types<br>
number --> integer<br>
text --> single word surrounded by single quotes<br>
<br>
Note:<br>
Variables automatically get declared if not declared already when used in destination field (eg: add a,10 //creates a variable a and stores 10 in it)<br>
Spacing is strict. only one space in a statement and it is placed between keyword and arguments.<br>
adding texts leads to concatenation<br>
<br>
Instructions:<br>
mov dest,src //dest=src<br>
mov variable,number<br>
mov variable,text<br>
mov variable,variable<br>
<br>
add dest,src //dest=dest+src<br>
add variable,number<br>
add variable,text<br>
add variable,variable<br>
<br>
similarly sub,mul,div works<br>
<br>
show varlist<br>
varlist --> variables separated by commas (eg:show a,b,c)<br>
It prints values of the variables listed<br>
</p>
</div>
<div id="sampleprogram">
<h4>Sample Program</h4>
<p>
mov a,10<br>
add a,20<br>
mov name,'raj'<br>
add name,'kumar'<br>
mov b,2<br>
mul c,8<br>
div c,b<br>
show a,b,c,name<br>
</p>
</div>
</body>
</html>

File: proglangscript.js


window.onload=initAll;
var vartable;
var output;

function initAll()
{
initialize();
document.getElementById("run").onclick=getCode;
document.getElementById("check").onclick=checkVarValue;
document.getElementById("clear").onclick=clearAll;
}

function initialize()
{
vartable={};
output="";
}

function clearAll()
{
document.getElementById("code").value="";
document.getElementById("output").value="";
}

function checkVarValue()
{
var varname=prompt("Enter variable name:","");
if(varname)
alert(vartable[varname]);
}

function getString(str) //returns string after removing quotes
{
return str.substr(1,str.length-2);
}

function whatIs(x) //returns whether x is a number or variable or text
{
var textpattern=new RegExp("\'.*\'");
if(isNaN(x))
{
if(vartable[x])
return "var";
else if(textpattern.test(x))
return "text";
}
else
return "num";
}

function move(movstmt) //code for processing move statement
{
var param=movstmt.split(",");
var dest=param[0];
var src=param[1];
switch(whatIs(src))
{
case "num":
vartable[dest]=parseInt(src);
break;
case "text":
vartable[dest]=getString(src);
break;
case "var":
vartable[dest]=vartable[src];
break;
}
}

function show(showstmt) //prints variables given
{
var variables=showstmt.split(",");
for(var i=0;i<variables.length;i++)
{
output+=variables[i]+" : "+vartable[variables[i]]+"\n";
}
document.getElementById("output").value=output;
}

function operate(stmt,operator) //code for processing div statement
{
var param=stmt.split(",");
var dest=param[0];
var src=param[1];
switch(whatIs(src))
{
case "num": //source is a number
if(vartable[dest]) //checking if source exists
{
if(isNaN(vartable[dest]))
{
alert("cannot handle number operating on text");
break;
}
switch(operator)
{
case '+':
vartable[dest]+=parseInt(src);
break;
case '-':
vartable[dest]-=parseInt(src);
break;
case '*':
vartable[dest]*=parseInt(src);
break;
case '/':
vartable[dest]/=parseInt(src);
break;
}
}
else
{
vartable[dest]=parseInt(src);
}
break;

case "text":
if(operator=='+')
{
if(vartable[dest])
{
if(!isNaN(vartable[dest]))
{
alert("cannot add text to number");
break;
}
vartable[dest]+=getString(src);
}
else
{
vartable[dest]=getString(src);
}
}
else
alert("not possible");
break;

case "var":
if(vartable[dest])
{
switch(operator)
{
case '+':
vartable[dest]+=vartable[src];
break;
case '-':
vartable[dest]-=vartable[src];
break;
case '*':
vartable[dest]*=vartable[src];
break;
case '/':
vartable[dest]/=vartable[src];
break;
}
}
else
{
vartable[dest]=vartable[src];
}
break;
}
}

function process(instructions) //main code for processing each statement
{
for(var i=0;i<instructions.length;i++)
{
var stmt=instructions[i].split(" ");
var keyword=stmt[0];
switch(keyword)
{
case "mov":
move(stmt[1]);
break;
case "add":
operate(stmt[1],'+');
break;
case "sub":
operate(stmt[1],'-');
break;
case "mul":
operate(stmt[1],'*');
break;
case "div":
operate(stmt[1],'/');
break;
case "show":
show(stmt[1]);
break;
default:
alert("something wrong with the keyword at line "+(i+1));
}
}
}


function getCode() //gets text from text area and splits them into instructions for processing
{
initialize();
var code=document.getElementById("code").value;
code=code.trim();
var instructions=code.split("\n");
process(instructions);
}


File: proglangstyle.css

body{
background-color:#EEEEFF;
}
#codearea{
float:left;
width:45%;
margin:5px;
}

#outputarea{
float:left;
width:45%;
margin:5px;
}

#rules{
width:45%;
float:left;
margin:5px;
background-color:#EEEFF5;
}

#sampleprogram{
width:45%;
float:left;
margin:5px;
background-color:#EEEEF5;
}
input{
float:left;
width:25%;
margin:3px;
}

textarea{
width:90%;
height:90%
}




Friday, August 3, 2012

JavaScript day 2

In today's session we were given 3 interesting questions. I will share them with you now,
1.Write JavaScript code to crash a browser
2.Create a simple pattern using JavaScript
3.Write your own programming language using JavaScript


The day went in solving these 3 questions. You too try coding answers.
I will share my answers along with concepts used for coming up with the solution in next post.

Thursday, August 2, 2012

Javascript Introduction

Audio blog on JavaScript introduction


The day went away in a jiffy learning basics of javascript. I went through basic and some advanced concepts of javascript in www.w3schools.com . It was fun. 

I would now explain a popular javascript functionality of ‘handling events’ with not-so-popular functionality of html ..’maps’ !


Events -- > actions performed by user like mouse click, keystroke etc...
Here i have done a sample program which uses onmouseover and onmouseout functions of Javascript. 

Sharing screenshots of code and output with you here,
pic1 - html page containing the javascript code as well.
The code is kind of self explanatory.
pic2 - output.. the paragraph content is head because the mouse pointer was hovering over the upper part of the picture. A video would have been better. But this is a very simple program. U can very well code and see the output in your machine.



Pic 1 the html file

Output. Mouse pointer is over head of penguin :P


Will be posting more about JavaScript in coming days.
See you in next blog.

Wednesday, August 1, 2012

TDD and BDD

Audio blog on TDD and BDD


TDD:
TDD-->test driven development
Adhering to tdd means you shouldn’t write a line of code without developing test case for it first !!
For beginners this may be very difficult...sometimes we write functional code without writing test cases. That’s not correct TDD.
It’s a technique where we use unit tests to drive the design of the program or project.
Unit tests ??
    These are very small scripts which send some input to a class or method and verifies if the result is same as the predicted result.
For example , suppose we have a function to concatenate two strings, we test it like,
START TEST
    assert(concatenate(“hello”,”world”),”helloworld”,”expects helloworld as output”);
END TEST
Forget the syntaxes used above, they are invented by me now to explain testing.
There are different testing frameworks available for carrying out testing in different programming languages.
Java - JUnit
C - Check
...

How TDD works? or Motto of TDD..
RED ---> GREEN --> REFACTOR

RED --> Develop a test case and make it fail. ( a test case will obviously fail without even writing the function it is testing)
GREEN --> develop code so that the test case developed in the first step passes.
REFACTOR --> Change code to remove duplication(accept it.. there will be some sort of duplications even in experts code) , improve the design.

It's to be noted that testing is to be done after refactoring to verify everything is working correctly.
This is done for every piece of code that is going to be a part of your project.
Getting the point?
You develop test cases based on requirements and then write code to pass those test cases . This is the essence of TDD.

Why use TDD ??
You may think you are good at designing and then implementing the same. But that may not be the case. May be you are a gifted programmer and designer.. ! but that may not be the case with everyone. TDD makes sure that the code you write is good. You will understand if i explain some advantages of TDD,
Advantages:

  • It helps you in proceeding step by step. The important thing is that it gives you confident that the step you completed is perfectly working and correct. Some programmers tend to code many many lines of code and then wonder where the bugs are !! it's always better to go step by step.
  • It forces decoupling of modules or dependencies. For example , you write a program to find multiplicative inverse modulo of a number. This program in turn needs to calculate gcd of the number and the modulo involve. Normally you tend to write the code to calculate the gcd within the main module itself. So they become coupled. If you need to find gcd of two numbers in some other program you need to copy paste the code. Had you used TDD you would have developed test cases to check whether the gcd algorithm works correctly. Automatically you would have put the gcd algorithm as a separate module. Decoupling happens !!
  • The test cases are like a constant feedback to the developer that the modules are working properly. Again it's for the confidence of the developer. It helps in easily identifying and fixing bugs too.
  • It is like a documentation that never goes out of date unlike the documentation developers write !

BDD:
It stands for behaviour driven development. It involves identifying behaviour of a system and then writing test cases for that behaviour to be satisfied. It combined acceptance testing and unit testing.
People consider BDD as rephrasing the existing good testing process based on TDD. Am not going into BDD any further as i have zero experience in them.

Where TDD may go wrong? or what led to BDD when we have TDD !!
TDD may have design impacts (not always.. happens due to bad programming logic or poor design knowledge of the programmer). Sometimes design may not be clear to the coder. In those cases you need to change the test case itself which may prove very costly.
Lot of time investment is involved to get into the TDD approach. Normally you can see developers skipping test cases for some part of the code or project .

TDD approach by example:
    Let’s assume you are writing a simple 2D game... like mario.
As you are considering game as an example test cases will be innumerable. I will tell only a few here.
When mario gets a mushroom,
    if he is small he must become big
    if he is big then he must get firepower

Now you decide to put this into code, you decide to write a mushroom_got function which gets called when mario gets a mushroom.
so you start by writing test cases.

testcase 1:
input : small mario gets mushroom
result: big mario

test case 2:
input: big mario gets mushroom
result: mario with firepower

Now you begin implementing methods that does the same. You then find that becoming big or getting firepower also needs to be tested.

so you write test case for those like,
after becoming big -- is height of mario larger than his prev height etc..

for implementing this testcase you will write makemariobig as separate function and then use it inside the mushroom_got function.

This is how TDD works. You can understand how it helps in deciding the flow too.

That’s it for today. See you in next post.