Tuesday, June 29, 2010

Data Access Layer

Data Access Layer created. Repository pattern:


Database objects:
  • Algorithm (immutable)
  • BoughtStock (immutable)
  • PlusMinus (enum)
  • CurrentStock (immutable)
  • StockHistory (immutable)
  • StockHistoryArchive (immutable)
  • StockTransaction (immutable)
  • Database (immutable)

Database repositories:
  • IRepository interface
  • Repository
    • Find
    • FindComplex
    • FindAll
    • FindAllComplex
    • Insert
    • InsertComplex
    • Save
    • Delete
  • IStatementFactory interface
Table specific repositories:
  • TableDeleteFactory implements IStatementFactory
  • TableInsertFactory implements IStatementFactory
  • TableSelectionFactory implements IStatementFactory
  • TableRepository extends Repository

Database factories:

  • IReaderFactory
    • ProcessList for retrieving lists
    • ProcessRecord for retrieving one record
  • TableFactory implements IReaderFactory
Example:
    public Algorithm FindById(int id, Database database)
    {
        Algorithm algorithm = new Algorithm();
        algorithm.setId(id);
        return Find(algorithm, new AlgorithmSelectionFactory(), new AlgorithmFactory(), database);
    }

C# tip - 6/29/2010

Initializing private variables in a class is good.  This practice will allow the object to never have nulls and not having to deal with NullPointerExceptions.  However, there are three reasons why you should not initialize the attributes:

  • initializing VALUE TYPES to NULL or 0 is redundant since these types are initialized to 0 at creation time.  
    • Int32 i; <== already initialized to 0.  
  • multiple initializations for the same object
public class Test
{
  private ArrayList t = new ArrayList();
  public Test(){}
  public Test(ArrayList l)
  {
    t = l;
  }
}
    • in this scenario, the ArrayList's initialization in the attribute is overriden by the initialization in the second constructor.  It's like initializing the same variable twice.  The following example is a better solution:
public class Test
{
  private ArrayList t;
  public Test()
  {
    Test(new ArrayList());
  }
  public Test(ArrayList l)
  {
    t = l;
  }
}
  • exception handling is impossible on attribute initialization.  If exception handling is necessary, initialize the attribute in the constructor.
When initially instantiating a type, here's the order of operations:
  1. Static variable storage is set to 0
  2. Static variable initializers execute
  3. Static constructors for the base class execute
  4. Static constructor executes
  5. Instance variable is set to 0
  6. Instance variable is initialized
  7. Base class instance constructors execute (for a specific constructor overload)
  8. Instance constructor executes

Monday, June 28, 2010

C# tip - 6/28/2010 (last one for today)

Use foreach loops.

Here's why:

  • efficiency => the compiler picks the most efficient loop implementation depending on the data collection being looped
  • ease of use => it's very simple to change the collection instantiation without changing the foreach loop.
private Square[] _board  = new Square[8];
foreach(Square _sq in _board);
===============================

private Square[] _board  = new Square[10];
foreach(Square _sq in _board);//will work without the need to change anything
===============================
private Square[,] _board = new Square[10,10];
foreach(Square _sq in _board);//will still work without the need to change anything
===============================
private Square[,,] _board = new Square[10,10,10];
foreach(Square _sq in _board);//will still work without the need to change anything



C# tip - 6/28/2010 (making up for the weekend)

Differences between four equality methods/operations:

  • public static bool ReferenceEquals(object, object)
    • returns true if objects refer to the same object.  It does not care about the object contents
Obj obj = new Obj();
Object.ReferenceEquals(obj,obj) returns true
Obj obj2 = obj;
Object.ReferenceEquals(obj,obj2) returns true
Obj obj3 = new Obj();
Object.ReferenceEquals(obj,obj3) returns false
Int32 i = 1;
Object.ReferenceEquals(i,i) returns false
Int32 i2 = i;
Object.ReferenceEquals(i,i2) returns false

  • public static bool Equals(object, object)
    • returns true if the objects are equal at runtime.  It uses the instance Equals methods to compare the objects if they are identical.


Obj obj = new Obj();
Object.Equals(obj,obj) returns true
Obj obj2 = obj;
Object.Equals(obj,obj2) returns true
Obj obj3 = new Obj();
Object.Equals(obj,obj3) returns false
Int32 i = 1;
Object.Equals(i,i) returns true
Int32 i2 = i;
Object.Equals(i,i2) returns true

  • public virtual bool Equals(object)
    • compares the object calling the method with the object passed in the method.  This is an overridable method.  The reason for that is because the default Equals method has to check for EVERY type possible since it doesn't know which type the object is until runtime.  One example is the ValueType, which is the base for all value types.  It will have to use reflection at runtime to get the runtime type of the object.  To improve the performance of this method, override it for your concrete class and only check for those conditions that you deem necessary.
    • To override the method properly, use the following pattern:
      1. check that the object parameter is not null (return false)
      2. check identity with ReferenceEquals method (return true if it is)
      3. check that the types are the same (return false)
      4. compare the objects' contents
    • do NOT throw exceptions out of this method
    • do NOT use as operation since that will incorrectly convert the object of subtype into a base type.
      • example: 
        • parameterObject is type BaseType, which SubyType extends
        • SubType test = parameterObject as SubType will always return NULL
        • parameterObject2 is type SubType
        • BaseType test = parameterObject2 as BaseType will return the object
      • use object.getType() instead
    • write the override for GetHashCode as well if this is done
  • public static bool operator ==(Class left, Class right)
    • override when dealing with value types


Stock code generator

Was able to successfully create a code generator in Ruby.  Currently, the generator creates 4 types of files:
- .JAVA files for each object in the following format:
package ;

public class {
  private _;

  public get(){
    return this._;
  }

  public void set( value){
    this._ = value;
  }
}

Due to the fact that some of the object will have to be immutable, some keys will have to be added to the code generator to validate that the correct object type is being generated.

- .SQL file for the DB creation
- .JAVA files for the repositories.
- ERD diagram view Ruby tK.

It has been a struggle learning Ruby.  If I had to go back and recreate all of my code, I would probably pick a language I already know (JAVA, C#, PHP, etc.) and use that language for my generator.  That's not to say that Ruby is a bad language; however, I am more of a traditional learner and "learning by doing" did not work out for me too well.

Now, onto developing the framework for the app, and starting tomorrow, starting my "one algorithm per day" development.

C# tip - 6/28/2010

Consider using immutable types when creating objects.

If your object is only considered to be a storage place where every set of data is linked to the rest of the object, consider not only using Struct, but also making the entire object immutable (and maybe even using static factory methods to populate the object itself).

An example would be a stock information.  The stock object would have NAME and SYMBOL as its attributes.  If you change NAME, you will have to also change the SYMBOL.  For example:

Stock stock = new Stock("MSFT", "Microsoft Corporation");
stock.setSymbol("GOOG");
- now, the stock object has the SYMBOL as GOOG and NAME as Microsoft Corporation, which is obviously wrong.

The best way to implement this is:

  • create the object as a struct
  • make all private attribute be readonly
  • implement only private setters and public getters
  • consider implementing static factory methods.
  • consider implementing the Builder pattern to create these objects if you have more than a couple attributes to set
Issues arising from using immutable types are the same that are listed in the Builder Pattern tip.

Thursday, June 24, 2010

C# tip - 6/24/2010

Value types versus Reference types:

  • structs
    • created on the stack
    • not polymorphic
    • memory efficient
    • not inheritable
    • used mostly for data storage
  • classes
    • created references on the heap
    • polymorphic
    • inheritable
    • define behavior
Use structs for simple objects that are just data containers.
Use classes for any objects that perform work.

Tuesday, June 22, 2010

Data Model

The below XML is a representation for the Data Model for the Stock application.  This XML will be used to create the following:

  • Data Access Objects
  • Repositories
  • ERD (for documentation purposes)
  • Create/update databases themselves
<?xml version="1.0" encoding="UTF-8"?>
<database name="Stock">
  <table name="current_stock" engine="Innodb">
    <column>
      <name>id</name>
      <type>int</type>
      <isNull>false</isNull>
      <key>PRIMARY</key>
      <autoIncrement>true</autoIncrement>
    </column>
    <column>
      <name>stock_symbol</name>
      <type>varchar(5)</type>
      <isNull>false</isNull>
    </column>
    <column>
      <name>last_price</name>
      <type>float</type>
      <isNull>true</isNull>
    </column>
    <column>
      <name>ask_price</name>
      <type>float</type>
      <isNull>true</isNull>
    </column>
    <column>
      <name>bid_price</name>
      <type>float</type>
      <isNull>true</isNull>
    </column>
    <column>
      <name>change_from_close</name>
      <type>float</type>
      <isNull>true</isNull>
    </column>
    <column>
      <name>open_price</name>
      <type>float</type>
      <isNull>true</isNull>
    </column>
    <column>
      <name>close_price</name>
      <type>float</type>
      <isNull>true</isNull>
    </column>
    <column>
      <name>change_from_previous</name>
      <type>float</type>
      <isNull>true</isNull>
    </column>
    <column>
      <name>stock_timestamp</name>
      <type>datetime</type>
      <isNull>true</isNull>
    </column>
  </table>
  <table name="stock_history" engine="Innodb">
    <column>
      <name>id</name>
      <type>bigint</type>
      <isNull>false</isNull>
    </column>
    <column>
      <name>stock_symbol</name>
      <type>varchar(5)</type>
      <isNull>false</isNull>
    </column>
    <column>
      <name>last_price</name>
      <type>float</type>
      <isNull>true</isNull>
    </column>
    <column>
      <name>plus_minus</name>
      <type>enum</type>
      <values>
        <value>PLUS</value>
        <value>MINUS</value>
        <value>SAME</value>
      </values>
      <isNull>true</isNull>
    </column>
    <column>
      <name>percent_change</name>
      <type>float</type>
      <isNull>true</isNull>
    </column>
    <column>
      <name>date_inserted</name>
      <type>timestamp</type>
      <isNull>true</isNull>
    </column>
    <column>
      <name>stock_timestamp</name>
      <type>timestamp</type>
      <isNull>false</isNull>
      <default>CURRENT_TIMESTAMP</default>
    </column>
  </table>
  <table name="stock_history_archive" engine="Innodb">
    <column>
      <name>id</name>
      <type>bigint</type>
      <isNull>false</isNull>
    </column>
    <column>
      <name>stock_symbol</name>
      <type>varchar(5)</type>
      <isNull>false</isNull>
    </column>
    <column>
      <name>last_price</name>
      <type>float</type>
      <isNull>true</isNull>
    </column>
    <column>
      <name>plus_minus</name>
      <type>enum</type>
      <values>
        <value>PLUS</value>
        <value>MINUS</value>
        <value>SAME</value>
      </values>
      <isNull>true</isNull>
    </column>
    <column>
      <name>percent_change</name>
      <type>float</type>
      <isNull>true</isNull>
    </column>
    <column>
      <name>date_inserted</name>
      <type>timestamp</type>
      <isNull>true</isNull>
    </column>
    <column>
      <name>stock_timestamp</name>
      <type>timestamp</type>
      <isNull>false</isNull>
      <default>CURRENT_TIMESTAMP</default>
    </column>
  </table>
  <table name="stock_transaction" engine="Innodb">
    <column>
      <name>id</name>
      <type>bigint</type>
      <isNull>false</isNull>
      <key>PRIMARY</key>
      <autoIncrement>true</autoIncrement>
    </column>
    <column>
      <name>stock_symbol</name>
      <type>varchar(5)</type>
      <isNull>false</isNull>
    </column>
    <column>
      <name>number_of_shares</name>
      <type>smallint</type>
      <isNull>false</isNull>
    </column>
    <column>
      <name>bought_price</name>
      <type>float</type>
      <isNull>false</isNull>
    </column>
    <column>
      <name>sold_price</name>
      <type>float</type>
      <isNull>false</isNull>
    </column>
    <column>
      <name>investment</name>
      <type>float</type>
      <isNull>false</isNull>
    </column>
    <column>
      <name>gain_loss</name>
      <type>float</type>
      <isNull>false</isNull>
    </column>
    <column>
      <name>bought_stock_timestamp</name>
      <type>timestamp</type>
      <isNull>false</isNull>
      <default>CURRENT_TIMESTAMP</default>
    </column>
    <column>
      <name>sold_stock_timestamp</name>
      <type>timestamp</type>
      <isNull>false</isNull>
      <default>CURRENT_TIMESTAMP</default>
    </column>
    <column>
      <name>algorithm_id</name>
      <type>tinyint</type>
      <isNull>false</isNull>
    </column>
  </table>
  <table name="bought_stock" engine="Innodb">
    <column>
      <name>id</name>
      <type>bigint</type>
      <isNull>false</isNull>
      <key>PRIMARY</key>
      <autoIncrement>true</autoIncrement>
    </column>
    <column>
      <name>stock_symbol</name>
      <type>varchar(5)</type>
      <isNull>false</isNull>
    </column>
    <column>
      <name>number_of_shares</name>
      <type>smallint</type>
      <isNull>false</isNull>
    </column>
    <column>
      <name>price_bought</name>
      <type>float</type>
      <isNull>false</isNull>
    </column>
    <column>
      <name>stock_timestamp</name>
      <type>timestamp</type>
      <isNull>false</isNull>
      <default>CURRENT_TIMESTAMP</default>
    </column>
    <column>
      <name>algorithm_id</name>
      <type>tinyint</type>
      <isNull>true</isNull>
    </column>
  </table>
</database>

Stock application frontend

The user interface for this application is fairly straight forward.  I want to see the following things initially:

  • stock history
    • include ranges to see certain sections
    • way to graph the history
  • way to execute algorithms
    • ability to select a set of stocks
    • ability to select date ranges
    • ability to select an algorithm
  • way to view stock summary
    • show for each algorithm:
      • price bought and sold
      • date bought and sold
      • initial investment
      • gain/loss
The technology used will be JSP with JQuery (Flot and DataTables plugins).  Each of the main bullets is its own tab:


C# tip - 6/22/2010

Advantages and disadvantages of is, as, and cast operations.

is - used to checked whether an object is a specific type
Object t;
if(t is SomeType){...}
  • Advantages
    • checks an object without an overhead of a try/catch block
  • Disadvantages
    • doesn't actually cast anything
as - used to cast reference objects from one type to another
Object t;
t as SomeType
if(t != null) {...}
  • Advantages
    • casts an object without an overhead of a try/catch block (returns null if cast fails)
  • Disadvantages
    • cannot be used on value types
cast - used to cast values from one type to another
  • Advantages
    • can be used to cast any types of values, whether reference or value.  Used in foreach loops for that reason (since collections could have either type)
    • ability to fail out of the class when casting an unexpected object is attempted 
  • Disadvantages
    • try/catch overhead
    • may force to insert business logic into the catch block

Monday, June 21, 2010

C# tip - 6/21/2010

Couldn't wait until tomorrow for this one.  Difference between const and readonly.

readonly is resolved at run time.  const is resolved at compile time.  Use readonly when you don't know the value until you have to use it.  Use const when the value is known and will never change throughout the lifecycle of the system.

Examples:
public class ConstExample
{
  public const int example = 1;
  public const String anotherExample = 2; //will fail compilation
}


System.out.println(ConstExample.example);
System.out.println(ConstExample.anotherExample);


public class ReadonlyExample
{
  public readonly int example = 1;
  public readonly int anotherExample;


  public ReadonlyExample(String value)
  {
    anotherExample = Int.parse(value);//if value is not an int, this will not throw an error until runtime
  }
}


System.out.println(ReadonlyExample.example);//prints 1

System.out.println(new ReadonlyExample("x").anotherExample);//won't fail until compile time
System.out.println(new ReadonlyExample("5").anotherExample);//will run fine


Advantages and disadvantages are very clear:

  • For readonly
    • Value can be set at runtime so a developer does not have to worry about knowing the real value ahead of time
    • Can be used for mutable objects
    • More flexibility
    • Can be have different values for each instantiation of the class
    • const can only be used for int and String types
  • For const
    • Performance
    • Maintenance (much easier to make a mistake with readonly if the attribute gets an unexpected value set at runtime)
    • Must be used with enums

Stock algorithm generation using DSL

Best way that I could figure out to create algorithms would be to have a utility where I specify the algorithm in a simple language, and upon running this query, it will end up being compiled as part of the algorithm project.

The simple example would look like the following:

CREATE ALGORITHM SimpleAlgorithm
LOAD STOCK HISTORIES FOR stock_symbol FROM dateOne TO dateTwo
IF stock_symbol IS NOT OWNED AND SOLD DATE IS NOT THE SAME AS TODAY
  LOAD STOCK HISTORIES FOR stock_symbol LAST 60 plus_minus ENTRIES
  CHECK 
   IF SUM OF stock_plus IS GREATER THAN SUM OF stock_minus
     AND LAST price IS GREATER THAN FIRST price
     BUY STOCK
     ALERT USER
IF stock_symbol IS OWNED AND BOUGHT DATE IS NOT THE SAME AS TODAY
  RETRIEVE LAST 60 ENTRIES FOR stock_symbol
  CHECK 
   IF SUM OF stock_minus IS GREATER THAN SUM OF stock_plus
     AND LAST price IS LESS THAN FIRST price
     SELL STOCK
     ALERT USER

The above code will generate a Java class that will extend PredictiveAlgorithm abstract class.  The generation will be implemented in Ruby and the code will be posted sometime next month.  This implementation makes it a lot easier to generate many algorithm and not have to worry about Java syntax.  The 17 lines above would generate quite a few lines of OO code.

Code Tip - 6/21/2010

If you want to create an immutable object but want to make sure that you don't have to create a different constructor for every combination of parameters, a consistent state, and readability, use the Builder pattern.

For an example, in my Stock application, I will have a Stock object that will not change throughout algorithm run.  It will always have the same name, description, dates, current price, and symbol.  The Stock object will always have a symbol but may not have some or all of the other parameters (or some combination of).  Here's the builder pattern implementation to create this object:

public class Stock
{
  private final int id;
  private final String stockName;
  private final String description;
  private final String symbol;
  private final float price;
  private final Date date;


  public static class StockBuilder
  {
    private final int id;
    private final String symbol;



    private String stockName;
    private String description;
    private float price;
    private Date date;



    public StockBuilder(int id, String symbol)
    {
      this.id = id;
      this.symbol = symbol;
    }


    public StockBuilder StockName(String name)
    {
      this.stockName = name;
      return this;
    }





    public StockBuilder Description(String description)
    {
      this.description= description;


      return this;
    }





    public StockBuilder Price(float price)
    {
      this.price= price;


      return this;
    }


    public StockBuilder StockName(String name)
    {
      this.stockName = name;
      return this;
    }


    public StockBuilder StockDate(Date date)
    {
      this.date= date;
      return this;
    }

    public StockBuilder Build()
    {
      return new Stock(this);
    }
  }


  private Stock(StockBuilder builder)
  {



    this.id = builder.id;
    this.symbol = builder.symbol;


    this.stockName = builder.stockName;
    this.description = builder.description;
    this.price = builder.price;
    this.date = builder.date;



  }


And now to initialize the Stock object:


Stock stock = new Stock.StockBuilder(5,"GOOG").StockName("Google").Price(500).Build();


Stock nextStock = new Stock.StockBuilder(5,"MSFT").Price(70.5).Description("Microsoft corporation description").Build();

Sunday, June 20, 2010

Stock application architecture


  • Presentation
    • JSP/JQuery
    • Silverlight
    • PHP
  • Validation block
    • Servlets
  • Web Services
    • Metro stack built on Glassfish
    • Service Interface Layer
      • contracts
      • contract implementations
      • data contracts
      • translators from data contracts to business objects and vice versa
    • Business Layer
      • workflows for logic processing
      • business objects
    • Data Access Layer
      • repositories for data access

Saturday, June 19, 2010

Code tip - 6/19/2010

x
This one's an oldie.  Originally from Effective Java by Josh Bloch.

Use static factory methods instead of constructors when creating an object.

Reasons why:

  • Method names describe the instantiation of the object much simpler than a constructor.
public class MyObject implements IObject
{
   public MyObject()
   {
      //logic
   }

   public MyObject(string param1)
   {
      //some other logic; however, besides the documentation, no way to tell what the difference between this constructor and the previous one is
   }
}
=============================================
public class MyObject implements IObject
{
   private MyObject() {} //no ability to call a constructor outside the class

   public static MyObject getInstanceDefault()
   {
      return new MyObject();
   }

   public static MyObject getInstanceWithName(string param1)
   {
      MyObject obj = new MyObject();
      obj.setName(param1);
   }
}

constructor instantiation:
IObject m = new MyObject();
IObject m1 = new MyObject(name);

factory method instantiation:
IObject m = MyObject.getInstanceDefault();
IObject m1 = MyObject.getInstanceWithName(name);
  • Can return an object that's a subtype of the current type
public class MyObject implements IObject
{
   public MyObject()
   {
      //logic
   }
}
=============================================
public class MyObject implements IObject
{
   public MyObject() {} 
}

public class MyOtherObject implements IObject



{
   public MyOtherObject() {} 
}

public class MyObjectProvider
{
   List list = new ArrayList();

   public static void AddNewObject(IObject obj)
   {
      this.list.add(obj);
   }

   public static IObject getInstance()
   {
      if(list.Size() == 0)
          throw new Exception();
      else
      {
          IObject obj = this.list.get(0);
          this.list.remove(0);
          return obj;
      }
   }

}

constructor instantiation.  Can ONLY return object of MyObject type:
IObject m = new MyObject();

factory method instantiation:
prep:
MyObjectProvider.AddNewObject(new MyObject());



MyObjectProvider.AddNewObject(new MyOtherObject());

IObject m = MyObjectProvider.getInstance(); //returns MyObject type
IObject m1 = MyObjectProvider.getInstance(); //returns MyOtherObject type

    Friday, June 18, 2010

    Stock application requirements

    Currently, I'm in the process of creating a predictive algorithm for Stock trading.  The requirements are the following:
    • Since I am not planning on investing more than $25,000, I can only make one trade per day (buy/sell)
    • The profit must be more than 5% total income + transaction fees per week:
      • example: $500 deposit must make (500*.05) + 15 trades (buy/sell) at $3.95 a piece = 25 + 30*3.95 = $143.5 profit per week, which is actually 28.7% gain (way above any average).
      • $1,000 deposit must make (1000*.05) +15 trades (buy/sell) at $3.95 a piece = 50 + 30*3.95 = $168.5 profit per week, a 16.8% gain.
      • $5,000 deposit must make (5000*.05) +15 trades (buy/sell) at $3.95 a piece = 250 + 30*3.95 = $368.5 profit per week, a 7.3% gain.
      • $10,000 deposit must make (10000*.05) +15 trades (buy/sell) at $3.95 a piece = 500 + 30*3.95 = $618.5 profit per week, a 6.1% gain.
    • Algorithms used must be interchangeable (runtime deployment).
    • A way to specify the time period and specific stocks to use when testing algorithms.
    • A way to interchange presentation interfaces.
    • A way to compare algorithms on the fly.

    Daily C# tip - 6/18/2010

    From Effective C# book:


    Easy one: Use Properties instead of data members.


    Reasons why:

    • .NET supports properties for data binding code classes, but does not support data members.
    • Validations can be used within the property.  Validations will then be coupled with the objects themselves instead of being implemented everywhere the object is used, simplifying code maintenance.
    • Multithreading can be added easier just by adding the synchronization to the get and set methods.
    • Greater flexibility on the visibility of different functions of your property.  For example, the get method can be public but the get method is protected and can only be called within the object hierarchy.

    Labels