Monday, October 06, 2008

/// <summary>
/// Round up to the best human readable number
/// (could be kb, mb or gb depending on size of
/// number
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public string BytesToHumanReadable(double bytes)
{
var gig = Math.Pow(1024, 3);
var meg = Math.Pow(1024, 2);

if(bytes > gig)
return Math.Round(bytes / gig, 1) + " GB";
else if (bytes > meg)
return (int)(bytes / meg ) + " MB";
else
return (int)(bytes / 1024 ) + " KB";

}


This is useful for file and memory sizes. 

Monday, October 06, 2008 10:42:20 AM (Central Standard Time, UTC-06:00)  #    Comments [0]
 Thursday, September 11, 2008
http://blog.sqlauthority.com/2006/12/30/sql-server-shrinking-truncate-log-file-log-full/ Assuming SMS is your database name and sms_log is your transaction log name: USE SMS GO DBCC SHRINKFILE(sms_log, 1) BACKUP LOG sms WITH TRUNCATE_ONLY DBCC SHRINKFILE(sms_log, 1)
Thursday, September 11, 2008 2:48:48 PM (Central Standard Time, UTC-06:00)  #    Comments [0]
If you need to find the size of each table in the database here is an easy way. EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'" I use it to find those pesky tables that are hogging all my disk space. (Usually it's the log file though) Source: http://www.4guysfromrolla.com/webtech/032906-1.shtml
Thursday, September 11, 2008 2:45:13 PM (Central Standard Time, UTC-06:00)  #    Comments [0]
 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]