Showing posts with label design. Show all posts
Showing posts with label design. Show all posts

Sunday, August 01, 2010

De|Bug|er

We are living some where in starting to mid-point of technology revolution, still a long way to reach ‘apogee’. But the effect of technology is enormous, from fast food ordering system to NASA Space Shuttle orbiter maneuvering controlled by technology.Every-where and  every-one depends on technology in one way or other. Just look back a decade ago, the nightmare we faced during Y2K era, we were thought world going to be standstill. But thanks to ‘Sang-Froid Debuggers’ emerges from various part of world to make our life easy by providing timely solutions. 

Whether we like it or not, since technology is immature, no bible like guide to develop code, no process (if we have one, people tend to hide behind it) the systems we develop comes with some risks. One of the main risk involves are A)Manual intervention to avoid some possible bugs B)code issues hence system acting weird when some conditions met.

The risk (A) can be mitigated easily but what about (B), to make things worse, most of time the system’s knowledge base may not even exist when some bugs appears.In my experience, there were some bugs took 12-18 months to fix it,of course from very legacy code. But i think we have to do somethings creatively to decrease process time.

Here are some steps to become Sang-Froid Debuggers(assume new developer on a old legacy system without any knowledge base around)

1. Code Psychology : 
One of the most and foremost way of finding and fixing is, understanding developer’s psychology in terms of how s/he writes code,his/her thinking pattern and his/her deviation from “standard” way. The best of doing it is by reading code from left to right, for software developers reading code is most productive than documents.

2. Team Enthusiasm : 
Next level to become ‘Sang-Froid Debuggers’ is understanding teams enthusiasm,of course we may not know until we found some design documents or some real time stories from current co-workers. First of all what i mean by team enthusiasm, (please read 2 nd para first 2-3 lines) due to that each team try to develop system different ways. Some of them very enthusiast towards open source. If that's the case, try to understand, how tightly the system integrated with open source components and start documenting each one’s known issues. Based on my experience, most of the “late hard to fix bugs” were from open source components, mainly we don’t know how to use it.

3. Raising Flags
We all want to fix bugs which are assigned to us by only us but sometimes we need help from others. It is better to ask help from others whenever we needed. I knew some horror stories like one guy took 3 months to troubleshoot an issue, he couldn’t but the other guy did it with in 3 hours. It was not because the first guy was bad, second guy experienced the same or similar issue elsewhere.

4. Early detection and prevention
If we did better work on points 1 and 2, smart developers can sense the system vulnerabilities, start documenting it and start enlighten others.Start some brainstorming for prevention of unwanted bugs from legacy system.

Saturday, August 29, 2009

Book Review : Refactoring by Martin Fowler

Brilliant book and must read one for Software engineers and Java developers. Why i emphasis this book? This book would help us to communicate efficiently. When i start reading Gang of 4 book and Head First Design Patterns, i start wondered that "hey, we did that 2 years ago and it should be called "Factory Pattern" etc etc. Giving an industry standard name for an activity is really important and it is the progression of IT industry. Once we are all familiar with naming and concept then our job is very easy to perform and make quality software. Here after i no need to explain moving method from a class to other class because of blah blah reason, just refer Martin Fowler refactoring pattern "Move Method(142)". Through out the book, i was excited to see Martin Fowler captured everything in detail related to refactoring which we facing in our day by day activity.

Nowadays IT industry code development become static, thanks to current economic downturn and baby boomer's retirement. But the software usage is exploding year by year. The software which developed during IT boom period early this decade is still in use but not scaling up with demand. The only solution now available is to refactor it and make it scalable. Not all of them afford to write a new software from scratch again thanks to economic downturn.

Refactoring by Martin Fowler gives you feeling that he is directly talking with you as a Java Architecture or a team mate. One main thing of refactor is "pairing teams" and "solid test suite". Why pairing teams is important for refactoring, we have to discuss in detail about what kind of refactoring make lot of sense. Based my experience, we have to move step by step process to achieve solid refactoring and always keep it simple.

This book gives you a in depth knowledge of refatoring and most of it, we already heard from our senior team members and architecture, i mean we already followed it because of our experience. But this book consolidated all of them and give them a name and giving some other techniques to refactoring also. Again a must read...

Friday, May 08, 2009

Twitter Java API

In my opinion, after Apple's iPhone, iPod and Amazon's Kindle, Twitter is greatest innovation of this decade. The simple but powerful innovation allows us to share our thoughts, news, and opinion to 100K plus or even 1 million users within a single click. Amazing doesn't it? How will you share your opinion transparently within 140 chars? The twitter users also has amazing power, people sharing from all over globe by using a single entity is something powerful medium.

I was drawn into Twitter world like a year ago, so far my experience with Twitter is excellent. But every things has it is own drawback, in twitter world there are some a-holes try to deceive us by following us and make us to follow them and then silently unfollow us. I want to develop a small piece of code to find this mischief. But i found an excellent site www.refollow.com to do the same thing efficiently, i recommend all to use the refollow.com to find this rogues. But anyways i did this code to understand various APIs for Twitter. One of the great and fastest Java API for Twitter is from Google but there is no good documentation. Here is step by step guide to integrate with Google java-twitter.

1. Go to http://code.google.com/p/java-twitter/

2. Download Java-Twitter-*-SNAPSHOT.jar. Java-Twitter-*-SNAPSHOT.jar depends on following JARS

commons-beanutils-1.6

commons-codec-1.2

commons-collections-3.1

commons-httpclient

commons-lang-2.4

commons-logging-1.1.1

ezmorph-1.0.6

google-collect-1.0-rc1

httpcore-4.0

httpcore-nio-4.0

joda-time-1.6

json-lib-2.2.3-jdk15

3. Here is the complicated Google's Protocol Buffers. Google uses Protocol Buffers for almost all of its internal RPC protocols and file formats. Our job is to download bunch of C++ code and convert it to Java and jar it.

4. Download protobuf*.zip and protobuf*win32.zip.

5. Unzip protobuf*.zip and proto*win32.zip, and find a proto.exe from protobuf*win32.zip.

6. Copy the file to protobuf/src/ folder.

7. Go to protobuf/java and find README.txt for step by step guide to create protobuf-java-2.0.3.jar file.

8. After adding all the above JAR files, the code development is easy and refer http://java-twitter.googlecode.com/svn/site/apidocs/index.html

9. Make sure to install Java 1.6 and set JAVA_HOME variable correctly.

Here is code to find friendship exists call.

Sunday, June 08, 2008

Stereotypes - Architecture point of view

Today morning i went to an indian store to buy chicken for lunch. One of my ex-colleague called me for a friendly chat. Since i'm in indian store, the context was set as business and india, he told me that recently he visited a suburb where 70% of business owned by a particular people from an indian state. I said yes their main core is business, but we (both of us from south india) are good for project oriented jobs. Is it stereotyping or assumptions?

As per architecture point of view, stereotypes and assumptions are really important to understand/build a new system but with in human systems based on stereotypes and assumptions "forcing" is wrong. My opinion is, a little stereotype, common sense and general knowledge is really required to understand a stranger and to build a good relationship or persuade others. The 3 combination of stereotype, common sense and general knowledge are success weapon to deal with most diversified cultures like india.

For example, couple of weeks ago my wife with couple of other housewives planned to contact a get together. One is from east india, other one is from west india(we haven't meet them earlier). My wife came from prominently beef eating state based on that she suggested a beef dish. She lacked to apply stereotype, common sense and general knowledge here, because i knew western india states banned beef slaughtering , and most of them are sensitive to hindu culture in which eating beef is sin and a simple stereotype that their last name belongs to veg. only community. I was upset with her and i felt some uneasy when we went there for dinner ofcourse with pure veg. dishes.

Tuesday, May 27, 2008

Decrator Pattern

The definition of Decorator pattern (from Gang of 4) "Attach additional responsibilities to an object dynamically. Decorator provide a flexible alternative to sub classing for extending functionalities."

The classic example of decorator pattern is Java I/O package. We used to create a powerful reader by chaining file reader and buffered reader. So file reader got decorated by buffer reader at run time with out extending it.

BufferedReader buff = new BufferedReader(new FileReader("something.txt"));

Again back to our traditional Car example. Assume, Car came out of factory and it is ready for drive but not quite ready for fully operational. Because now Car directly going to a dealer and he/she needs to add their own accessories and some more accessories depends on customer choice. If we think about an object model here, we can clearly understand the decorator.

So Car manufacture has to create an interface ( assume an abstract class ) to satisfy both dealer specific accessories as well as customer specific ones, with out changing or extending car.Now dealer implementing all accessories by extending manufacture interface (abstract). Now dealer decided to create one more abstract class by extending manufacture interface, to give more choice to customers for highly customize the car.

//Manufacture abstract class
public abstract class Accessories {
public void addMoonRoof();
public void fixNumberPlate();
}

//Dealer concrete class
public class DealerAccessories extends Accessories {
public void addMoonRoof() { //add a basic moon roof }
public void fixNumberPlate(); { //fix number plate }
}

//Dealer abstract class for high customization
//allowing customer to choose what moon roof they want, panoramic, 3D etc
public abstract class AccessoriesDecorator extends Accessories {
public abstract void addMoonRoof();
public void fixNumberPlate(); { //fix number plate }
}

//Panoramic view moon roof
public class Panoramic extends AccessoriesDecorator {
public void addMoonRoof() { //add a panoramic moon roof }
public void fixNumberPlate(); { //fix number plate }
}

Now we have all in place, just we want to add it in car. Assume, car has one method called add Accessories(Accessories), here we can add any thing Panoramic or basic Dealer Accessories.

Thursday, May 15, 2008

Factory Pattern (Contd.)

Ok. Now Ford, Honda, Toyota coming back to us and saying that you know what, we are no longer a single country car makers, we have multiple car assembly plant, all across globe, we want you to design something to fit into our global assembly model. Oops...what to do? how to improve our design? Here comes real factory model. While we talk with them further, we understood that they are going to use same assemble, sentToPaint and delivery for different countries. One thing definitely going to change is car creation because now we have a simple car factory but the requirement says we need different cars based on different countries.

To achieve the above problem statement, first thing we do is convert CarBuilder class to an abstract class, we no longer use simple parametrize create car factory, we need something robust, multiple factories depends on countries.

Lets think about country based factories like India, US, Japan and so on. So definitely we need a IndiaCarFactory, USCarFactory, JapanCarFactory. Now what we have as a common with in these factories, one thing is assemble,sendToPaint,delivery so does it make sense if we implement all these methods in super class and let sub class implement createCar method and let sub class decide which car object to instantiate depends on country. That's our factory model. Here is real implementation.

public abstract class CarBuilder {
public void assemble(Car car) {
//a concrete shared assemble implementation goes here.
}
public void sendToPaint(Car car) {
//a concrete shared send to paint implementation goes here.
}
public void delivery(Car car) {
//a concrete shared delivery implementation goes here.
}
//this is going to differ based on country
abstract Car createCar(String name);
public void buildCar(String name)
{
Car car = this.createCar(name);
assemble(car);
sendToPaint(car);
delivery(car);
}
}

Here is factory implementation.
public class IndiaCarFactory extends CarBuilder {
Car createCar(String name) {
Car car = null;
if(name.equals("ford") {car = new IndiaFord(); }
if(name.equals("honda") { car = new IndiaHonda(); }
if(name.equals("chev") { car = new IndiaChev(); }
if(name.equals("toyota") { car = new IndiaToyota(); }
return car;
}
}

The same can be implement in USCarFactory and JapanCarFactory etc. So when we want to these from caller, all we need to do is

CarBuilder builder = new IndiaCarFactory();
builder.buildCar("honda");

that's it, now we have a car builder from India factory for Honda.

Factory Pattern

Factory and abstract factory pattern are little confusing but mostly used pattern in object oriented design.

Factory Pattern
The definition of factory pattern in simple "encapsulate object creation" and from Gang of 4 "Define an interface for creating an object but let subclass to decide which class to instantiate".

Here is one example, we have a concrete CarBuilder class implementation and a method buildCar as follows, this class has other important methods to build a car also.

public class CarBuilder {
public void buildCar(String name) {
Car car = null;
if(name.equals("ford") { car = new Ford(); }
if(name.equals("honda") { car = new Honda(); }
if(name.equals("chev") { car = new Chev(); }
assemble(car);
sendToPaint(car);
delivery(car);
}
}

Okay, all are fine, our class is ready to support 3 types car creation and supporting methods. After some days, now we want to add Toyota into this mix, so we need to add one more condition in buildCar method, and after some time we need to add 3 more types and adding 3 more if conditions in code, looks bad as well as we are leading to ambiguous class. So now we have to remove all if conditions to some class like utility class and pass name from here to get correct class implementation. Removing all if condition from here to other class is a simple factory pattern, that is, encapsulate object creation.

public class SimpleCarFactory {
public static Car createCar(Sting name) {
Car car = null;
if(name.equals("ford") {car = new Ford(); }
if(name.equals("honda") { car = new Honda(); }
if(name.equals("chev") { car = new Chev(); }
if(name.equals("toyota") { car = new Toyota(); }
return car;
}
}

createCar method declared static so we can call this method without instantiate SimpleCarFactory class. We haven't really into real factory pattern yet. This is just a starting point.

Tuesday, April 01, 2008

Unified Modeling Language - Sequence Diagrams

After use case and class diagrams, sequence diagrams are important element to any design document. One issue with sequence diagram is, we can spend 12 hours to draw a diagram or 5 mins to do the same. But it all depends on each team's decision that how much time they want to spend on sequence diagrams. Sequence diagram comes handy when the system/product goes to maintenance mode or when ever new team takes over the system/product from current team.

The simple definition of sequence diagram is, explore the logic of complex function and how many objects required and how they interacts with each other to satisfy an use case. Not all use case required a sequence diagram, we can identify complex or most critical ones to be the candidate for sequence diagram.

Lets take a simple use case "add a product in shopping cart".
1. User enter user name and password in login screen.
2. System validates user and let user in the main page.
3. User select a product from product menu.
4. System adds the product into user's shopping cart.
5. User clicks checkout link.
6. System displays checkout page.

Based on above simple use case, we can draw a simple sequence like this

User -> Login - > main page -> Product catalog -> check out

Now we need to convert this simple sequence into objects

aLogin -> aProductCatalog -> aCheckOut -> aBusinessService

After second iteration, we identified that we need a business service to get products from a data base. After second iteration we have to put some more stress on aBusinessService, how it is going to get data, which pattern we are going to use to get data, how we are going to transfer the data etc.

aLogin - > aProductCatalog -> aCheckOut -> aLookUp -> aProductDTO -> aProductDAO

Ok. Now we have some decent sequence, lookup service going to get DAO reference and return to aProductCatalog and aProductCatalog executes getProducts method on aProductDAO, in return aProductDTO as value object. We have a good explained sequence here at high level.

DAO = Data Access Objects
DTO = Data transfer Objects

So we are moving towards Sun's J2EE design pattern here, hence it is good add a business delegate in front of aLookUp. Like wise, we can improve our design and apply design pattern after each iteration until we satisfy our design.

Here is excellent UML and applying design patterns book by Craig Larmen

Sunday, March 23, 2008

Unified Modeling Language(UML) - Class Diagrams.

First 2-3 years in start-up, we are always in hurry to code new features based on hallway, coffee break discussions. No documents, no design and no formal code comments also. After some point of time, our products are out there in market and everybody liked it. Now comes the documentation trouble, we have to document all, whatever we designed and developed in past, primarily in our mind. If you are designer, you no need to write 100-200 pages document describing what you did. All we need are excellent diagrams, sketches and flow from start to end. UML is the only way to achieve this.

UML is not a strict language, in other words, there is no compiler out there to check our code against standards. A rough sketch can do the purpose. UML is useful on both forward and reverse engineering. The main component of UML is class diagrams, there is almost none design document without class diagrams. In class diagrams, the following notations and relationships are really important. This class diagrams are going to be a blue print for long time, hence this notations are important to communicate because we don't know whether designer who developed this diagram going to be with company or not.

1. Associations
In Class diagrams, if a class associate with other class, the a solid line with a arrow, from source to target. A perfect example would be, Customer and Order class. Order class must give a customer id, who created order. Since we can identify an unique customer id from order, Order class has an association with Customer class, that same with customer class also, from customer class we can find orders by the customers.

2. Compositions
A class can have other class's reference in it. This is useful when we want to utilize other class's methods without inheritance. Decorator pattern is mainly based on compositions.

3. Generalization
This is for grouping similar classes, to add these similar methods in a super class and all other classes are derived(inheritance) from it. This is nothing but super class and sub class notation in UML.

4. Aggregation
Aggregation and association are interchangeable. There is no major difference between them. A car has an engine, then car (whole) has a aggregation relationship with engine (part). To make our life simple, we can use association notation instead of aggregation. Most of the UML gurus discourage us to use aggregation notation in class diagram. We can just use association for top level design and composition and aggregation can be used in detailed design.

5. Realization
Realization is relationship between an interface and implements class. The interface specifies behaviours and a class implements that behaviour then the class realize relationship with interface.

6. Dependency
Dependency relationship as name implies, if one class change, other class's state changes then these two classes are dependency relationship. MVC pattern can be good example of dependency relationship.

The other important elements from class diagrams are multiplicity (upper bound..lower bound), Stereotype, visibilities and properties.

From Generative AI to Agentic AI: 2 Weeks of Building MyNewsAI.io

From Generative to Agentic AI: 2 Weeks Building MyNewsAI.io Over the last two weeks, I set out to evolve MyNewsAI...