Monday, August 28, 2006

A simple logging function

I love to use the built in tracing functionality. However, sometimes I want logging for a sublevel .dll that I do not want to attach to the standard tracing files. Whenever I encounter this kind of a situation I fall back on a simple WriteToLogFile function that I wrote a long time ago. I usually have the .dll know about it's own log file and call this function from a helper .dll This has proven a very useful function when I need to log in .dlls without relying on a Tracing being turned on.

static public void WriteToLogFile(string filename, string output)
{

System.IO.FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.Read);
Byte[] OutputArray = new Byte[output.Length];
for ( int i=0; i < output.Length; i++)
{
OutputArray[i] = Convert.ToByte(output[i]);
}
fs.Write(OutputArray, 0, OutputArray.Length);
fs.Close();
}

Sunday, August 27, 2006

Standard Maintenance pages

I've been helping a friend out with websites recently, and I am constantly writing a fairly standard type of maintenance page. Almost every page has the same layout, and fields. It ties to a single table from a database and allows the user to edit all of the fields. Occasionally some of the fields are dropdowns or option buttons, but mostly they are simple text fields.

I'm thinking of writing a program to generate this standard maintenance page and all of it's code with all of the fields defaulted to text boxes. Then I can modify a mostly complete page to finish it.

I think it will be a fun program, now to find the time to do it.

T.V. a waste of time.

"T.V. is the great equalizer. The assasination of John Kennedy and David Letterman's stupid dog tricks are absolutely equal." -- Warren Bennis.

The book I'm reading has an entire chapter on how TV is wasting our times and how it is preventing leaders from emerging. I disagree. I think TV allows leaders to reach a large audience who otherwise would not have been able to. I believe the internet goes a step further. However, I do agree with one thing he says. TV is a great waste of time. The content that is on TV is terrible. The format of what we watch has gotten worse, and commercials are taking over more and more of the television. A typical 30 minute program has 5-10 minutes of commercials, with another 5-10 minutes of the show itself being used as a medium to show a companies wares; every 30 minutes we are bombarded with 10-20 minutes of advertising. While we sit on our butts and do nothing and learn nothing we absorb the advertising. I myself am guilty of it. I even blog about commercials that I think are clever. This is part of the reason that I only really like to watch sports on TV. I still don't grow, from the TV, but at least I get to watch a competition. Sport though, are even worse with advertising. Ads have been placed in the ballpark in such a way as to almost always be on the TV. The Nike swoosh on the clothes, the Rawlings insignia on the gloves. The backdrop behind home plate, even the world series had tv tricks last year to make advertisements look like they were on the field when they were only camera tricks.

I spent some time this weekend channel surfing looking for a program to help expand my mind. I found one. I think MythBusters could actually be considered a learning program. However, the rest of everything on TV. was solely entertainment. I have decided to reduce the amount of TV that I am watching. I will no longer watch these shows. When I go to bed, I will not turn the TV on to anything other than a sporting event. Erin can watch what she wants, I will close my eyes and sleep.

What will I be giving up when I give up TV? I'll be giving up my frustration. I'll be giving up my annoyance with commercials and programs.

What will I be gaining? I hope to be gaining more quality time with my children. I hope to not have TV distracting me from blogging, reading my newsletters, and reading books after the kids go to bed.

I will now take control back into my life and remove as much TV from it as I can. As I wrote this blog I turned off the Cubs vs. Cardinals game.

Friday, August 25, 2006

Comments available

Talking with someone who read this blog, I found out that he wanted comments available. I guess something I wrote was worth commenting on.

In any case, I have gone back through most of my old posts and turned comments back on. There are still some that I didn't think that anyone would need to comment on, so I left them disabled on those posts. On all of my posts going forward comments will be enabled. I hope you enjoy the topics that I cover. Feel free to comment and ask questions about the artidcles.

Thursday, August 24, 2006

Unit Testing Problem

I previously wrote about keeping Unit tests as stand-alone tests. This is the correct practice, However, doing so can sometimes cause a problem.

Problems occur when trying to test a boundary object, especially if it has complicated rules about how it works. The best thing to do in this instance is to replace the boundary object with a Mock Object that implements the expected results. If you don't plan for it, this can cause a shift in your design. For example:
puclib class ClassToTest
{
private boundaryClass myBoundary;
}

This is not good anymore. This code specifically ties ClassToTest to the boundary class. However, the boundary class could have the problems. Instead ClassToTest should have the boundary object passed to it, and code to an interface.

public class ClassToTest
{
private IboundaryClass myBoundary;
public ClassToTest(IboundaryClass boundaryClassItem)
{
myBoundary = boundaryClassItem;
}
}

This is a good chunk of code now, because boundaryClassItem can be either the actual boundary item (as it will in the deployed program), or it can be a mock object (which it will be in Unit Testing).

In my particular case, the login function being called by each test is a problem. However, for the purpose of testing each as a stand-alone item everything works fine.

The problem is that Login can only be called once nicely in on the boundary class and have the process work correctly. So NUnit itself is actually breaking my boundary class. However, a Mock class that allows that will allow NUNit to run successfully. I will at least have the confidence that everything except my boundary class works correctly, and then just try to fix bugs if they occur.

Tuesday, August 22, 2006

Unit Testing Do's and Dont's

Unit Testing is awesome and can be a huge help in validating that changes to code doesn't cause any problems anywhere else in the application. There are a few small guidelines to make sure you follow.

1. Unit Tests should be completely independent of each other. The order of tests is not gauranteed. Thus any setup code that happens all the time should be factored out into a function with the SetUp attribute. If the setup code happens on a lot of the tests, then create a private helper function that is called from each test to initialize values.

2. Unit Tests are good. Do them.

3. Name your unit test for what it is testing. Test1_GetFiles is not good. The test number is not important and can actually get confusing later when inserting new tests. TestGetFiles or Test_GetFiles is a good name. However, since only functions that have the Test attribute will show up in NUNit nameing it GetFiles is also useful.

4. Code that is in the function with the SetUp attribute should still be tested. If the SetUp is initializing a variable to an object or to a specific value, then have a test that just validates that the initialization worked. For Example:

int FilesExpected=0;
[SetUp]
private void Initialize()
{
FilesExpected = 10;
}

[Test]
public void TestNumberOfFilesExpected()
{
Assert.AreEqual(10, FilesExpected);
}

Strategy or Template Pattern

The training that I had last week was perfect. It put patterns into my mind and I have recognized that I have a problem to solve that requires either the Strategy Pattern or the Template pattern.

But how can I decide which one?

Problem description.
Iterate through a folder containing a list of files. Connect to a backend system. Process the files.

This itself does not lend itself to the pattern, it is not until I checked out the code from the repository that I see that we already have 2 other applications (they look like they were copy and pasted) that do the same basic logic. There is a slight difference in the processing of the file, but the logical flow of the application is exactly the same in all 3 instances.

Strategy Pattern according to James Cooper in C# Desing Patterns
"The strategy pattern consists of a number of related algorithms encapsulated in a driver class called the Context. Your client program can select one of these differing algorithms, or in some cases, the Context might select the best one for you. The intent is to make these algorithms interchangeable and provide a way to choose th emost appropriate one."

Template Pattern according to James Cooper in C# Desing Patterns
"The Template pattern formalizes the idea of defining an algorithm in a class but leaving some of the details to be implemented in subclasses."

I think they could both be made to work, but the Template pattern sounds better. So can build the base class, derive my class from it, and build a front end to use my new generic class. Then I need to refactor the other 2 implementations to make them also fit and I can have a single front end that can handle calling all implementations.

I like this whole patterns thing, I wish I had known it sooner.

Monday, August 21, 2006

Yes or No

I just finished reading Yes or No. This is a great short book by Spencer Johnson. You can read some of my other comments on it over on my non-geek blog.

This is a short book that could be read in 2 hours or less if you have time to dedicate to it. It took me 4 days of off and on reading. The program presented in the book to help make better decisions is a good thing. Basically any program to help make better decisions is a good thing if you use it. This book says that too. In order to facilitate better decisions you need to ask yourself 6 questions.

3 practical questions
1. Am I pursuing the real need?
2. Am I informed of my options?
3. Have I thought this through to a better result?

3 private questions
1. Am I really telling myself the truth?
2. Does this decision really feel right to me?
3. Do my actions show I believe I deserve better?

If you answer yes to everything then you do it, if not then rethink the process.
Spencer Johnson has once again written his book in the form of a parable, and there are times that the main character was frustrating either by stating the obvious, or by suddenly remembering a quote that fit perfect. For some reason the quotes were annoying because they were always stated by the guy who didn't know the system. The book was good though and it was definitely worth the time to read it. I would however suggest checking it out from a library vs. purchasing it. 20$ seems like a lot to spend on a pamphlet.

Wednesday, August 16, 2006

The more I know

I feel like a stupid kid sometimes when I get to the design portion of my work. I sometimes feel like I have trouble thinking in design.

I hope that as I continue to learn more and more about design patterns and Java I'll start thinking design more and be able to build better applications.