Tuesday, April 30, 2013

Write your own equality comparisons for a given class in C#.

The goofy book on philosophy I recently found has kept me entertained between jobs. I am inappropriately reading programming concepts onto the subject matter.

C# 4.0 in a Nutshell, the book I've been reading for far too long, explains how to write your own equality comparisons for a given class, and I thought of how I might apply this trick when measuring happiness as John Stuart Mill might define it. I thus might be able to roll a class called Act and assert that one instance of Act was superior to another like so:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Utilitarianism.Core.Acts;
namespace Utilitarianism.Tests
{
   [TestClass]
   public class ActTests
   {
      [TestMethod]
      public void reading_wordsworth_is_greater_than_drinking_ale()
      {
         ReadingWordsworth thatTimePoemsWereRead = new ReadingWordsworth();
         DrinkingAle thatTimePintsWereDrunk = new DrinkingAle();
         Assert.IsTrue(thatTimePoemsWereRead > thatTimePintsWereDrunk);
      }
   }
}

 
 

The vapid-while-not-vapid book I bought suggests he would agree with this test passing as it does. When I first heard of Utilitarianism (an ethics doctrine of: that which creates the most happiness is that which is moral) twenty years ago in an Austin Community College class I immediately feared how it could be the rationale for slavery. Mr. Mill had a different fear per the book. He feared it could be the rationale for epicurean excess in lieu of intellectual pursuits. Observe:

The book is too skim-the-surfacesque to delve into how reading Wordsworth is graded superior to drinking ale, yet Mill surely had a way to distinguish the two. This makes me wonder if there could be a subjective corny algorithm for as much like so:

namespace Utilitarianism.Core
{
   public static class Happiness
   {
      public static int Measurement(Act act)
      {
         return BalloonImportanceOfStimulation(act) * act.Joy;
      }
      
      private static int BalloonImportanceOfStimulation(Act act)
      {
         if (act.Stimulation < 0)
         {
            return act.Stimulation * act.Stimulation * -1;
         } else {
            return act.Stimulation * act.Stimulation;
         }
      }
   }
}

 
 

One makes the greater than comparison work at Act as shown below. Please note that without the "greater than method" below that the test at the top of this posting cannot compile and without the sister "lesser than method" one cannot have the "greater than method." Comparably the "equals method" has to have an accompanying "not equals method." If you have all four methods, you may wish to try to pull some of the mechanics out to a fifth private method as I did below. In the case of an inanely subjective algorithm that is apt perhaps to being swapped out with something else, the fifth method has a "Don't Repeat Yourself" function too, decoupling the algorithm from four places where it might otherwise be referenced.

namespace Utilitarianism.Core
{
   public abstract class Act
   {
      public int Joy { get; protected set; }
      public int Stimulation { get; protected set; }
      
      public static bool operator ==(Act yin, Act yang)
      {
         int position = JudgeBestOfTwoActs(yin, yang);
         return (position == 0);
      }
      
      public static bool operator !=(Act yin, Act yang)
      {
         int position = JudgeBestOfTwoActs(yin, yang);
         return (position != 0);
      }
      
      public static bool operator >(Act yin, Act yang)
      {
         int position = JudgeBestOfTwoActs(yin, yang);
         return (position == 1);
      }
      
      public static bool operator <(Act yin, Act yang)
      {
         int position = JudgeBestOfTwoActs(yin, yang);
         return (position == 2);
      }
      
      private static int JudgeBestOfTwoActs(Act yin, Act yang)
      {
         int yinValue = Happiness.Measurement(yin);
         int yangValue = Happiness.Measurement(yang);
         if (yinValue > yangValue) return 1;
         if (yangValue > yinValue) return 2;
         return 0;
      }
   }
}

 
 

In feeding my corny algorithm I tried to imagine why Mill might fear others being drawn to happiness by alcohol in lieu of literature and I constructed two goofy getsetters for Act to somehow delineate high-minded happiness from that which is of base pleasures. They both get numeric values. The farther they stray upwards, the better. Again, I have no idea what Mill's measuring stick for this really looked like, but I bet his was pretty silly too.

Here is the Act subclass Mill exalts: Here is a lesser Act child:
namespace Utilitarianism.Core.Acts
{
   public class ReadingWordsworth : Act
   {
      public ReadingWordsworth()
      {
         Joy = 1;
         Stimulation = 3;
      }
   }
}
namespace Utilitarianism.Core.Acts
{
   public class DrinkingAle : Act
   {
      public DrinkingAle()
      {
         Joy = 5;
         Stimulation = 1;
      }
   }
}

 
 

The book also has this interesting view of how everything (all experience) works per George Berkeley, who did not believe that matter existed!

This sort of one-way broadcasting is how RSS feeds work and really anything else with subscribers drinking up from a publisher. Meh. I go back to work in a week's time. I am probably overdue for being busy again given how I've distracted myself.

No comments:

Post a Comment