For those of you that know me, they know I've been an avid martial artists for a while now, and recently had the distinct pleasure of attending a seminar of Guru Dan Inosanto.
For those of you not in the know, Guru Dan is probably one of the largest figures in the Martial Arts world, being the right hand man of the late Bruce Lee, and one of the main men in continuing the Jeet Kun Do tradition of Lee fame.
While his seminar was very rewarding, and the man obviously has a encycopedic knowledge of martial arts, there was one thing that struck me as the main reason that he is such a large figure within martial arts:
He doesn't let his ego get in the way.
He gave us a little piece of advice, which I felt was really useful (to paraphrase):
It doesn't matter what level you are at, always remain a student at heart.
Guru Dan explained this in terms of when he was given his first black belt, and was an instructor, he was actually a worse fighter than he was when he was brown. He explained that simply because he was so afraid that anyone would see him do wrong, that he became incredibly conservative in what he did, and was genuinly afraid of failing in front of his students.
As a student, you are able, if not expected, to fail, make mistakes and pick up where you left off - somehow when you get raised to a higher position, often your ego will tell you that you are no longer able to enjoy the learning process of making mistakes, and must be 'correct' all the time.
I know I've seen it, not only in my own martial arts training, but also in the workplace, where people get promotions to Managers, Supervisors, etc, and become one of those people everyone hates to work for, because their ego predominates their actions, and they don't want to seem stupid, ignorant or in error. Those Managers that everyone love working for, are often the ones that don't have their ego attached to their opinions, and are more than happy to point out when they don't know something, and enjoy the process of learning something new.
In regards to Guru Dan, I think he really is a student at heart. He had no problem regailing us with tales of his misfortunes, mistakes and total screw ups. Some where funny, and some were good lessons to be learnt. He is also happy to start with a white belt again, and learn something totally new, even if he has decades of experience in that area. There is a man who eats his own dogfood.
So to Guru Dan - thank you for the valuable lesson, and thank you for a great seminar.
To everyone else, have a Happy Holidays and New Years, and hopefully this post has also provided you with something to think about, much like it did me.
This totally defeats all purposes of OO encapsulation, and a variety of other principles, but it was a fun time to play around with.
Basically, i wanted to see if I could turn a Struct into a Bean object on the fly. Strangely enough, it was pretty simple considering the flexibility you have with functions and CFCs.
The code works out like follows:
<cfscript>
//helper functions
function identityHashCode(object)
{
var system = createObject("java", "java.lang.System");
return system.identityHashCode(arguments.object);
}
function setInstance(instance)
{
variables.instance = arguments.instance;
}
</cfscript>
<cffunction name="structToObject" hint="Converts a structure to a object with getters and setters" access="public" returntype="WEB-INF.cftags.component" output="false">
<cfargument name="inputStruct" hint="Struct to convert" type="struct" required="Yes">
<cfscript>
var obj = createObject("component", "WEB-INF.cftags.component");
var lKeys = StructKeyList(arguments.inputStruct);
var hash = identityHashCode(arguments.inputStruct);
var key = ""; var path = GetDirectoryFromPath(CGI.PATH_TRANSLATED);
var nl = chr(10) & chr(13);
var buffer = createObject("java", "java.lang.StringBuffer").init("<cfscript>#nl#");
//move variable scope to inside the CFC
obj.setInstance = setInstance;
obj.setInstance(arguments.inputStruct);
StructDelete(obj, "setInstance");
</cfscript>
<cfloop list="#lKeys#" index="key">
<!--- write out the function to get and set --->
<cfscript>
buffer.append("function get#key#() { return instance.#key#; }#nl#");
buffer.append("function set#key#(#key#) { instance.#key# = arguments.#key#; }#nl#");
</cfscript>
</cfloop>
<cfscript>
buffer.append("</cfscript>");
</cfscript>
<!--- make the file - use the hash to ensure uniqueness --->
<cffile action="write" file="#path##hash#.functions" output="#buffer.toString()#">
<!--- import the file --->
<cfinclude template="#hash#.functions">
<!--- delete the file --->
<cffile action="delete" file="#path##hash#.functions">
<!--- add it to the object --->
<cfloop list="#lKeys#" index="key">
<cfscript>
obj["get#key#"] = variables["get#key#"];
obj["set#key#"] = variables["set#key#"];
</cfscript>
</cfloop>
<cfreturn obj>
</cffunction>
To test this out:
<!--- test code --->
<cfscript>
myStruct = StructNew();
myStruct.fred = "Fred";
obj = structToObject(myStruct);
</cfscript>
<cfoutput>
#obj.getFred()#
<hr>
<cfscript>
obj.setFred("blue");
</cfscript>
#obj.getFred()#
</cfoutput>
Essentially this function:,
- Creates a basic CFC (WEB-INF.cftags.component is the base CFC for all CFCs),
- Adds the 'setInstance()' function to the CFC, and sets the Struct to the instance data of the CFC with it,
- Removes 'setInstance()' function from the CFC,
- The function loops through the Struct Keys,
- It writes a series of getter and setter functions to an external file,
- Imports in the file, placing these functions into the variables scope,
- Deletes the file (cleanup!)
- Adds all these functions to the CFC,
- Returns the CFC.
And look at that, we have created a CFC at run time! Totally defeats some core OO concepts, but it was interesting to note that you can do it.
I'm really starting to get annoyed with all the spiders I am getting through my site, simply so that they can put referrers in my web logs.
It wouldn't be such a big deal, considering it's very easy for me to pick out which referrers are real and which aren't pretty fast. The issue comes from the fact that they are badly written, so spider my links badly, and send URL requests that send me about 10-15 error message emails a day!
They all use a Internet Explorer User Agent, so I can't stop them all that way, I've tried banning people by IP, but they change that often it't almost not worth it.
I'm half considering puting some sort of CAPTCHA on my site just to kill these bots... but that would stop every other good bot too (i.e. Google).
Any thoughts anyone?
Btw - sorry about the lack of posts, I'm currently in the process of rewriting CT using a new OO framework idea I've been toying around with, but more on that later...