Monday 3 July 2017

Software Engineering: Bad Smells! (Smelly code)

Bad Smells!

What are bad smells?
This refers to any symptom in the source code of a program that possibly indicates a deeper problem. 

These symptoms do not mean that the program does not function, but instead this shows weaknesses in design that may be slowing development or increasing risk of bugs or failures in the future.

The bad smells...

  1. Repetition: Copied, pasted, slightly modified
  2. Opacity: Bad naming
  3. Needless Complexity: Components add no direct benefit
  4. Rigidity: Change in code will need a cascade of changes
  5. Fragility: Slightest error the system does not usually handle
  6. Immobility: Design hard to re-use (extensiblility)
  7. Viscosity: Shortcuts can be made in the code
  8. Loose/Tight Coupling: Class dependency on each other



Software Engineering: Facade Pattern

Structural Pattern - Facade Pattern

Intent
Provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. 

Why is this used?
A facade decouples a client from a complex subsystem. Whilst the Facade subsystem and uses delegation to perform the work of the facade.


UML diagram representation

https://www.tutorialspoint.com/design_pattern/facade_pattern.htm

Source code example

https://www.tutorialspoint.com/design_pattern/facade_pattern.htm
More to follow...

Software Engineering: Adaptor Pattern

Structural Pattern - Adaptor Pattern


Intent
Converts the interface of a class into another interface the clients expect. Adaptor lets classes work together that could not otherwise because of incompatible interfaces.  

Why is this used?
Adaptor lets classes work together that could not otherwise because of incompatible interfaces. 


UML diagram representation

Adapter Pattern UML Diagram
https://www.tutorialspoint.com/design_pattern/adapter_pattern.htm

Source code example

https://www.tutorialspoint.com/design_pattern/adapter_pattern.htm
More to follow...

Software Engineering: Decorator Pattern

Structural Pattern - Decorator Pattern

Intent
Attach additional responsibilities to an object dynamically.  

Why is this used?
Decorators provide a flexible alternative to sub-classing for extending functionality.


UML diagram representation


https://www.tutorialspoint.com/design_pattern/images/decorator_pattern_uml_diagram.jpg

Source code example

https://www.tutorialspoint.com/design_pattern/decorator_pattern.htm
More to follow...

Software Engineering: Visitor Pattern

Behavioral Pattern - Visitor Pattern


Intent
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Why is this used?
Handy for recovering lost type information. Allow you to add operations to a Composite structure without changing the structure itself. Also, adding new operations is relatively easy. Furthermore, the code operations performed by the Visitor is centralised.


UML diagram representation


https://www.tutorialspoint.com/design_pattern/images/visitor_pattern_uml_diagram.jpg
Visual representation


http://www.vincehuston.org/images/visitor_taxi.gif

Source code example

https://www.tutorialspoint.com/design_pattern/visitor_pattern.htm
More to follow...

Software Engineering: Mediator Pattern

Behavioral Pattern - Mediator Pattern

Intent
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. 

Why is this used?
This increases the res-usability of objects supported by the Mediator by decoupling them from the system. In addition, this simplifies the maintenance of the system by centralising control logic. Also, this simplifies and reduces the variety of messages sent between objects in the system.


UML diagram representation


https://i.stack.imgur.com/T4KCw.png

Visual representation


http://www.jamesportis.com/wp-content/uploads/2015/11/mediator-software-design-pattern.gif

Source code example

https://dzone.com/articles/design-patterns-mediator
More to follow...

Software Engineering: Builder Pattern

Creational Pattern - Builder Pattern


Intent
Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Why is this used?
Encapsulates the way a complex object is constructed. This also allows objects to be constructed in a multi-step and varying process (as opposed to one-step factories). Furthermore, this hides the internal representation of the product from the client, while the product implementation can be swapped in and out because the client only sees the abstract interface.


UML diagram representation


https://www.tutorialspoint.com/design_pattern/images/builder_pattern_uml_diagram.jpg


Example of Builder
https://sourcemaking.com/design_patterns/builder

Source code example

https://sourcemaking.com/design_patterns/builder/java/2
More to follow...

Software Engineering: Bad Smells! (Smelly code)

Bad Smells! What are bad smells? This refers to any symptom in the source code of a program that possibly indicates a deeper problem.  ...