Friday, August 31, 2012

When to use Static Classes


I recently read this post about static classes and when to use them.  I found it a very useful and persuasive read.  Also it is good to know "why" to use object oriented code and not just blindly default to that design pattern.

It is for C#, but is applies to any language IMO.  Here static classes/methods can also be meant to be "functional" programming.

This was educational to me, so I'm sharing the wealth.

PS: I am still going to use static classes like String.trim() where there is no member data (stateless), and the functionality is VERY well defined and not likely to change.

Wednesday, August 29, 2012

Perl Parentheses

Oh man,

In Perl, you use () to declare a list like this:

my @array = (0);
my %hash = ('name' => 'Arvid');

Then when you want to access you have to use [] or {}:

my $array_val = $array[0];
my $name = $hash{'name'};

Just nutty syntax folks.

Tuesday, August 28, 2012

Problem with Getopt::Euclid and Empty @ARGV

Today I was working on a pl program that had some existing code using @ARGV. It worked. Then I added:

use Getopt::Euclid;

I was mystified to find out that the code did not work anymore. I did a little RTFM and found that Euclid will populate %ARGV instead of @ARGV. Fun! It makes perfect sense for Euclid to use a hash, but I was confused for a few minutes...

Why did the code compile with array access to a hash?? Because it's Perl!!!

Edit: After more work and more troubleshooting, the ARGV thing is clearing up.  We have a lot of existing code that uses %ARGV and @ARGV.  Sometimes it easier to access all the elements of an array instead of a hash.  This code used to work.  Now it is broken since I upgraded Getopt::Euclid.  In a certain release of Getopt::Euclid the developer decided to empty out @ARGV.  This is documented in the Euclid pod.  Now the question is, "What do I do with all that existing code using @ARGV?"

I've got some ideas.  One easy, but maybe kind of lame solution is to put a wrapper around Getopt::Euclid and save off @ARGV and reset it after Euclid is done.  Or I guess I could change the code.  More research is required.

Monday, August 27, 2012

Perl At Underscore

So I am doing some work taking a lot of boilerplate code and moving it into a static or class subroutine.  And when I say boilerplate, I mean boilerplate to the extreme.  I'd say 99% of the subs in this code base have the same kind of code at the beginning.  (Function entry logging, arg counting, arg hash key verification, validation that $self is a ref)

One thing I learned along the way:

$self or (this in java) is not implicit in a Class like it is in java.  That means if you call a method from Class a within Class a, you still need to prefix it with $self->method().

Hmmmmmmmmmm.

See also http://www.netalive.org/tinkering/serious-perl/.

One other thing.  I ended up sending @_ to my new sub as a reference like this \@_.

Wednesday, August 15, 2012

Behavior Driven Development (Again)

Today I have been rocking the regression test world.  I made 18 regression tests using Selenium.  That's 3 suites and one "master" suite.  Now I will feel a bit more comfortable doing re-factoring in my web app.

Tomorrow, I may do some JUnit... oh wait - I'm on vacation!  :D