Angel \”Java\” Lopez on Blog

January 9, 2009

The Clean Code Talks — Inheritance, Polymorphism, & Testing

Filed under: .NET, Programming, Software Development — ajlopez @ 8:55 am

I’m a fan of Twitter. One account I’m following to, is @delicious_prog, that sends many tweets each day, related to programming and software development in general. In one of that tweets, I found the post

The Clean Code Talks — Inheritance, Polymorphism, & Testing

by Daniel Wild, where he includes a Google Talk video:

by Misko Hevery in Google Tech Talks.

It’s very clear and interesting. It’s a talk explaining the uses of polymorphism, and the way to test it. I pleased to find in this video many similar arguments to the ones I use in my dev talks. Recently, I was using polymorphism in my AjCat interpreter (a cat-like programming language interpreter written in C#)

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/AjCat

This is my Integer Binary Operation: 

public abstract class IntegerBinaryOperation : Expression { public abstract int Apply(int op1, int op2); public override void Evaluate(Machine machine) { int op2 = (int) machine.Pop(); int op1 = (int) machine.Pop(); machine.Push(this.Apply(op1, op2)); } }

The Add and Subtract operations override the Apply method:

public class IntegerAddOperation : IntegerBinaryOperation { private static IntegerAddOperation instance = new IntegerAddOperation(); private IntegerAddOperation() { } public static IntegerAddOperation Instance { get { return instance; } } public override int Apply(int op1, int op2) { return op1 + op2; } public override string ToString() { return "add_int"; } }
public class IntegerSubtractOperation : IntegerBinaryOperation { private static IntegerSubtractOperation instance = new IntegerSubtractOperation(); private IntegerSubtractOperation() { } public static IntegerSubtractOperation Instance { get { return instance; } } public override int Apply(int op1, int op2) { return op1 - op2; } public override string ToString() { return "sub_int"; } }

It’s very similar to the example presented in the video. I was a bit lazy in my operator implementation in AjPython (an interpreter, again, in C#, work in progress):

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/AjPython

 

public class BinaryOperatorExpression : BinaryExpression { private Operator @operator; public BinaryOperatorExpression(Expression left, Expression right, Operator oper) : base(left, right) { this.@operator = oper; } public override object Evaluate(Environment environment) { object leftvalue; object rightvalue; leftvalue = this.Left.Evaluate(environment); rightvalue = this.Right.Evaluate(environment); switch (this.@operator) { case Operator.Add: return (int)leftvalue + (int)rightvalue; case Operator.Subtract: return (int)leftvalue - (int)rightvalue; case Operator.Multiply: return (int)leftvalue * (int)rightvalue; case Operator.Divide: return (int)leftvalue / (int)rightvalue; case Operator.Power: return System.Convert.ToInt32(System.Math.Pow((int)leftvalue, (int)rightvalue)); } throw new System.InvalidOperationException(); } }

but I’m confident in refactoring, I have a test base covering this operators.

It’s good to find this talk, I recommend it, thanks to @delicious_prog and Daniel Wild!

Angel “Java” Lopez
http://www.ajlopez.com/en
http://twitter.com/ajlopez

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: