Urban pro

View My Profile

Wednesday, September 26, 2012

What is GOD OBJECT and RAVIOLI CODE ?

Hi friends,

The other day , i was going through the basic concepts of OOPS(I am a bit rusty , i fear). It's at that point of time, that i came across two mind boggling terms : GOD object and RAVIOLI code. I will try to emulate here what i have understood .

                                                            THE GOD OBJECT

                                                     

Normally :

The basic idea behind object-oriented programming is that a big problem is broken down into several smaller problems (a divide and conquer strategy) . Then, solutions are created for each of them. Once the small problems have been solved, the big problem as a whole is automatically solved. Therefore, there is only one object about which an object needs to know everything: itself. Likewise, there is only one set of problems an object needs to solve: its own.

The all knowing, all encompassing object : 

In object-oriented programming, a god object is an object that knows too much or does too much. Here, most of a program's overall functionality is coded into a single "all-knowing" object, which maintains most of the information about the entire program and provides most of the methods for manipulating this data. Because this object holds so much data and requires so many methods, its role in the program becomes god-like (all-encompassing). Instead of program objects communicating amongst themselves directly, the other objects within the program rely on the god object for most of their information and interaction.

An Example :

Let us consider a banking scenario. Let us consider, K is customer of the bank ABC . K has a savings account. Subsequently, he has an account number. He can withdraw and deposit and can view his yearly statements. Also, he has a credit card  CC  and a debit card DC. 

So a stray piece of code will look like this :
if(user is customer)
{
acctNumber = getCustmerAccountNumber();
amountWithdrawn = getAmountWithdrawn();
depositMade = getDepositMade();
yearlyStatements = getYearlyStatements();
cerditCardNumber = getCreditCardNumber();
debitCardNumber = getDebitCardNumber();
creditExpense = getCreditExpense();
debitExpense = getDebitExpense();
creditStatement = getCreditStatement();
debitStatement = getDebitStatement();
}

if(customer is highNetWorth)
{
customer.setCustmerAccountNumber(acctNumber);
customer. setAmountWithdrawn ( amountWithdrawn);
customer. setDepositMade ( depositMade);
customer. setYearlyStatements ( yearlyStatements);
customer. setCreditCardNumber ( cerditCardNumber);
customer. setDebitCardNumber ( debitCardNumber);
customer. setCreditExpense ( creditExpense );
customer. setDebitExpense ( debitExpense);
customer. setCreditStatement (  creditStatement);
customer. setDebitStatement (  debitStatement);
}

So here , the object customer becomes a GOD object. Any subsequent change in some part of the existing structure, will require a change here in the customer object as it contains every item related to the user's account .

Advantage of this approach :

In micro controllers, performance efficiency and control are two very important aspects. Here, the use of this technique is very frequently seen.

Disadvantage of this approach :

This object is referenced almost every time within the project hierarchy. Hence, maintainability becomes a problem .

                                                          RAVIOLI CODE 

                                                         

It looks delicious :

Yes, we all know it is delicious. But get a grip on yourself and look at it closely. What do you see ? Can you see what is inside the outer covering ? It's very difficult . isn't it ? Well, that's encapsulation(hiding the contents).

What is this ?

This is the opposite of the GOD object.

Ravioli code consists of very small and  loosely coupled code fragments. The term stems from the analogy of ravioli (small pasta pouches containing cheese, meat, or vegetables) to modules (which ideally are encapsulated(hidden), consisting of both code and data).

Advantage:

Places where we want to have a loosely coupled code structure(which can be changed without butting head against the wall), this is very useful.

Disadvantage :

Too much separation and hiding of the code(encapsulation) makes maintenance very difficult .

Well friends, that's it for today. Hope you found it informative.

Goodbye.

Monday, September 24, 2012

STEPPING INTO PYTHON - CHAPTER 3 (Part 1)

All right i am back again with a little bit of a dose of python . Let us start looking  into loops and conditions from today. Since this is a vital and integral part of any programming language , this chapter will be split-ted in smaller sub chapters to provide the unassuming  user a better understanding of how loops , conditions and decisions work in python .

YOU  GOTTA MAKE THE CORRECT DECISIONS IN LIFE :

Some great guy correctly said what i mentioned above . Decisions are very important in life and they have at least the same importance in the programming world,  if not more .

The If  condition :

Let's see the syntax first :
In python , one can comment using : hash operator(#).


kNumeric = int(raw_input("Please enter an integer: ")) # kNumeric is the variable chosen here . It's upto you. 
if kNumeric < 0:
     kNumeric = 0
     print 'Negative changed to zero'
elif kNumeric == 0:
     print 'Zero'
elif kNumeric == 1:
     print 'Single'
else:
    print 'More'

O/P : If i input here 42 , i get the output as More .

Here the key word elif stands for else if and is very widely used to get rid of repetitive indentation . However, the else part is optional here . This conditional flow is a kind of a substitute for switch and case statements found in other languages .

The  for loop :

The for statement in Python is different from what we may be used to in other languages. Rather than always giving the user the ability to define both the iteration step and halting condition, Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

For example in Java :



In Python :

# Let us find out length of individual items of a list
kList = ['item1', 'item2', 'item3']
for k in kList:
     print k,"-->", len(k)

O/P : 
item1 --> 5
item2 --> 5
item3 --> 5

Question : One question that is being repeatedly asked in interviews is : how can you modify a list on the fly and insert an element of the list in another location of the list depending on a condition given ?

Answer : The answer is slice copy . Let's see how to do it .


kList = ['item11', 'item2', 'item3333333']
for k in kList:
     print k,"-->", len(k)
for k in kList [:]: # in this way we can make a slice copy of the entire list on
                         # the fly
    if len(k) > 9: kList.insert(0, k)
print "Now the first element is ::",kList[0]

O/P :

item11 --> 6
item2 --> 5
item3333333 --> 11
Now the first element is :: item3333333

That's it for today friends . Next time we will have a look in the next sub chapter which will contain many other loops and conditions and constructs . Have a good day .



Saturday, September 22, 2012

My webpage

Hi friends,

Just launched my web page today to be  in more contact with you all. It's  not much but as time will fly, it will come up . You guys can personally reach me here :

http://kunalbhowmick.page.tl

See you next time .

Friday, September 21, 2012

Why building IOS apps if you don't have an i phone and a MAC is a bad idea ?

Hi friends,

I am sure many of us has come across this Question but we do not know what the answer to it is . I am not going to explain about this to you in a lot of detail right now(I will give detailed explanations later) . Rather I wanted to share with you this thread, which does shed some light in this dark area . Find the link to the thread below : :

http://programmers.stackexchange.com/questions/165644/is-there-a-way-to-publish-ios-app-from-windows-linux

I will come up with a much detailed explanation later .

Adios .

Monday, September 17, 2012

DEBUGGING

Hi guys/gals,

Back to you after a long hiatus . Sorry about that, i was so busy, could not even pick up the laptop and open face book. However, today i took some time out to speak about something perennially important, namely : DEBUGGING .

Let me start by telling you that in the context of what i am speaking , there are two kinds of debuggers :

  1. People who like to use the all important SYSTEM.OUT.PRINTLN().
  2. People who like to use the inbuilt java debugger and the magical breakpoints .
Now i have been using RAD for sometime and it contains WAS6.1 to publish and deploy my web application . Let me come to my main point here. Since i was fully new to the application and i had to first know, how the code flows and so on and so forth, i tried using the java debugger . Now the problem here is that, coupled with WAS6.1 the debugger works really badly. It at times used to stop when i had not reached the break point which was supposed to show me the values for a particular variable, sometimes the WAS server used to stop by itself and would not start for 15-20 mins. All this heartache and i needed to find out many things very quickly. So, what did i do ? I went back to our very special friend system.out.println().

Now kindly bear in mind that i am not saying that you must not use java debugger. It's just that sometimes you may be short of patience and time to effectively  reap all the benefits of it. All those times, i suggest that you start using the sysout().It acts both as a tracer and debugger for the application.

You can , however use the log() method as well .One just  needs to import log4j.jar and log4j.xml and properties file where the logfile name and path is specified and voila you can start logging your information .

A little demonstration will not hurt, huh :

// In you base class :
public static log(String s) {
// Point to the appropriate trace level :
public Static DEBUG_LEVEL = "ERROR";
switch (DEBUG_LEVEL) {
case: ERROR
System.out.println(s);
case : TRACE
writeToYourFile(s);
case : PRODUCTION
prod(s);
}
}

Hope you liked it. Will be back next time, with something else . Keep in touch to see my next upcoming tutorial on Objective C  and the upcoming chapters on stepping into python and JS-OOPS .