Making Maths Functions n' Stuff Part 1

Welcome to part 1 of my random blurbs of math stuff into methods, using TDD methods, and sticking a nice simple WPF Interface on top.  Most of these examples I dug up out of O’Reilly’s C# Cookbook.  My point below though is for TDD examples with some WPF tossed on top.  More than anything, I just wanted to do this for fun.  Without further ado, here goes the coding.

First I created two projects, one class project for the methods and one test project for the tests.

Next I jumped right in and created this test first.

   1:  #region
   2:   
   3:  using MbUnit.Framework;
   4:   
   5:  #endregion
   6:   
   7:  namespace MathCookbookTests
   8:  {
   9:      [TestFixture]
  10:      public class Functions
  11:      {
  12:          [Test]
  13:          public void TestGettingDecimalValue()
  14:          {
  15:              const double numerator = 15;
  16:              const double denominator = 3;
  17:              const double result = 5;
  18:   
  19:              Assert.AreEqual(MathCookbook.Functions.GetDecimalOfFraction(numerator, denominator), result);
  20:          }
  21:      }
  22:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Then I sketched out the method.  Of course, as always I used ReSharper to get the skeleton of the code via the ole’ Alt+Enter option.

   1:  namespace MathCookbook
   2:  {
   3:      public class Functions
   4:      {
   5:          public static double GetDecimalOfFraction(double numerator, double denominator)
   6:          {
   7:              return 0;
   8:          }
   9:      }
  10:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

This now gives me a failing test.

Now to go after the green light.

   1:          public static double GetDecimalOfFraction(double numerator, double denominator)
   2:          {
   3:              return numerator/denominator;
   4:          }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

This now gives me a green light.  Next I added two tests for the next method I wanted to add.

   1:          [Test]
   2:          public void TestApproximationIsTrue()
   3:          {
   4:              const double numerator = 1;
   5:              const double denominator = 7;
   6:              const double aValue = .1428571;
   7:              const double epsilon = .0000001;
   8:   
   9:              Assert.IsTrue(MathCookbook.Functions.IsApproximatelyEqualTo(numerator, denominator, aValue, epsilon));
  10:          }
  11:   
  12:          [Test]
  13:          public void TestApproximationIsFalse()
  14:          {
  15:              const double numerator = 1;
  16:              const double denominator = 7;
  17:              const double aValue = .142857;
  18:              const double epsilon = .0000001;
  19:   
  20:              Assert.IsFalse(MathCookbook.Functions.IsApproximatelyEqualTo(numerator, denominator, aValue, epsilon));
  21:          }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Now of course, when I flesh out the skeleton code and just toss a default true or false back to get a clean compile, one will pass and one won’t.  But just jump into getting it to perform the appropriate function.

   1:          public static bool IsApproximatelyEqualTo(double numerator, double denominator, double value, double epsilon)
   2:          {
   3:              return Math.Abs((numerator / denominator) - value) < epsilon;
   4:          }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Now we run, and get green lights.

I wanted to enable the method above to accept floats for the value parameter also, so to do that I first added two tests.

   1:          [Test]
   2:          public void TestApproximationIsTrueWithFloat()
   3:          {
   4:              const double numerator = 1;
   5:              const double denominator = 7;
   6:              const float aValue = (float) 0.1428571;
   7:              const double epsilon = .0000001;
   8:   
   9:              Assert.IsTrue(MathCookbook.Functions.IsApproximatelyEqualTo(numerator, denominator, aValue, epsilon));
  10:          }
  11:   
  12:          [Test]
  13:          public void TestApproximationIsFalseWithFloat()
  14:          {
  15:              const double numerator = 1;
  16:              const double denominator = 7;
  17:              const float  aValue = (float) 0.142857;
  18:              const double epsilon = .0000001;
  19:   
  20:              Assert.IsFalse(MathCookbook.Functions.IsApproximatelyEqualTo(numerator, denominator, aValue, epsilon));
  21:          }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Then I got into doing two things, one adding the new method and the second thing was doing a complete refactor of the code.  What I ended up getting green lights with was the code below.

   1:          public static bool IsApproximatelyEqualTo(double numerator, double denominator, double value, double epsilon)
   2:          {
   3:              return IsApproximatelyEqual(numerator, denominator, value, epsilon);
   4:          }
   5:   
   6:          public static bool IsApproximatelyEqualTo(double numerator, double denominator, float value, double epsilon)
   7:          {
   8:              return IsApproximatelyEqual(numerator, denominator, value, epsilon);
   9:          }
  10:   
  11:          private static bool IsApproximatelyEqual(double numerator, double denominator, object value, double epsilon)
  12:          {
  13:              if (value is double)
  14:              {
  15:                  return Math.Abs((numerator / denominator) - (double)value) < epsilon;
  16:              }
  17:              return Math.Abs((numerator / denominator) - (float)value) < epsilon;
  18:          }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

So that’s it for now.  I’ll be hitting on more Maths Cookbook material, and will eventually get on to some other topics related to security, threading, delegates, and other material.  Eventually I’ll get around to posting some basic pattern implementations and how to test various parts of those patterns.  Basically this is kind of a little series I’m starting for my own entertainment, but maybe it might be useful to some out there in the development community.

In part two I’ll add the WPF UI to the application so we can actually enter and play with different values.

Microsoft Rant

[Rant On]

I’m very much pro standards.  I’m pro rail standards, I’m pro road standards, I dig web standards and browser standards, and I enjoy security standards and all sorts of standards.

Now, when there are standards and some entity ignores the standards it really pisses me off.  Recently I’m trying to download various WCF Projects from Microsoft and they ask for authentication via one’s passport.  Of course I have that and tried to login with it.  I was using Chrome and it didn’t work.  What makes this even worse is the fact that the little AJAX busy icon came up and I got zilch.  No message, no notification, no communication in any way that the site was stuck.  This is complete crap, and Microsoft should fix this immediately.  I tried a couple months ago and this even happened in IE.  Other problem being I’ve only got this to work in IE sometimes and also in Firefox… sometimes.  Opera it seems to work in consistently, which is odd.

[Rant Off]

Anyway, I’m done ranting now, on to a new topic…   which I have no idea for yet but I am actively researching WCF technology and authentication practices right now.  With that research I’ll definitely have a few blog entries coming down the pipe soon.

Stay Tuned

ReSharper, The Better Way, Is it Readable?

While working through some client application services samples recently I was going through and doing a code review, of myself, to see how or were I might be causing myself duplication, have isolation issues, or am not testing something correctly.  As I went through I found this little nugget of curiosity.  The code started out like this:

   1:          public ClientFormsAuthenticationCredentials GetCredentials()
   2:          {
   3:              if (ShowDialog() == DialogResult.OK)
   4:              {
   5:                  return new ClientFormsAuthenticationCredentials(
   6:                      usernameTextBox.Text, passwordTextBox.Text,
   7:                      rememberMeCheckBox.Checked);
   8:              }
   9:              else
  10:              {
  11:                  return null;
  12:              }
  13:          }

The “If” in the statement above had the little straight line under it that ReSharper signifies it thinks you should change something.  I then hit Alt-Enter for the ResSharper menu to pop up.  It did and gave me an option for “Replace with ‘return'” and also a “Convert to ‘switch’ statement”.  I decided I’d select the first option, since it was given as the default option, and received the conversion below:

   1:          public ClientFormsAuthenticationCredentials GetCredentials()
   2:          {
   3:              return ShowDialog() == DialogResult.OK ? new ClientFormsAuthenticationCredentials(
   4:                                                           usernameTextBox.Text, passwordTextBox.Text,
   5:                                                           rememberMeCheckBox.Checked) : null;
   6:          }

There are a few things of note in the code.  First off, there are 7 less lines used.  Second, it doesn’t really seem to be easier to read nor does it layout exactly what is going on.  It almost seems to work against behavior driven development principles.  Last thing I noticed right off is that the formatting, lines, and spacing aren’t to my liking, and I fixed that by hitting a new line at the “new” keyword, did ReSharper’s cleanup code option and got this:

   1:          public ClientFormsAuthenticationCredentials GetCredentials()
   2:          {
   3:              return ShowDialog() == DialogResult.OK
   4:                         ?
   5:                             new ClientFormsAuthenticationCredentials(
   6:                                 usernameTextBox.Text, passwordTextBox.Text,
   7:                                 rememberMeCheckBox.Checked)
   8:                         : null;
   9:          }

Now we’re back up to 9 lines of code, not like the line count matters.  The second point though, is that it is a little easier to read.  The formatting was also a little bit better.  Overall though, I’m still not sure this is easier to read than the if then else statement above.  One thing I was curious about though, and haven’t checked into further, is what this will compile to.  Does the CLR actually end up with the same compiled code?  At some point I might dig it apart, but for now I know the later examples are the suggested way to do this now, and it does remove code that is redundant (such as the “else” keyword in the first code snippet above).  For now I’ve grown a bit more used to reading the later two examples, but they still just don’t jump out to explain what is going on.

Anybody else have an opinion on these three variances?  Got a favorite?

Blogroll is NOT in the Database w/ BlogEngine.NET

ARRrrrrrrrrrrrrrrrrrrrrrrrrrrrrrGHGHGHGG!

I realized after doing a full push to my host again, that my blog roll is decimated!  Obviously BlogEngine.NET, even when setup to run against a database, doesn’t run the blog roll against the database.

So you’ve been warned BlogEngine.NET users!

Meanwhile, I’m going to work on getting that list re-created again.  Ugh.

…and yeah, I should keep a backup, and I do of the database, but just not that stuff.

Open Beer/Coffee Days and Awesome Geek Discussion

Ben Strackany has been putting on Open Beer and Open Coffee days over the last half dozen or so months.  I often try to attend because these events have great tech talk and other topics of discussion.  I suppose that always happens when you get smart people together for caffeine or beers.

I’m aiming to attend the next Open Beer day at The Green Dragon so make a point to head out and say hello.  I’m always looking to meet new people out there in Portland and see what everyone is up to.

Open Beer Day
Thursday, October 9th, at 5pm
The Green Dragon
928 SE 9th Ave.

View Larger Map

Open Coffee Day
Last Wednesday of Every Month at 10am
Backspace
115 NW 5th Ave

View Larger Map

Coffee or beer, you gotta like one, so come down and meet us, show us what you’re working on and who knows, maybe we’ll just invent the next big thing.