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]
 Tuesday, January 08, 2008

Have you ever need to browse to several pages and print them?  It seems like it should be easy and it is with this little Python script.

I had an Intranet based system that displayed items in a browser.  The manager of the group I built this for asked for a hard copy of every item that fit a certain criteria.  So instead of them going to over 400 pages by hand I just created this simple script. 

It worked like a charm.

#start script

from win32com import client
import time

ie = client.Dispatch("InternetExplorer.Application")

def printPage(url):

    print "printing " + url
    ie.Navigate(url)

    while ie.Busy:
        time.sleep(1)
        print 'navigating...'

    print 'starting print job...'
    ie.ExecWB(6, 2)
    print 'executed print job.'
  
    while ie.Busy:
        time.sleep(1)
        print 'printing...'


printPage("http://google.com")
ie.Quit()

#end script

Sources:

http://www.darkcoding.net/software/printing-word-and-pdf-files-from-python/

http://www.grimsworld.org.uk/printie.html

Tuesday, January 08, 2008 3:44:48 PM (Central Standard Time, UTC-06:00)  #    Comments [0]
 Sunday, December 16, 2007
Or ORM's Are Not the Whole Answer
OR Some ORM's Are A Better Answer

One point I feel I need to make.  So many people seem to think that at some point an ORM will appear that will just magically work.  It will map all your entities to objects and "just work."

So an endless parade of ORM's continue to lurch forth from the hinterlands. 

I'm amazed that people like Bruce Eckel think that an ORM powerful enough will just solve the problem.

The thinking is usually something like "we can shield the user from that ugly, mean SQL with some nice procedural and markup tricks." 

I've come to believe that anytime you try to hide/wrap/abstract a powerful technology/language (SQL, JavaScript, HTML, etc) you end up with at best a leaky abstraction and at worst a complete mess.

SQL is concise and powerful.  It doesn't need to be hidden.  It's not that hard to understand.  It's at least easier to understand than most of the ORM frameworks.  It's quite ridiculous what kind of code acrobatics you must perform to add parameters to a query. 

One of the biggest problems with an ORM is predicting what kind of code they will generate/execute.  The more considerate frameworks include a logging function of some sort.  This is nice but when I go to look at some code and I find a bunch of ORM garbage code, I find myself wishing for the SQL.

I used to hate embedded SQL, then I realized there really is nothing wrong with it, and it fact it's preferable as I found out what a web page is doing all in one file, without analyzing a (now unsupported) ORM or trying to find a stored procedure in the database.

The stored procedure approach is not that bad, I just find myself shying away from them as code re-use is a myth.  Yes, now I've become a true OOP Heretic.  Code re-use, especially between projects, is a rarity.  And this is the major problem with ORM's, they tend to emphasize the one object for every situation paradigm.

If I have an employee table, naturally I will have an employee object, if I'm an OOP ORM Myrmidon.  So I try to use this Employee object for my employee edit screen, my employee absence report, my employee salary report, my company org chart, etc, etc.  However, I soon find my nice clean ERD and UML diagrams become corrupted by the business needs.  We have to join in extra tables.  Pretty soon we are writing all manner of custom logic in our programming language of choice.  We have to use a little somthing like join_to(tables.employee_benefits).get_by(table.columns.user_name='a%').limit(10).sort_by(table.columns.user_name). 

We are wasting resources by bringing back unneeded columns.  Because the One True Employee contains fields for every situation, we populate the whole object for every request.  We can resort to lazy loading and other performance optimizations for expensive entities. 

The Better Solution

The answer is that there is no answer.  However, I think the frameworks like CoolStorage.NET and the db library in Web.Py have a nice ability to load data from queries into objects.  This takes away about 80 percent of the pain of Database/Object interaction and let's us use queries specific to the Use Case at hand.

I think the best we can do is to use the best tool for the job, in the case of talking to databases that tool is SQL.  We should use SQL and take advantage of automatic mapping. 

Learning SQL one time is much better than learning a new ORM every year.  I feel the same for HTML, JavaScript, CSS and the rest of fundamental technologies of our trade. 



 |  |  | 
Sunday, December 16, 2007 3:54:42 PM (Central Standard Time, UTC-06:00)  #    Comments [0]