Tuesday, July 16, 2013

Some of the Anti patterns

Anti Patterns
It is exactly opposite of software design Patterns. Some the well known  anti patterns are the problems which software industry faces. Anti patterns are out comes of following bad practices, over engineering, implementation of design patters without having enough knowledge of patterns or not understanding the context of problem.  

Anti patterns are real world experience of understand recurring problems of software industry. It helps to recognize problems and understand the cause of it. They are also a bridge between understanding architectural concepts and its real world implementations.

There are three categories of software anti patterns like  
1. Software development anti patterns
2. Software architectural anti patterns
3. Software Project Management  anti patterns.

Some of the development Anti patterns

1. God Object ( Monster Object):

If some object know too much or does too much called a God object. Which is opposite to Single responsibility Principle  one of the SOLID principal. 

If Problem is big it is very hard or tough to resolve it. So if we divide it in a smaller parts of problem it is easy to solve them. So in software engineering if we create and object with single responsibility, we should divide one big task in to smaller separate responsibility (class) so when any change or problem occur we have to change smaller class and also we have to test only one smaller class after change.  

In case of God object, It is very tough to maintain it. It is also very tough to test it. If we change something small thing of a God object we have to test the whole object functionality.  

Symptoms: 
  • Single class with many attributes & operation.
  • Controller class with simple data object class
  • Lack of Object oriented design
  • A migrated legacy design
   
Consequences:
  • Tough to maintain 
  • Tough to test
  • Lost all object oriented advantage
  • To complex to reuse 
  • Expensive to load.
   
2. Copy and Paste programming.



This is favorite of any developer including me. Ctrl + C, Ctrl + V is part of any developers life. It is good if you are doing it from external sources to boost your development with good understanding of it, but it becomes Anti pattern when you do it with your own code.  If you are doing copy and paste of your own code then it is an Anti pattern.
 
Symptoms:

  • Similar code found in an Application.
  • When client change the requirement, You have to change code at more then 1 places.
  • Bug found by Tester and developer has fixed it but it reoccurs ( because not fixed all the places)
  • No code review done by experience programmer or lead.

 Causes:

  • It has huge maintenance efforts, If some requirement changed, developer has to change code to many places.
  • Developer is lack of knowledge of software development principals like OOPs, SOLID, DRY (Do not Repeat Yourself).
  • Lack of forward thinking among developers. 
  • A "Not Invented Here" syndrome applied to development environment.  Meaning development environment do not adopt external tools and employ reinvent the wheel. 
  • Developers are lack of knowledge of  new easy technology available. 

Whenever you as an developer come across the situation to copy and paste your own code, Stop that and find the opportunity to write it with common class or component and make it reusable with loose coupling.

3. Functional Decomposition

In Object oriented environment, If someone write code in procedural programming way, Just decompose Main methods in to several private methods only then it becomes Anti Patterns. So instead of writing different classes or design class architecture according to different tasks, if code is just decomposed in to private methods only then it is an anti pattern.  This is mostly done by experience programmer in procedural languages like  C and shifted to Object oriented programming language.

Symptoms:

  • All Class attributes are private and used in one class.
  • Degenerated architecture and missed most object oriented architecture
  • No use of re usability
  • Very tough to do documentation or design class diagram.


Causes:

  • Lack of object oriented design understanding. 
  • Lack of architecture enforcement.




4. Spaghetti code:

Spaghetti code is a code meaning unstructured code, written by developer who is less experienced or lake of knowledge of Object oriented programming. Code written like a procedural programming.  This kind of code, developer himself  can't understand his own code what he has written after few days.

code looks like spaghetti.


Symptoms:

  •  Code is not much reusable.
  •  Flow of execution dictate by objects and not the client of the objects
  •  Minimum relationship exits between objects.
  •  Code is not a unit testable. or end up writing too many integration tests.
  • No any advantage of Object oriented programming.
  • Very difficult to maintain. so maintenance cost is higher then re write new from the scratch.


Causes

  • Code written by inexperience in object oriented programming developer.
  • No design prior to development
  • No effective code review.

Whenever experience developer see this kind of code, he smells it and find opportunity to re factor it.

There are some other anti patterns are also there. I will understand it thoroughly and edit this blog.