The diary of a .NET Developer, working on an unlaunched startup project. Also, I live and work in Southern Oregon.
Sunday, September 20, 2009
Where have I been?
Tuesday, April 21, 2009
Saturday, April 4, 2009
Unplugging from the borg
Wednesday, March 25, 2009
Website Finished and What's Next?
Saturday, March 7, 2009
Crunch Time
Wednesday, February 25, 2009
Lost Developer Time
Tuesday, February 24, 2009
I actually like Safari's font rendering over the "Windows Standard"
Thursday, February 19, 2009
Thoughts on the SOLID and some goals
- SOLID principles are all about decoupling your code.
- Decoupling your code helps you write good unit tests.
- Unit tests and (if you're up to it, TDD) are great ways to have quality software.
- Focus on when I should be using SOLID in my own code.
- Trying to make it up to Portland, OR for a PADNUG meeting
- Practice, practice, practice and make a lot of mistakes.
Wednesday, February 18, 2009
How to follow high traffic twitter conversations
Sunday, February 15, 2009
.NET Developer Stereo Types
Friday, February 13, 2009
Stackoverflow and SOLID Principles
And, I'm not sure this episode #41 has changed anything.
I've been programming for 6 years. With each new project I work on, I strive to improve my code just a little bit more than the previous project.
I'm not saying that I only write code that follows things like the SOLID principles. I'm saying that do try to think about ideas like this (and many other ideas) while I write my code.
Maybe I heard it incorrectly, but both Joel Spolsky and Jeff Atwood still seemed to think the SOLID principles were a waste of time.
I think this was more confusing than helpful to the programming community.
The idea that there is a set of engineering principles that I can incorporate into my code is invaluable to me.
Everyone that listened to the stackoverflow podcast #38 and #41 should at least listen to the hanselminutes episode with Bob Martin, and go read about the SOLID principles, and then just think about them next time you write some code.
Tuesday, February 10, 2009
Comparison<T> and IComparable<T> vs LINQ
Sorting Before LINQ:
- Implement IComparable<T> so you can use the default sort.
- Add a few Comparision<T> delegates so I could sort other ways
public class DrawDown : IComparable<T> : IComparable<T>
{
public static Comparison<DrawDown> DateComparison = delegate(DrawDown field1, DrawDown field2) { }
public static Comparison<DrawDown> ValueComparison = delegate(DrawDown field1, DrawDown field2) { }
public int CompareTo(DrawDown other) {}
}
And sort like this:
var list = new List<DrawDown>();
list.Sort();
//or
list.Sort(DrawDown.DateComparison);
Sorting with LINQ:
var list = new List<DrawDown>();
list.OrderBy(d=>d.Date);
//or
list.OrderBy(d=>d.Value);
Basically, I use LINQ more then anything else in the .NET framework. It really does save hours of writing boring “sort” code.
NOTE: I am working on getting better syntax highlighting in my blog. Live Writer is not working out at all.
Wednesday, February 4, 2009
Char to Int vs String to Int
I spent way to much time staring at this code trying to figure this out:
int number = 10;
int firstDigit = Convert.ToInt32(number.ToString()[0]);
Assert.AreEqual(1,firstDigit) //fails: firstDigit == 49
I knew it was returning the Unicode value of 1, which is 49, but I was after the integer value. Converting a char to an int always returns the Unicode value, not the integer value. Converting a string returns the expected result.
int number = 10;
int firstDigit = Convert.ToInt32(number.ToString()[0].ToString()); //add an extra ToString()
Assert.AreEqual(1,firstDigit) //passes
Nothing too exciting, but I was stumped for about 15 minutes trying to figure this out. I almost started looking for a atoi() function.
Tuesday, January 27, 2009
ASP.NET Ajax vs jQuery.ajax
I love jQuery but ASP.NET Ajax has some great features for calling your own webservices. Using jQuery to call services is much simpler, and has less going on behind the scenes, but is harder to debug. ASP.NET Ajax is creating a leaky abstraction for you, and along with that you get intellisense, helpful erorrs, and full visual studio debugging capabilities.
Calling ASP.NET Webservice with jQuery:
$.ajax({
type: "POST",
url: "PortfolioService.asmx/AddBenchmarkToCart",
data: '{portfolioId: "'+portfolioId+'", benchmarkId:"' + benchmarkId+ '", position:"'+position+'"}',
contentType: "application/json; charset=utf-8",
dataType: "json"});
Pros:
- Simple code: not a lot of behind the scenes magic
- Efficient: doesn’t load any extra javascript files
Cons:
- Hard to debug: it either works or it doesn’t, you get no useful error messages
- Six lines of code to copy/paste each time
Calling ASP.NET Webservice with ASP.NET Ajax:
<asp:ScriptManager ID="ScriptManager1" runat="server" >
<Services>
<asp:ServiceReference Path="~/Portfolio/PortfolioService.asmx" InlineScript="false" />
</Services>
</asp:ScriptManager>
PortfolioService.AddBenchmarkToCart(portfolioId, benchmarkId, position);
Pros:
- Intellisense
- Useful Error Messages
- Fully functional visual studio debugging
Cons:
- Less efficient: includes a small proxy javascript file for your webservice (which the client must download).
For more information on this, see this nice tutorial over on the asp.net site.
Javascript Class Definitions
Two ways to define a class in javascript.
At first glance, I didn’t realize what was going on in the second example, but the code is just using JSON syntax to define the prototype.
The benefit of reading other people’s code is you always learn something new.
(I picked this up browsing through the jQuery source code.)
I placed the actual source code here so you can can see it in action.
This is the way I always defined classes:
function Person2(name, age) {
this.name = name;
this.age = age;
}
Person2.prototype.HaveBirthday = function() {
this.age++;
}
Person2.prototype.GreetWorld = function() {
return "Hello World, my name is " + this.name + " and I am " + this.age + "years old";
}
A shorthand way using JSON syntax:
Person.prototype = {
HaveBirthday: function() {
this.age++;
},
GreetWorld: function() {
return "Hello World, my name is " + this.name + " and I am " + this.age + "years old";
}
};
This makes a lot more sense when you realize you can even do this:
var ben3 = {
age: 29,
name: "Ben",
GreetWorld: function() {
return "Hello World, my name is " + this.name + " and I am " + this.age + " years old"; },
HaveBirthday: function() { this.age++; }
};
Thursday, January 22, 2009
TDD and Private Methods: What an eye opener!
I’m continuing to practice TDD while creating a simple word search solver. I’m giving TDD a shot is because normally I don’t write testable code and find it difficult to create unit tests after getting so far into a project. This leads to little or no unit tests.
In my short time trying TDD, I’ve realized a couple things about my coding style. Although my code always works (ie. it performs the task at hand), it is full of interdependencies. This creates an environment that is pretty much impossible to write unit tests for. Any tests that I do write only cover large parts of the system, but they are not testing small units of my code—so they do not provide much value.
By sticking to TDD I’ve been forced to change this style in a drastic way!
Normally, this how my public facing code would look. The algorithm for SolveForWord would basically be hiding inside some private methods and fields of the PuzzleSolver class.
I could only test the method “SoveForWord”, which tells me nothing about how it was implemented.PuzzleSolver solver = new PuzzleSolver(wordSearchText);
PuzzleSolution solution = solver.SolveForWord(“dog”);
With TDD, I did things much different. After a bit of “new thinking”, I created a PuzzleParser class that implements parsing and data manipulation, and left the PuzzleSolver class to perform only the solving algorithm. PuzzleSolver gets an instance to PuzzleParser in the constructor. Then I extracted an interface for PuzzleParser:
public interface IPuzzleParser
{
string GetLetterFromCoordinate(int row, int col);
string GetLettersFromCoordinate(int row, int col, ParseDirection direction);
int MaxCol { get; set; }
int MaxRow { get; set; }
}
This is of course the DependencyInjection pattern, and I finally get why this is so valuable! In this case, I can mock the parser in a test:
MockRepository mock = new MockRepository();
IPuzzleParser parser = mock.StrictMock<IPuzzleParser>();
PuzzleSolver solver = new PuzzleSolver(parser);
So it's certainly baby steps in the right direction, but mabye I'm starting to see the light!
Monday, January 19, 2009
My own personal version of a "Startup Company"
Sunday, January 18, 2009
Testing private methods
I am writing a crossword word search puzzle solver for the purpose of practicing Test Driven Development. Yep, I am only writing code after first writing a test.
I started off with a very powerful method named SolveForWord:
public class PuzzleSolver
{
public PuzzleSolution SolveForWord(string word)
}
Almost instantly, I had the urge to start writing private methods to help parse the puzzle into a private data-structure. But since I’m doing TDD, I had to think about how to continue. Since I can’t test private methods, how do I write that code?
To be honest what I did next was find out how other people do in this situation. After a bit of googling, I decided to try testing my private methods using reflection. Then it hit me. My attempt to test private methods was “test friction” right? The PuzzleSolver class shouldn’t be doing any parsing! By experiencing this “test friction” I changed my design.
public class PuzzleParser
{
public PuzzleParser(string text)
{
}
}
PuzzleSolver will use PuzzleParser to perform parsing and not actually perform parsing itself. Single Responsibility in action!
Note: I didn’t know how much code to include in my post, so I purposely left out most of it.
Create Syntax Highlighted code in Blogger using Live Writer
I found a Windows Live Writer plugin to do syntax highlighting of text.
You can install plugins by simply copying the dll to your plugin directory.
Here is a test:
public class PuzzleSolver
{
private string[] rows;
public PuzzleSolver(string[] rows)
{
this.rows = rows;
}
public PuzzleSolution SolveForWord(string word)
{
}
}
Saturday, January 17, 2009
Test Driven Development is design
Monday, January 5, 2009
Vista Sidebar Gadgets are HTML pages!
Yep, you heard me right, the vista sidebar gadgets are tiny html pages with images and JScript (aka javascript).