Summary First Period

Summary First Period

The topics that will be covered in the exam of the first period are the next ones:
  • What is architecture?
  • Object Oriented Basics.
  • Patterns covered in the first period.
  • Refactoring.
  • Code snippets like the ones of the quizzes (Study them, they will not be covered in this blog entry).
  • Bad smells in code.

What is architecture?

Architecture of a complex system is a function of its components as well as the hierarchic relationship among these components.

Now we also talk about components, but what is a component. A component is an encapsulated part of a software system that serves as a building block for the structure of a system and it has the following properties:
  1. They are deployable entities.
  2. They are usually not a complete application.
  3. They might be used in unanticipated combinations.
  4. They have a well defined interface.

Object Oriented Basics

There are some concepts that we need to remember when we talk about Object Oriented Programming (OOP). These concepts are the following ones:
  • Abstraction: essential characteristics of some object, relative to the perspective of the viewer.
  • Encapsulation: hides the details of the implementation of an object.


  • Polymorphism: objects can respond to the same message in different ways in a class-dependent fashion.
  • Inheritance: a subclass may inherit the structure. and behavior of its superclass.

Patterns covered in the first period

Observer Pattern:

The Observer Pattern provides a way to subscribe and unsubscribe to and from these events for any object that implements a subscriber interface. This pattern was made in the second programming activity of the course (if you have any doubt I encourage you to do again the programming activity or only check it out).

Template Method Pattern:

The Template Method Pattern is a behavioral design pattern that defines a skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure. In this kind of pattern you use the inheritance property of Object Oriented Programming (OOP).  Also, I encourage you to check your implementation of the programming activity if you have any doubts.

Strategy Pattern:

The Strategy pattern is a behavioral design pattern that turns a set of behaviors into objects and makes them interchangeable inside original context object. An example of this pattern is the activity we did in class (specifically the strategy.rb and strategy_test.rb), in this example all the method we create are interchangeable because we define a different method doing different stuff with the data that the object Student has.

Refactoring

When we talk about refactoring we could end having two different definitions:

  1. A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.
  2. To restructure software by applying a series of refactorings without changing its observable behavior.
Now, it exists different types of refactors that will be going to describe bellow.

Replace Magic Number with Symbolic Constant

You have a particular number with a particular meaning. Create a constant, name it after the meaning, and replace the number with it.

Rename Method

The name of the method does not reveal its purpose (bad selection of the name). Change the name of the method.

Split Temporary Variable

You have a temporary variable assigned to more than once, but it is not a loop variable nor a collecting temporary variable. Make a separate temporary variable for each assignment.

Extract Method

You have a code fragment that can be grouped together. Turn the fragment into a method whose name explains the purpose of the method.

Hide Method

A method that is not used by any other class. Make the method private.

Introduce Explaining Variable

You have a complicated expression. Put the result of the expression, or parts of the expression, in a temporary variable with a name that explains the purpose.

Replace Error Code with Exception

A method that returns a special code to indicate an error. Raise an exception instead.

Introduced Named Parameter

The parameter in a method call cannot easily be deduced from the name of the method you are calling. Convert the parameter list into a Hash, and use the keys of the Hash as names for the parameters.

Replace loop with Collection Closure Method

You are processing the elements of a collection in a loop. Replace the loop with a collection closure method.

Bad Smells in Code

If it stinks, change it. There are different things that we need to have in mind when we say "Bad Smells in Code.

Duplicated Code

The simplest duplicated code problem is when you have the same expression in two methods of the same class. Then, all you have to do is Extract Method and invoke the code from both places.

Long Method

The object programs that live best and longest are those with the short methods.

Large Class

When a class is trying to do much, it often shows up as too many instance variables. When a class has too many instances variables, duplicated code cannot be far behind.

Long Parameter List

Long parameter lists are hard to understand, because they become inconsistent and difficult to use, and because you are forever changing them as you need more data. In object oriented programs parameter lists tend to be much smaller than in traditional programs.

Divergent Change

Divergent change occurs when one class is commonly changed in different ways for different reasons. Divergent change is one class that suffers many kinds of changes.

Shotgun Surgery

Is similar to divergent change but is the opposite. When the changes are all over the place, they are hard to find, and it's easy to miss an important change. Shotgun Surgery is one change that alters many classes.

Feature Envy

A method that seems more interested in a class other than the one it actually is in.

Data Clumps

Often you will se the same three or four data items together in many places like instance variables or parameters. Bunches of data that hang around together really ought to be made into their own object. 

Primitive Obsession

People new to objects are usually reluctant to use small objects for small tasks, such as money classes that combine number, and currency, and special strings such as telephone numbers and Zip Codes. You replace all that individual data values into objects.

Case Statements

The problem with case statements is essentially that of duplication. Often you find the same case statement scattered about a program in different places. Most times when you see a case statement you should consider polymorphism.

Parallel Inheritance Hierarchies

Is a really special case of Shotgun Surgery. In this case, every time you make a subclass of one class, you also have to make a subclass of another. 

Lazy Class

Each class you create costs money to maintain and understand. A class that isn't doing enough to pay for itself should be eliminated.

Speculative Generality

You get it when people say, "Oh, I think we need the ability to do this kind of thing someday".

Temporary Field

Sometimes you see an object in which an instance variable is set only in certain circumstances. Such code is difficult to understand, because you expect an object to need all of its variables.

Message Chains

You see message chains when a client asks one object for another object, which the client then asks for yet another object, which the client then asks for yet another object, and so on.

Middle Man

You look at a class's interface and find half the methods are delegating to this other class. After a while it is time to use Remove Middle Man and talk directly to the object that really knows what's going on.

Inappropriate Intimacy

Sometimes classes become far too intimate and spend too much time delving into each other's private parts.

Alternative Classes with Different Interfaces

Use Rename Method on any methods that do the same thing but have different signatures for what they do.

Incomplete Library Class

Builders of Library Classes are rarely omniscient, this means that it is probably that libraries will not be complete enough.

Data Class

There are classes that have attributes, and nothing else. Such classes are dumb data holders and are most certainly being manipulated in far too much detail by other classes.

Refused Bequest

Subclasses got to inherit the methods and data of their parents. The Refused Bequest is when the subclasses don't want or need the things that they. inherit.

Comments

Comments are often used as deodorant. It's surprising how often you look at thickly commented code and notice that the comments are there because the code is bad.

Metaprogramming Madness

While in most cases Ruby's dynamic nature provides great benefits, it can be misused. 

Disjointed API

Libraries are often flexible. This flexibility often presents itself as a relatively fine-grained, disjointed API, with many configuration options. More often than not, individual project will not take advantage of all the configuration options.

Repetitive Boilerplate

Most code isn't simple enough to declare it with the Extract Method, but when the purpose of the code can be captured clearly in a declarative statement.

Comentarios

Entradas populares de este blog

Summary Second Period

2-3: Microservices

2-4: Understanding the SOLID Principles