Wednesday, July 30, 2008

This method indicates whether an object is numeric or not.  Handy in reflection type situations when you need to perform and action based on type.

private static bool IsNumeric(Object o)
{
    if((o is int) || (o is int?) ||
        (o is decimal) || (o is decimal?) ||
        (o is double) || (o is double?) ||
        (o is float) || (o is float?) ||
        (o is long) || (o is long?) ||
        (o is ulong) || (o is ulong?) ||
        (o is ushort) || (o is ushort?) ||
        (o is short) || (o is short?) ||
        (o is byte) || (o is byte?) ||
        (o is sbyte) || (o is sbyte?) ||
        (o is uint) || (o is uint?))
        
        return true;
    
    return false;
}


It's long and a bit ugly but it's the only way I've found.

Wednesday, July 30, 2008 1:24:38 PM (Central Standard Time, UTC-06:00)  #    Comments [1]
 Sunday, July 13, 2008
http://erikengbrecht.blogspot.com/2008/07/love-hate-and-type-inference.html

Type inference is great, locally that is.  When it's global it's a mess.  That's why large programs are easier to maintain and refactor in C# than in Python. 

Conversely, scripts are much better in Python than in C#.

So when right a short and dirty script, Python is fantastic.  When writing something a little bigger, the statically typed languages start to win.  There is no "Static is better or Dynamic is better"  only what locally optimal.

I think C# 3.0 is blurring the line somewhat with the best of both worlds.  It's locally type inferred (or it can be if you want it that way) and globally static. 


Sunday, July 13, 2008 8:06:02 PM (Central Standard Time, UTC-06:00)  #    Comments [0]
Scraping the web is hard.

Matt Cutts says so:
http://www.mattcutts.com/blog/the-web-is-a-fuzz-test-patch-your-browser-and-your-web-server/

I've found this to be true.

 A couple of implications. 

It's hard to build a web crawler that can suck information out of pages reliably.

Validation doesn't matter b/c google doesn't penalize for it.  And if Google doesn't care, you shouldn't either.


Sunday, July 13, 2008 7:54:28 PM (Central Standard Time, UTC-06:00)  #    Comments [0]
 Tuesday, June 24, 2008
  1. Reduce dependencies
    Isolate them if you can't remove them.
  2. Log Don't Explode
  3. Don't Handle Errors if You Don't Know How
    Let them raise so they can get identified and fixed
Tuesday, June 24, 2008 3:49:58 PM (Central Standard Time, UTC-06:00)  #    Comments [0]
 Thursday, June 12, 2008
Occasionally I need to change a a camel case or Pascal case string into a human readable string.  Since I find myself doing this again and again, I figured I better post it here.


private string HumanizeString(string source)
{
StringBuilder sb = new StringBuilder();

char last = char.MinValue;
foreach (char c in source)
{
if (char.IsLower(last) &&
char.IsUpper(c))
{ sb.Append(' '); }
sb.Append(c);
last = c;
}
return sb.ToString();
}

Thursday, June 12, 2008 3:47:38 PM (Central Standard Time, UTC-06:00)  #    Comments [2]
 Wednesday, March 26, 2008
Find controls in a page or another control

List<T> FindControls<T>(Control control) where T : Control
{
List<T> list = new List<T>();

foreach (Control c in control.Controls)
{
if (c is T)
{
Global.LogDebug(c.ID);
list.Add(c as T);
}

list.AddRange(FindControls<T>(c));
}

return list;
}


private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}

foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}

return null;
}


Wednesday, March 26, 2008 9:35:09 AM (Central Standard Time, UTC-06:00)  #    Comments [0]
Find the compatible types in an assembly:

public static Type[] FindCompatibleTypes(Assembly assembly, Type baseType)
{
List<Type> types = new List<Type>();

foreach (Type type in assembly.GetTypes())
{
if (type != baseType && baseType.IsAssignableFrom(type))
types.Add(type);
}

return types.ToArray();
}

Wednesday, March 26, 2008 8:43:39 AM (Central Standard Time, UTC-06:00)  #    Comments [1]
 Sunday, February 24, 2008
In the last few years, I've come to believe very strongly in personality as a key to understanding yourself and your relationship with others.

I've come across an article that I agree with and have seen work in person:

http://www.softwarebyrob.com/2006/08/20/personality-traits-of-the-best-software-developers/

Personally, I've seen the example of flaky people writing flaky code, where the invere is also true; solid people writing solid code.

Sunday, February 24, 2008 9:43:16 AM (Central Standard Time, UTC-06:00)  #    Comments [0]
 Sunday, January 20, 2008
Interpolation is just a fancy word for variable substitutioin in a string.

According to the free dictionary interpolate means:
1. To insert or introduce between other elements or parts.
2.
    a. To insert (material) into a text.

Perl does this very well, other language less so.  Less is a hybrid of the C# method for string formatting.  I find it pretty convenient when creating a lot of dynamic html to insert using that oh so hander innerHTML property.  Remember, innerHTML is usually the fastest way to add dynamic html.

function format(format) {
var params = arguments;
var toReturn = format;

for (var i = 0; i < params.length - 1; i++) {
var rString = '\\$\\{' + (i) + '\\}';
var regex = new RegExp(rString, "g");
toReturn = toReturn.replace(regex, params[i + 1]);
}

return toReturn;
}

Here's the original interpolation function that extended the string object.  I don't really think that extending builtin objects is a great idea unless there is no other way.

Sunday, January 20, 2008 7:46:19 PM (Central Standard Time, UTC-06:00)  #    Comments [0]
 Wednesday, January 09, 2008

OK, so I've been using Python quite a bit lately and I've been enjoying it.  I've been getting stuff done with it too.  Python inter-operates with COM quite well and that saved me a lot of pain yesterday.  However, learning Python has caused me pain in other areas.

Yesterday I had to perform some maintenance in a C# ASP.NET app that I had written not too long ago.  Even though I use the CoolStorage.NET and some other nice time savers, it has become painful to write C# code.  It's just too verbose.

I used to marvel at people who claimed that you don't need an IDE to develop with Python.  I thought that was blasphemy as Visual Studio was the bee's knees in C# development.  However, I'm amazed at how much more I'm engaged in coding when I'm writing Python (or JavaScript).  You have to pay attention to what you're doing, no IDE is going to help you out.  However, this heightened state of concentration is actually fantastic for writing good code.

I use Notepad++ as my editor for Python (and JavaScript and HTML, etc) when I'm not using C# and Visual Studio.  I find its adequate for the job and it does have a function/method browser built in.  It also does HTML auto-complete and it can highlight a psp template file where I have Python, HTML and JavaScript all in one file.  It's also very stable, something that Visual Studio is not.

Visual Studio 2005 is flaky.  Sometimes all my Windows will rearrange or reset.  Other odd things happen and I find myself re configuring my layout from scratch.  Actually worse than scratch because every thing is helter skelter around. 

The worst part though is the amount of code I have to write to keep the compiler happy.  I know what I want to do and I know how I want to do it but I have to tell the compiler what I'm doing ahead of time so that it's happy.  Honestly, this is a great feature for a newbie or someone who really doesn't want to learn the language.  However, it becomes a burden after a while.  The C family of language has so many tokens (symbols like { [ ( ; , for while do foreach if) that your code becomes very noisy very fast.  Python is nice and clean which is something that I've come to appreciate.

I think I've found a better appreciation for Python (and JavaScript) because I've used C#/C++/Java.  For my everyday programming needs Python can handle everything I need to do.  However, at my current job getting Python into the mix might be a bit hard.  However, it might be worth it.  We spend so much of our day writing Programs that should just be nice and easy scripts.  We have so many programs and projects that could easily be one python file.  We do a lot of file transfer and validation. 

So yes, I'm feeling the pain of C#.  I just don't need/want that bug IDE and big language for what I need to do. 

Python does it faster, smaller and cleaner. 

Wednesday, January 09, 2008 9:33:53 AM (Central Standard Time, UTC-06:00)  #    Comments [0]