Saturday, September 6, 2014

What is Agile?

What is Agile?
  • Agile development is what we use to develop software.
  • Waterfall is what competitors use to develop software.
Simple question - simple answer.

Countless development processes have been developed to speed up the development of software.
Examples being: kanban, scrum, xp, lean, SAFe, CMMi, and ScrumXP to name a few

Factoid: Even though discredited, Waterfall was developed to speed up development and improve quality.

1. Question: So what is agile?

Answer: Agile is any process that parallelizes development.

Example:

Let's say you are shipping product X. Your customers give you a list of requirements. One month later they ask why it's taking so long.

1.1. Waterfall Answer:
I'm sorry but first we need to understand the requirements, then build infrastructure, then create the product and then test.

1.2. Standard Agile Answer:
We have broken up your requirements into multiple parts and each team is working in parallel. However, they number of requests you submitted is too much for our team to handle. We either need more time or more resources.

1.3. Super Agile Answer:
Here is what we did. Here is our backlog. If you wish, here are the APIs that we currently use. Please be free to extend the product if you are able and willing to do so.

2. Question: So what is Agile?
Answer: It is a process where you parallelize the work and let many people work on the project at the same time. The more you can separate the tasks into multiple parallel tasks, the more agile you are.

2.1. Open Source Projects:
In my opinion open source projects hosted in repositories such as GitHub are the ultimate in agile, since anyone can fork the work and drive the project in ways no-one who started the project could foresee.

Agile is an evolutionary process that drives the development of a project. Each new contributor is free to add and change things. At the end of the day the best solution wins and is integrated into the main product.

2.2. Never say "By Design"
The key to Agile is the integration of diverse views into a product designed to satisfy the needs of the Customer.

Many developers defend the current state of a product by saying that the feature is by design. That is anti-Agile thinking. Any feature that fails to meet the needs of a customer needs to be removed or redesigned. Any feature that goes against the expectations of a customer Must be removed or redesigned.

It is true that some processes need to work in a certain way for logical reasons. This must be clearly documented and processes that satisfy the customer must be explained. However, just because something has been done one way is no reason why we should continue doing it that way.

Example: Microsoft Excel has a global undo stack. It was created that way from the beginning. However it breaks the encapsulation principle where work on one object shouldn't affect work on another project. Also, it is standard for all document editors to maintain a separate undo stacks for each document. That way, changing one document won't have unexpected effects on another document.

If you ask the Excel developers, they would say that it is by design - however that is a design that can't easily be defended, especially since I have lost work because of that so-called design.

3. Finally

Question:So what is Agile?
Answer: Agile is any process that exceeds (or at least satisfies) the customer's expectation, allowing for a greater revenue stream.

Wednesday, July 16, 2014

The Flaws in Microsoft’s Windows Store (Windows 8, 8.1)

There is no question that Microsoft’s Windows Store is convenient.
  1. It allows people to download applications in a seamless way.
  2. It allows easy upgrades of applications.
  3. It’s an incredible platform that allows anyone to develop and sell applications that can compete with the big players.
  4. It is more secure than regular desktop apps since Microsoft verifies applications before posting.
However, Windows Apps have a few major issues that Microsoft needs to address.

1: Performance

Microsoft’s Windows Store Apps have horrible startup performance.
Try this performance test on Windows:
  • Open Visual Studio and create a new Windows Store Application
  • Run the application without changes
The splash screen appears.
SplashScreen
After a few seconds, the main app starts.
  • Switch to either the desktop or another Windows Store app
  • Switch back
The splash screen appears again and stays there for at least a second.

Why does it take so long for the application to start, even though there is no user code?

The answer is that Windows stops the application and saves its state. It then reloads the app when the user switches back.

2: Attention Hog

Imagine you execute a command that takes ten seconds to complete. If you were running a traditional desktop application, you could switch to another program and then switch back when the application completes its task.
Unfortunately that is not an option with Windows Store Apps. Windows pauses the application the moment you switch away from the app.
It is true that applications can do some background work while paused. However, in my opinion, this will make the application unnecessarily complicated.
Also, Windows only gives the application 2 seconds of processor time for every fifteen minutes of run time*. That means the application might take hours to execute a command that would otherwise take seconds.
Currently the only thing the user can do is sit at the computer and do absolutely NOTHING while the application does what it needs to do.
Many applications work best when run in the background. A perfect example is your music application. It plays music in the background while you do other things. A Windows Store App is currently not an option since switching away would shut off the music.
I like listening to WARM 106.9 in my web browser. (URL: http://player.liquidcompass.net/KRWMFM) How would I listen to that if the web browser were not in the front and taking up monitor real estate?


The same is true for video processing applications that perform time-consuming tasks.
> Windows Store applications force users to give applications 100% of their attention and prevent them from multi-tasking.
What can be done?
The solution is to allow the user to decide if an application should be paused when switching away from it.
Ideally the user should have two choices:
  1. Users need a global switch to turn on or off whether all Windows Store apps are paused
  2. Users need to be allowed to create exceptions. Ideally users should be able to do this through the task manager.
Since the ability to pause an app affects power consumption, it would make sense that it be in ‘Control Panel\Hardware and Sound\Power Options’.
The user would then choose application sleep options based on if the computer is plugged in or if it is running on batteries.

By letting application run in the background like regular desktop applications, the time delay for switching applications should be eliminated. This would give users a better user experience.

3: Windows Store Crashes

I notice that Windows Store applications can be very temperamental. For example I sometimes like playing Microsoft Solitaire or Mahjong. However, it tends to crash a lot when I do the daily challenges. This tells me two things:
  1. Microsoft’s Windows Store is running on Windows Azure and it is close to its limits.
  2. Windows Store applications can’t handle when connectivity is an issue.

4: Conclusion

Windows Store applications could be incredible if Microsoft fixed the performance issue. They also can’t be run as desktop applications. This means some applications, such as a music player, can’t be packaged as a Windows Store application.
My desktop computer is not a phone. Why should I be limited to what a phone can do?
* Professional Windows 8 Programming, Application Development with C# and XAML, 2012, ISBN: 978-1-1182-0570-9, page 168

Object Oriented Programming (OOP) Tutorial

In this blog I would like to present a simple tutorial on the basic principles of Object Oriented Programming (OOP) and the design patterns that are the basis of modern programming.
For convenience I will be using C#.

1. History

The principles of object oriented programming existed even before modern computers came into being. However the so-called 






1. Basic OOP Concepts

Object Oriented Programming is built on three principles: Encapsulation, Inheritance, and Polymorphism.

1.1. Encapsulation

Encapsulation allows you to hide data and implementation details and expose them in a controlled manner through publicly exposed methods.
Objects are self-describing things with internal structure not visible to the external world. They encompass one single idea or concept and they provide methods that allow external objects to interact with them.

1.2 Inheritance

Inheritance allows you to create a new class, using the previously defined methods of a base class.
Your new class will expose the existing methods of the base class as if they were newly defined. As such, creating a method with the same signature as the base class will hide the method in the base class.
NOTE: Inheritance is not polymorphism.

1.3. Polymorphism

Polymorphism occurs when an instance of a child class is treated as an instance of a parent class. Calling a method on the parent class will then express different behaviors, based on how the child class has overridden the parent class method.

Example:

As an example, you have a class called ‘Printer’. It defines certain actions, such as ‘Print’.
You then create a child class called Cannon 233. In this class you override the Print method.
You then call the ‘Print’ method on Printer. Since Printer is an instance of Cannon 233, the document is printed using the implementation encapsulated by Cannon233’s overridden method.
NOTE: In C#, polymorphism doesn’t occur unless you use either the key words virtual or abstract.

1. 4. OOP-Class

A class is the basic construct in Object Oriented Programming. As such it supports Encapsulation, Inheritance, and Polymorphism.
Note: When people talk about classes, they are referring to pure abstract classes that don’t have any implementation details. All you have are method signatures. As such, multiple inheritance is allowed.
For example:
pubic class Printer
{
    int PrintDocument(Document doc);
   
    Preferences GetPreferences();
   
    void Cancel(int cancelToken);
}

An important thing to keep in mind is that it is not the intent of OOP to prevent code duplication. That is just a side-effect of proper OOP design.
1.4.1. Responsibilities
  • Data Consistency: A class is responsible for making sure that the data it encapsulates is in a legal state between method calls.
  • Data Hiding: Class fields (integers, strings, etc.) should always be private. This data should only be exposed through methods.
  • Data initialization: It is the responsibility of the class to begin its life in a consistent state that is useful to the user.
  • Resource Management: Classes must make sure all resources are returned to the system when a task completes.
1.4.2. Data Structures
There is a fundamental difference between a class as defined in OOP and one as defined in a modern language such as C#. Unfortunately they both use the same name, which causes untold confusion. As such I will use OOP-Class to refer to the idea that OOP tries to encapsulate.
The following are various data structures relevant to modern programming…
C# Class
Supports: Encapsulation, Inheritance, Polymorphism
The C# class is the what people think of when they say ‘class’. C# classes are OOP-Class, since they enable encapsulation, inheritance, and polymorphism.
The fact that C# classes are reference types and reside on the heap is not relevant to the concept of an OOP-Class. That’s just an implementation detail.
Interface
Supports: Encapsulation, Inheritance, Polymorphism
The C# interface is the closest thing we have to the classic OOP-Class. The reason is that it only defines the operations in terms of method signatures. As a result, multiple inheritance is possible.
Interfaces are important since they are the basis for many design patterns.
Struct
Supports: Encapsulation
In C#, a struct is Not an OOP-Class. The reason is because it doesn’t provide inheritance (and polymorphism).
Structs are data structures that are used to implement integers, floats and other value types, including composite values.
Structs implement encapsulation. As a result, methods are exposed to modify its internal state.
C# Enums
Supports: <None>
In C#, an enum is Not an OOP-Class. The reason is because C# enums are just named lists of built-in numeric types such as int and float.
Java Enums
Supports: Encapsulation
In Java, the enum is a type of struct that has methods and internal states. However, just like enums, they don’t offer inheritance.
Prototype Collection
Supports: Encapsulation, Inheritance
See:
1.4.3. Objects
Classes define functionality. However, you can’t do anything unless you define an object and assign it states that are useful to the problem at hand.
An object is a data structure containing internal data that is in some valid state.
C# Objects
Objects are self-describing things with internal structure not visible to the external world. They encompass one single idea or concept and they provide methods that allow external objects to interact with them.



1.5. Methods

In OOP, methods encapsulate the specific single action you take to transform your data from its initial state to its final state.
The purpose of methods is to encapsulate actions performed of input data. When creating methods, you must ask yourself – What are the logical operations you are exposing through your API?
1.5.1 Method Variations
In traditional OOP, there is classes only implement methods. However, modern languages have variations on the theme. This allows us to encapsulate different design patterns within the language.
Methods





Properties
In C#, properties allow you to encapsulate internal fields and expose them to the caller. This allows the class to enforce policies that ensure its internal state is always consistent between method calls.
Static Methods
Static fields and methods are an odd thing in OOP. They don’t exist in traditional OOP.


























1.6. Conclusion

A programming language is considered object oriented if it implements a class as defined above.
According to the above definition C++ is an object oriented programming language.
That’s so last century. Smile with tongue out
When I think of OOP, I think of languages such as Java and C#, where everything is an object. It is impossible to program without creating a class definition and then calling methods on the class.
With C++, object oriented programming is possible, but not required.

2. Design Patterns

Design patterns are a funny thing. Everyone who has programmed with modern languages use the patterns without knowing that they are doing so. The reason is that many of them are built into the language.




3.1.1 Struct

A struct is a special type of class that is used to define values or collections of values.
The fundamental difference between a value type object





Reference

Friday, May 2, 2014

Prototype Collection

Objects in Object Oriented Programming can have arbitrary values. For instance, a string can contain any number of characters and a character can have any value.

However, not all values are relevant with regards to the problem space.

States

From Java, a classic example is the Planet class.

Planet data includes:

  • Average distance from the sun
  • Diameter at equator
  • Axis tilt
  • Rotation direction
  • Length of day
  • Length of year
  • Number of satellites
  • Type (Rocky, gaseous, etc.)

Obviously fields can have any value. There are an infinite possibility when it comes to planets. However, we only have 8 formally recognized planets in our solar system (the rest are planetoids, asteroids, etc.)

When we create an object of type Planet, we specify values for each property of the planet object. For instance, for Earth, we define LengthOfDay to be 86,400 seconds.

This is where prototypes come in.

Prototypes

A prototype is an object containing predefined values that has special significance to us.

This prototype is then copied and used wherever it is needed.

Using the Planet example, we can define prototypes for all the official solar system planets - Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune.

(Pluto is in another list called Planetoids, which possibly inherits from Planet)

The concept of the prototype was so significant that the Gang of Four (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides) included it in their book, Design Patterns: Elements of Reusable Object-Oriented Software (Copyright 1995).

Serialization

Serialization is a process of storing the state of an object in a portable format, such as a text string or XML document. This allows us to create a duplicate object at a later time.

Using serialization we can store object states that have significance for later use.

Prototype Collection

Since we have any number of prototypes for a specific object, we can collect these prototypes into a collection.

In java, this concept is encapsulated in its enumeration structure:

 

 

 

 

 

C# -
  1. public class Planet
  2. {
  3.     public static readonly Planet Mercury = new Planet("Mercury", 3.303e+23, 2.4397e6);
  4.     public static readonly Planet Venus = new Planet("Venus", 4.869e+24, 6.0518e6);
  5.     public static readonly Planet Earth = new Planet("Earth", 5.976e+24, 6.37814e6);
  6.     public static readonly Planet Mars = new Planet("Mars", 6.421e+23, 3.3972e6);
  7.     public static readonly Planet Jupiter = new Planet("Jupiter", 1.9e+27, 7.1492e7);
  8.     public static readonly Planet Saturn = new Planet("Saturn", 5.688e+26, 6.0268e7);
  9.     public static readonly Planet Uranus = new Planet("Uranus", 8.686e+25, 2.5559e7);
  10.     public static readonly Planet Neptune = new Planet("Neptune", 1.024e+26, 2.4746e7);
  11.     public static readonly Planet Pluto = new Planet("Pluto", 1.27e+22, 1.137e6);
  12.  
  13.     private Planet(string name, double mass, double radius)
  14.     {
  15.         this.name = name;
  16.         this.mass = mass;
  17.         this.radius = radius;
  18.     }
  19.  
  20.     public static IEnumerable<Planet> Values
  21.     {
  22.         get
  23.         {
  24.             yield return Mercury;
  25.             yield return Venus;
  26.             yield return Earth;
  27.             yield return Mars;
  28.             yield return Jupiter;
  29.             yield return Saturn;
  30.             yield return Uranus;
  31.             yield return Neptune;
  32.             yield return Pluto;
  33.         }
  34.     }
  35.  
  36.     public string Name { get { return name; } }
  37.     private readonly string name;
  38.  
  39.     public double Mass { get { return mass; } }
  40.     private readonly double mass;   // in kilograms
  41.  
  42.     public double Radius { get { return radius; } }
  43.     private readonly double radius; // in meters
  44. }

 

 

 

 

 

Please keep in mind that a prototype collection has no functionality. The only thing it can do is return a specified prototype and allows you to verify if two prototypes are equivalent.

As a result, many use the Java enumeration type incorrectly, by adding functionality.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

References

http://pdgowdy.wordpress.com/2012/07/27/c-implementation-of-a-java-like-enum/
http://stackoverflow.com/questions/469287/c-sharp-vs-java-enum-for-those-new-to-c
http://www.codeproject.com/Articles/38666/Enum-Pattern

 


 


 


 


 


IceRocket Tags: ,

Friday, March 28, 2014

Order of an Operation (Big O Notation)

The order of an operation refers to the amount of time an operation takes to complete. As a general rule, an algorithm runs with constant time if it works on a single piece of data. 

On the other hand, the order of an operation can vary greatly when dealing with collections.

1. Constant Time – O(1)

O(1) operations always take approximately the same amount of time, regardless of the size of a collection.

1.1 Array

Accessing an element in an array is a constant time operation, since the index is just an offset to the start of the array.

public int GetValueAtLocation(int[] arr, int index)
{
    // Bounds checking
    if (arr == null) throw new ArgumentNullException();
    if (index < 0 || index >= arr.Length) throw new ArgumentException();

    // This operation runs with constant time
    return arr[index];
}
1.2 Hashtable

Retrieving a value from a hashtable using a key can be done in almost linear time, depending in the implementation of the hashtable.

The time of retrieval depends on how well the hash key is implemented for the type being used as a key.

2. Log Time – O (Log(N))

Some operations take the log of the number of items to perform.

2.1 Binary Search

Binary search is used to find an item in a sorted array. With binary search, we half the number of elements we need to search during every iteration.

Find Smallest Integer
  1. /// <summary>
  2. /// Using binary search, find smallest int in array
  3. /// that has been sorted and rotated an unknown number of times.
  4. /// <example>
  5. /// arr = {93, 105, 124, 7, 23, 45, 87}
  6. /// returned index is 3
  7. /// </example>
  8. /// </summary>
  9. static int FindSortedArrayRotation(int[] arr)
  10. {
  11.     if (arr == null) throw new ArgumentNullException();
  12.     if (arr.Length == 0) throw new ArgumentException();
  13.  
  14.     int left = 0;
  15.     int right = arr.Length - 1;
  16.     int center = right / 2;
  17.  
  18.     if (arr[left] <= arr[right]) return 0;
  19.  
  20.     while (true)
  21.     {
  22.         if (left + 1 == right)
  23.         {
  24.             if (arr[left] < arr[right])
  25.             {
  26.                 return left;
  27.             }
  28.             else
  29.             {
  30.                 return right;
  31.             }
  32.         }
  33.         else if (arr[left] > arr[center])
  34.         {
  35.             left = center;
  36.         }
  37.         else
  38.         {
  39.             right = center;
  40.         }
  41.         center = (right - left) / 2 + left;
  42.     }
  43. }

3. Linear Time O(N)

The amount of time an O(N) operations takes place is proportional to the number of elements the algorithm is working on.

3.1 Search Unsorted Array

To search an unsorted array, we need to go through all the elements one at a time until we find what we are looking for.

Search for value
  1. public int Search(int[] arr, int val)
  2. {
  3.     for (int i = 0; i < arr.Length; i++)
  4.     {
  5.         if (arr[i] == val)
  6.             return i;
  7.     }
  8.  
  9.     return -1;
  10. }

The important point about the above method is that the ‘for’ loop goes through the array once.

3.2 Perform operation on all elements

By nature this is a linear operation.

Modify all elements
  1. public void DoSomething(int[] arr)
  2. {
  3.     // Do setup
  4.     for (int i = 0; i < arr.Length; i++)
  5.     {
  6.         // Do something
  7.     }
  8.     // Do cleanup
  9. }

4. Non-linear Time O(N2)

With O(N2) time, we go through the array once for every element in the array (worse case).

Bubble Sort
  1. public void BubbleSort(int[] arr)
  2. {
  3.     for (int i = 0; i < arr.Length - 1; i++)
  4.     {
  5.         for (int j = i + 1; j < arr.Length; j++)
  6.         {
  7.             if (arr[i] > arr[j])
  8.             {
  9.                 int t = arr[i];
  10.                 arr[i] = arr[j];
  11.                 arr[j] = t;
  12.             }
  13.         }
  14.     }
  15. }

Note: We know that it is O(N2) because we have two embedded for loops. The order of operation is just the number of embedded loops.

5. Non-linear Time O(Nm)

Here is a trick question. What is the Big O of this operation?

embedded loops
  1. public void DoSomething(int[] arr)
  2. {
  3.     // Do setup
  4.  
  5.     // loop 1
  6.     for (int i = 0; i < arr.Length; i++)
  7.     {
  8.         // loop 2
  9.         for (int j = 0; j < arr.Length; j++)
  10.         {
  11.             DoAction1();
  12.         }
  13.  
  14.         // loop 3
  15.         for (int j = 0; j < arr.Length; j++)
  16.         {
  17.             // loop 4
  18.             for (int j = 0; j < arr.Length; j++)
  19.             {
  20.                 DoAction2();
  21.             }
  22.         }
  23.     }
  24.  
  25.     // Do cleanup
  26. }

Since we have loop 1, that means we are dealing with a linear operation (at least). Loop 2 is embedded in loop 1, so now we have an O(N2) operation (at least).

The operation also has loop 3. Loop 2 and 3 are side by side, so they don’t affect the order of the operation. It is still O(N2) for now.

For loop 1,2 the order of operation is O(N2).

For loop 1,3,4 the order of operation is O(N3).

The higher order for the second one wins and the overall order of the method becomes O(N3).

Keep in mind that the order of operation for DoAction1 and DoAction2 must be taken into account. The order of operation of all embedded operations must be multiplied.

For the above DoAction1 and DoAction2, we had assumed the operation was in constant time. Now let’s assume the order for DoAction1 is O(N3) and order for DoAction2 is O(N1).

When we do this, loop 1,2 becomes 2 + 3 = 5, or O(N5).

Loop 1,2 becomes 3 + 1 = 4, or O(N4).

The first one wins now and the order of operation now becomes O(N5).

6. References