Monday, February 18, 2013
Java Reader Update
I just updated my old Java Squeak project to SS3 and Pharo. It now works with Pharo 2.0. You can find it here. If you find any bugs, let me know. I plan on doing more with it in the future. Stay tuned. For a quick reminder, it allows you to load Java class files and read in serialized streams. The only drawback right now is if the Java objects have customized read/write methods. But, I'm working on a solution for that. There are tests to make sure all major functionality works.
Friday, June 22, 2012
Pomodoro Technique
I was reading Robert Martin's excellent "Clean Coder" and he mentions a productivity tool called the "Pomodoro Technique". The premise is simple. Work for 25 minutes, then take a 5 minute break. During a 25 minute period (called a Pomodoro), you concentrate on one and only one task. If you are interrupted, you deal with it once the 25 minutes is up. Sounded simple and I was looking for a way to increase my productivity.
The first day that I tried it, I was enlightened and shocked. First, I was amazed how much progress I could make on a task in 25 minutes. I loved the 5 minute breaks for short walks. But, I was shocked how little productive time I got done in that first day. I only got through four Pomodoros. The rest of my day was meetings and helping my team mates. But, in that time, I got more accomplished than the previous day.
I kept doing my work for the rest week using this simple technique. I discovered that I loved working this way. First, it was an effective way to manage interruptions. Instead of addressing an interruption immediately, I would simply delay until the end of the current Pomodoro. After a few days, everyone got the swing of it and supported me. Another one was taking the break in the middle of a task that spanned multiple Pomodoros. At first, it was hard and wanted to keep on working. But, I decided to stick with the program. What I found was that a lot of times, just the 5 minutes to walk away when I came back, my work would take surprising turns. It forced me to evaluate where I wanted to go at the beginning of the next Pomodoro. By the end of the week, I was getting more work done, tracking better on my estimates, and felt better about the work I was getting done even the number of Pomodoros was low. I was marking more tasks off my to do list than ever before.
The next week I decided to read Pomodoro Technique Illustrated and the free ebook. And I made adjustments to how I was doing it based on what was in those books. When I got in the morning, I looked at my to do list and prioritized what I needed to get done that day. Next, I estimated how many Pomodoros it would take to complete the task. If the task was longer than 4 Pomodoros, I broke it up into smaller tasks. What I found out by doing this is that I could make head ways via 25 minute chunks on multiple projects. I could spend a Pomodoro on a bug as a break if I wanted to to help break up the monotony of a long task. I also noticed that I was getting more Pomodoros done because I wasn't jumping up at every interruption.
Every day I'm tweaking how I work within such a simple constraint of working for 25, break for 5. In conclusion, the Pomodoro Technique will forever be something that I do to track work. I've been amazed how much it has helped me thus far. I love that it's easy to track time on my projects, forces me to take mental breaks and exercise, breaks my flow in a good way, gives a graceful way to handle interruptions, allows me to reassess my work, and is simple. I've even started to using it on my spare time projects, so I can better allocate time to the things that matter to me. I've even playing around with the 30/30 off-shoot as well to handle free time and working on household chores. But, that is another blog post.
The first day that I tried it, I was enlightened and shocked. First, I was amazed how much progress I could make on a task in 25 minutes. I loved the 5 minute breaks for short walks. But, I was shocked how little productive time I got done in that first day. I only got through four Pomodoros. The rest of my day was meetings and helping my team mates. But, in that time, I got more accomplished than the previous day.
I kept doing my work for the rest week using this simple technique. I discovered that I loved working this way. First, it was an effective way to manage interruptions. Instead of addressing an interruption immediately, I would simply delay until the end of the current Pomodoro. After a few days, everyone got the swing of it and supported me. Another one was taking the break in the middle of a task that spanned multiple Pomodoros. At first, it was hard and wanted to keep on working. But, I decided to stick with the program. What I found was that a lot of times, just the 5 minutes to walk away when I came back, my work would take surprising turns. It forced me to evaluate where I wanted to go at the beginning of the next Pomodoro. By the end of the week, I was getting more work done, tracking better on my estimates, and felt better about the work I was getting done even the number of Pomodoros was low. I was marking more tasks off my to do list than ever before.
The next week I decided to read Pomodoro Technique Illustrated and the free ebook. And I made adjustments to how I was doing it based on what was in those books. When I got in the morning, I looked at my to do list and prioritized what I needed to get done that day. Next, I estimated how many Pomodoros it would take to complete the task. If the task was longer than 4 Pomodoros, I broke it up into smaller tasks. What I found out by doing this is that I could make head ways via 25 minute chunks on multiple projects. I could spend a Pomodoro on a bug as a break if I wanted to to help break up the monotony of a long task. I also noticed that I was getting more Pomodoros done because I wasn't jumping up at every interruption.
Every day I'm tweaking how I work within such a simple constraint of working for 25, break for 5. In conclusion, the Pomodoro Technique will forever be something that I do to track work. I've been amazed how much it has helped me thus far. I love that it's easy to track time on my projects, forces me to take mental breaks and exercise, breaks my flow in a good way, gives a graceful way to handle interruptions, allows me to reassess my work, and is simple. I've even started to using it on my spare time projects, so I can better allocate time to the things that matter to me. I've even playing around with the 30/30 off-shoot as well to handle free time and working on household chores. But, that is another blog post.
Wednesday, October 26, 2011
Lost two of my heros
This month I lost two of my heros: Steve Jobs and John McCarthy. As a child, I dreamed of being Steve Jobs. I admired him for what he accomplished and his impeccable showmanship. I learned so much from him and his vision. I'm sad that I will never be able to meet him.
When I got older and learned about how Xerox Parc and John Engelbart inspired the Mac, I found Lisp. My love affair with Lisp goes back to college when I didn't quite understand it, but thought it was strange yet elegant. The light eventually went on and the true power of macros and functional programming was eventually revealed. To this day, there is a source of amazement that comes with Lisp and I thank John McCarthy for that. I wish I could have met him as well.
I can't express how I much these men touched my life without ever meeting them. They both hit the ball so far out of the park that they will be revered for generations to come. The bar has been set and I plan on standing on their shoulders. Thank you.
When I got older and learned about how Xerox Parc and John Engelbart inspired the Mac, I found Lisp. My love affair with Lisp goes back to college when I didn't quite understand it, but thought it was strange yet elegant. The light eventually went on and the true power of macros and functional programming was eventually revealed. To this day, there is a source of amazement that comes with Lisp and I thank John McCarthy for that. I wish I could have met him as well.
I can't express how I much these men touched my life without ever meeting them. They both hit the ball so far out of the park that they will be revered for generations to come. The bar has been set and I plan on standing on their shoulders. Thank you.
Saturday, June 04, 2011
The days of templates are over
When I first started to work with web applications, it was just servlets and then came JSPs. At the time, I didn't like JSPs (and to this day I still don't). They seemed like a necessary evil that got the job done. The thing I found repulsive about them was the mix of server and client code together. The switching of contexts just made both look ugly and hard to maintain. But, it was easier than generating pages directly from servlets by hand. Of course, there were other solutions that I looked for, but nothing got away from the horrid markup. Then, I fell in love with Seaside which did away with markup and generated content through a custom DSL. It felt better to me and loved it until I realized that it was only for the developer, not the web page designer. It made their life harder. Now, we all know we should use minimal content and have CSS do the layout. But, in this modern world, you need folks who are experts at web page design. You can not and should not alienate them.
Fast forward to now. Currently, I've been using Javascript frameworks and working mainly from the front-end. The project I'm currently working on is pure HTML/Javascript/CSS for the front-end with RESTful calls to the backend. I must admit, I had my reservations about this arrangement, but after working with it; it is the right thing to do. No more ugly markup. The separation of concerns is well in place. And finally, the web designer is allowed to concentrate on making a rich user experience and the developer to concentrate on business logic. Perfect.
My feeling is that the days of template engines are over. It's a waste of time and all of them are a pain to debug. A pure Web 2.0 AJAX solution allows each to utilize their tools and do things separately. To me that is exciting and I will gladly leave ugly code behind.
Fast forward to now. Currently, I've been using Javascript frameworks and working mainly from the front-end. The project I'm currently working on is pure HTML/Javascript/CSS for the front-end with RESTful calls to the backend. I must admit, I had my reservations about this arrangement, but after working with it; it is the right thing to do. No more ugly markup. The separation of concerns is well in place. And finally, the web designer is allowed to concentrate on making a rich user experience and the developer to concentrate on business logic. Perfect.
My feeling is that the days of template engines are over. It's a waste of time and all of them are a pain to debug. A pure Web 2.0 AJAX solution allows each to utilize their tools and do things separately. To me that is exciting and I will gladly leave ugly code behind.
App Dev Today
I'm pleased to announce the start of a new venture. It's a new company to launch new mobile applications. The ball is just starting to roll and exciting times are ahead. We have some cool stuff up our sleeves and will be showing soon.
Monday, April 04, 2011
Easier Memory Release In Objective C with Blocks
I've been getting my head around Objective C recently and remembering all that is great about about manual memory management. I found myself repeating code that either a) autoreleased or b) released with try finally loops. The autorelease puts a drag on your pool so you want to use it sparingly (with APIs where you don't know who will be calling it) and it's mentioned in several places in the Apple documentation to favor "release" over "autorelease". So, that leaves me with option b in most of my code with a lot of boilerplate code. And I hate boilerplate code. Well, did some more reading and found out that blocks are a new thing in Objective C and I quickly whipped this up:
typedef void (^Monadic)(id value);
typedef id (^Niladic)();
-(void)use: (Niladic)calc during:(Monadic)monadic {
id toUse = calc();
@try {
monadic(toUse);
}
@finally {
[toUse release];
}
}
Basically, it allows you to send in one block to get the allocated object and then use it in a try finally. It automatically cleans up after itself! I then decided to try it out on some code:-(IBAction)calculateClicked:(id) sender {
[self
use: ^() { return [[Cents alloc] initWithString: [startView text]]; }
during: ^(id startAmount) {
[self
use: ^() { return [[Cents alloc] initWithString: [eachYearAmountView text]]; }
during: ^(id eachYearAmount) {
[self
use: ^() { return [[NSDecimalNumber alloc] initWithString: [interestView text]]; }
during: ^(id years) {
Cents *answer = [startAmount
atRetirementAdd: eachYearAmount
earning: years
for: [[yearsView text] intValue]];
[resultView setText: [answer description]];
} ];
} ];
} ];
}
I think I made it too generic in that I now have to type "^() { return [blah blah blah]; } every time. I realized I could get rid of those blocks and just pass in the object I want to clean up. Remove Niladic block and take two looks like this:typedef void (^Monadic)(id value);
-(void)use:(id)toUse during:(Monadic)monadic {
@try {
monadic(toUse);
}
@finally {
[toUse release];
}
}
And this is what it looks like when you use it:-(IBAction)calculateClicked:(id) sender {
[self
use: [[Cents alloc] initWithString: [startView text]]
during: ^(id startAmount) {
[self
use: [[Cents alloc] initWithString: [eachYearAmountView text]]
during: ^(id eachYearAmount) {
[self
use: [[NSDecimalNumber alloc] initWithString: [interestView text]]
during: ^(id years) {
Cents *answer = [startAmount
atRetirementAdd: eachYearAmount
earning: years
for: [[yearsView text] intValue]];
[resultView setText: [answer description]];
} ];
} ];
} ];
}
As long as the Monadic blocks stay on the stack we don't need to do the copy autorelease business on block objects. I like this code better than nested try finally code because I'm not repeating the name of my variable to release in the clean up code. I'm still not 100% happy with the code, but I'll keep playing around with other things until then. I do like the fact that it is simple. Just something I wanted to share as I learn Objective C.
Friday, March 11, 2011
Singleton Pattern in Dojo
Recently, I needed to have a singleton object. So, I came up with the following implementation to give me just that.
When declaring my class in Dojo, I did the following:
var makeSingleton = function (aClass) {
aClass.singleton = function () {
var localScope = arguments.callee;
localScope.instance = localScope.instance || new aClass();
return localScope.instance;
};
return aClass;
};
When declaring my class in Dojo, I did the following:
makeSingleton(dojo.declare(...));Then, to use it:
var instance = myClass.singleton();I like the fact with this implementation that it's obvious that I'm creating a singleton object up front around the dojo.declare. Of course, there's nothing preventing someone from creating the class with new, but we could easily get around that with throwing an exception in our constructor after our singleton has been initialized.
Saturday, January 22, 2011
Functional Programming In Python 2
I gave a talk this month to the Tampa Bay Python User Group on functional programming in Python. It's actually the second part of the talk. I've put the slides up for all to enjoy here. If you have any questions or complaints, feel free to contact me. I welcome all comments.
Collaboration Not Pairing
One of the tenants of Extreme Programming is pairing on all production code. At first, it seems to make perfect sense. Four eyes on all code is better than two, right? There is another partner to help solve issues, take over when one is tired, and to keep each other honest. What could be wrong with that? Plenty. I feel that pairing is good in spurts of time or as I like to say, "Collaborate not pair".
I want someone to help me on the hard problems. I don't need someone peering over my shoulder to make sure I'm coding a for loop correctly. I think big picture. I want a pair to help with the design and make sure it is sound. The physical act of coding might have some hard problems too and I would like a pair for that as well. But, for the mundane boilerplate code, I don't need to watch someone type that.
I believe you need a mix of alone time on a problem and together time to exchange ideas. Pairing constantly stops that and it's easy to fall prey to a stronger partner's ideas even if those ideas are bad. The stronger personality wins. This should not be the case. But, I've seen it many times on XP projects. A pair put into production code that was clearly not good and hard to maintain. The very thing pairing is supposed to prevent.
This doesn't mean I don't want four eyes on all code either. I think a collaborator should read over every line of code that is written in isolation and ask questions. This is where time away is helpful. The partner brings an outside perspective than if the pair was sitting side by side. This prevents group think or one partner overbearing another. There's also the case where some developers are intimidated working with someone all of the time. They need to have space to properly think. These developers can be prone to letting a stronger partner always have their way. Again, not what we want.
As you can see, I'm not against pairing, but against constant pairing. Collaborate when necessary and make sure to leave time to think in isolation. You need both. You can't be all one side or the other. Either one all of the time is not healthy. But, how much collaboration versus alone time is needed? This is where you have to use your best judgement. Agility is about thinking and finding the right solution for the right situation. It's not a one size fits all world.
I want someone to help me on the hard problems. I don't need someone peering over my shoulder to make sure I'm coding a for loop correctly. I think big picture. I want a pair to help with the design and make sure it is sound. The physical act of coding might have some hard problems too and I would like a pair for that as well. But, for the mundane boilerplate code, I don't need to watch someone type that.
I believe you need a mix of alone time on a problem and together time to exchange ideas. Pairing constantly stops that and it's easy to fall prey to a stronger partner's ideas even if those ideas are bad. The stronger personality wins. This should not be the case. But, I've seen it many times on XP projects. A pair put into production code that was clearly not good and hard to maintain. The very thing pairing is supposed to prevent.
This doesn't mean I don't want four eyes on all code either. I think a collaborator should read over every line of code that is written in isolation and ask questions. This is where time away is helpful. The partner brings an outside perspective than if the pair was sitting side by side. This prevents group think or one partner overbearing another. There's also the case where some developers are intimidated working with someone all of the time. They need to have space to properly think. These developers can be prone to letting a stronger partner always have their way. Again, not what we want.
As you can see, I'm not against pairing, but against constant pairing. Collaborate when necessary and make sure to leave time to think in isolation. You need both. You can't be all one side or the other. Either one all of the time is not healthy. But, how much collaboration versus alone time is needed? This is where you have to use your best judgement. Agility is about thinking and finding the right solution for the right situation. It's not a one size fits all world.
Friday, January 21, 2011
Domain Driven Design Immersion
I've been meaning to write about my experience at the Domain Driven Design Immersion training that I took back in November. But, I've been so busy that I haven't had the time to put down my thoughts on it properly until today. First off, I had a great time; met many wonderful and intelligent people; and learned quite a few new techniques. Just given that, it was well worth the time. The course was simply fantastic.
I read Domain-Driven Design
when it first came and fell in love with it. I've read it several times and consider it one of the greatest books on design. It's a tome of knowledge on being practical in design and getting closer to mapping user language to code. There are no silver bullets, just great advice and a road map of thought to get there. Despite the book being well-written, there were questions I've had when applying DDD throughout the years. I jumped at the chance to spend a week immersed in DDD and learn a few more tricks.
The training was excellent: well-paced, interactive, and loads of discussion. DDD is something that you must practice and the training provides a lot of it. From hands on coding to white board design sessions, you get your mind in the middle of the whole process. The trainers guided discussion and encouraged us to be creative in our solutions: messy whiteboards are good! I've been to a lot of training and seminars. Normally, little thought was given to pacing of the material. This was not the case with DDD Immersion. I felt there was an excellent mix of lecture, hands-on, and discussions. Each day felt like it went by too fast. Four days is not a lot of time to cram the amount of material that the DDD book covers, but the course materials provided abbreviated documents to bullet point the important ideas. I personally thought the materials made the course all worth it.
Even if you have read the DDD book, I still think the training is worth it's weight in gold. I work with Eric on his Time and Money library and I still got new ideas out of it. The training does add new material to the book and expands on Eric's new ideas concerning DDD.
The one thing that I took from the training is it's all about practicing to get better at design. Eric has laid out a path of thought on design. It still takes ingenuity and playing around with ideas to be successful. Design is a creative process and it takes hard thinking to match the language of users into code. The training brings that home. I whole heartily recommend the training and would go again given the chance. The real world keep giving me problems where questions arise to the best way to tackle them. You can never stop learning.
I read Domain-Driven Design
The training was excellent: well-paced, interactive, and loads of discussion. DDD is something that you must practice and the training provides a lot of it. From hands on coding to white board design sessions, you get your mind in the middle of the whole process. The trainers guided discussion and encouraged us to be creative in our solutions: messy whiteboards are good! I've been to a lot of training and seminars. Normally, little thought was given to pacing of the material. This was not the case with DDD Immersion. I felt there was an excellent mix of lecture, hands-on, and discussions. Each day felt like it went by too fast. Four days is not a lot of time to cram the amount of material that the DDD book covers, but the course materials provided abbreviated documents to bullet point the important ideas. I personally thought the materials made the course all worth it.
Even if you have read the DDD book, I still think the training is worth it's weight in gold. I work with Eric on his Time and Money library and I still got new ideas out of it. The training does add new material to the book and expands on Eric's new ideas concerning DDD.
The one thing that I took from the training is it's all about practicing to get better at design. Eric has laid out a path of thought on design. It still takes ingenuity and playing around with ideas to be successful. Design is a creative process and it takes hard thinking to match the language of users into code. The training brings that home. I whole heartily recommend the training and would go again given the chance. The real world keep giving me problems where questions arise to the best way to tackle them. You can never stop learning.
Thursday, October 14, 2010
Functional Python Talk In Tampa
Here's my presentation from last night for anyone that missed it.
I mentioned several books and articles as well.
Peter Norvig's "Tutorial On Good Lisp Programming Style"
Thomas Kuhne's "Functional Pattern System For Object-Oriented Style"
I mentioned several books and articles as well.
Peter Norvig's "Tutorial On Good Lisp Programming Style"
Thomas Kuhne's "Functional Pattern System For Object-Oriented Style"
Sunday, August 29, 2010
Imitation and Repetition
I was recently reading Alice Cooper, Golf Monster: A Rock 'n' Roller's 12 Steps to Becoming a Golf Addict
and had an epiphany. In one of the chapters Alice talks about the way to be great at anything. He explains one should mimic the greats by watching and then repeat what they do until it's natural. I immediately started to think about programming, of course. We all have programmers that we admire. For me, most of my admired heros are great writers. It's their advice that I adore. I soak their words in and then practice what they talk about. But, it's not the same as reading code. What Alice was talking about was studying what the greats do and repeating it until it's as natural as the way you breathe. Where can we find great code? I always felt my programming took a huge leap forward with Smalltalk. The reason I think that is now crystal clear: Smalltalk encourages you to read other people's code. All of the tools embrace reading code. If you don't know how to do something, you can search via the tools to find something similar and then repeat what they did. There is a plethora of patterns and good programming in any Smalltalk system. All of the GoF patterns came from Smalltalk originally.
That's fine and dandy, but what about Java, Python, Ruby, or other developers? It's just as easy as finding an open source framework you love and diving head first into the code. Study it, practice writing in that style, and repeat until it feels natural. Open source gives us opportunities that only Smalltalk and Lisp did in the past: a limitless supply of software to study in any language we choose. But, this begs the question: How do I know I'm studying good code? Alice touches on this in his book by saying that as you pass milestones in competence, you know what the next level looks like. Start with code that looks good to you now and keep practicing. For me, I strive for readability. How can I remove all noise until the code I write is nothing but the language of the problem I'm trying to solve. I'm always reading code to find new techniques for removing noise. I study code written in different languages and see how I can apply any new techniques in the language I am currently using. I've found a lot of gems by studying from functional programming and applying them to object-oriented languages.
This brings up another interesting observation. If it's good to read code and practice writing it, when should you use a new technique? For me, I always practice a new technique a lot before I apply it to production code. Normally, once a technique feels natural and I know it like the back of my hand. Most of the open source libraries I've written have been to explore an idea that I saw somewhere else to play around with it. There's nothing worse than not having mastery and immediately applying it because it was new at the time. Be patient. Read, code, repeat. Always be reading and practicing. It's the only way to get better.
That's fine and dandy, but what about Java, Python, Ruby, or other developers? It's just as easy as finding an open source framework you love and diving head first into the code. Study it, practice writing in that style, and repeat until it feels natural. Open source gives us opportunities that only Smalltalk and Lisp did in the past: a limitless supply of software to study in any language we choose. But, this begs the question: How do I know I'm studying good code? Alice touches on this in his book by saying that as you pass milestones in competence, you know what the next level looks like. Start with code that looks good to you now and keep practicing. For me, I strive for readability. How can I remove all noise until the code I write is nothing but the language of the problem I'm trying to solve. I'm always reading code to find new techniques for removing noise. I study code written in different languages and see how I can apply any new techniques in the language I am currently using. I've found a lot of gems by studying from functional programming and applying them to object-oriented languages.
This brings up another interesting observation. If it's good to read code and practice writing it, when should you use a new technique? For me, I always practice a new technique a lot before I apply it to production code. Normally, once a technique feels natural and I know it like the back of my hand. Most of the open source libraries I've written have been to explore an idea that I saw somewhere else to play around with it. There's nothing worse than not having mastery and immediately applying it because it was new at the time. Be patient. Read, code, repeat. Always be reading and practicing. It's the only way to get better.
Thursday, May 13, 2010
Partial With Frozen Arguments In Any Position
Recently, I was working on a problem where I wish I could have frozen arguments using a partial, but could not. The reason was that I needed to freeze the last argument instead of the first. Here's a simple example of what I wanted my function to behave like:
Now, I'm not excited about the implementation, but it satisfies my initial requirements. I'm going to continue to work on this to come up with something more elegant, but for now it works.
def contains_three(string): return '3' in stringBut, I wanted to define it like so:
contains_three = functools.partial(operator.__contains__, '3')But, partial only freezes the first arguments and you can't mix and match. I've ran into this before and I thought it would be nice to define a partial function that would allow you to freeze arguments in any position. Here's my implementation:
def custom_partial(func, *const_args, **const_kwds):
def result(*args,**kwds):
args_iter=iter(args)
def convert(value):
if value is None:
return args_iter.next()
else:
return value
to_call = map(convert, const_args)
new_kwds = const_kwds.copy()
new_kwds.update(kwds)
return func(*(to_call + list(args_iter)), **new_kwds)
return resultIs there a test? Of course, there is...from operator import __contains__
import unittest
class Test(unittest.TestCase):
def testContains(self):
has_three = custom_partial(__contains__, None, '3')
self.assertTrue(has_three('1234'))
self.assertFalse(has_three('124'))By looking at the test and code, the implementation I chose was to have the value of None to denote any argument I don't want to freeze. I could have created a placeholder value for it, but decided since I haven't had a need to freeze an argument with None to just leave it.Now, I'm not excited about the implementation, but it satisfies my initial requirements. I'm going to continue to work on this to come up with something more elegant, but for now it works.
Saturday, October 17, 2009
Devilishly Clever
I was doing my usual research on Python when I ran across this recipe to make tail recursive calls not blow the stack in Python: Tail Call Optimization Decorator. I read through the code and thought, "Wow! This is devilishly clever!" As a thought exercise, I think it's awesome. But, it got me thinking about clever and production code. In my opinion, clever is never good or wanted in production code. It's great to learn and understand clever code though. It's a great mental workout to keep you sharp.
So, what's my point with all of this besides to say that clever is bad? The example in the above link is factorial (which as everyone knows is hated by me, but that's another story). But, the amazing thing about factorial examples are that they are dead simple, yet there's several ways to get the answer. Here's a few that I came up with:
Each of these compute factorial. Amazingly, there's even more ways than what I listed (the math wizards in the audience know what they are). Now, think about this: Computing factorial is dead simple. What happens when we get to harder problems? Being clever can actually get in the way of making the code easy to understand. It might even kill performance. Let's think back to the devilishly clever code. The performance of making Python tail recursive is awful. Sure, it's pure and tail recursive, but in production code that is deadly. What we want is simple and to the point. It's why I generally like solutions that need less code. There's less noise to get in the way of understanding and generally can mean better performance as well.
Real world problems are hardly ever as straightforward as factorial. The balancing act comes when you drop a solution because it's not working for whatever reason. Raising and catching exceptions so you can have a tail recursive factorial is overkill. It took more code than the non-recursive version. It begs for the programmer to know their tool set and to know how to solve problems in that tool set that are straightforward. Tail recursion is powerful in languages like Haskell and Erlang. But, there's always another way of doing things that can make more sense in the language you are using. In our case, the other ways were just as easy yet more scalable for our tool set. Food for thought the next time you go down the path of clever and end up writing more noise than solution.
So, what's my point with all of this besides to say that clever is bad? The example in the above link is factorial (which as everyone knows is hated by me, but that's another story). But, the amazing thing about factorial examples are that they are dead simple, yet there's several ways to get the answer. Here's a few that I came up with:
def reg_fact(x):
if x is 1:
return 1
return reg_fact(x - 1) * x
def tail_fact(x):
def func(acc, x):
if x is 1:
return acc
return func(acc * x, x - 1)
return func(1, x)
def not_rec_fact(x):
result = 1
for each in range(2, x + 1):
result *= each
return result
def not_rec_fact_fancy(x):
return reduce(lambda result, each: result * each, range(1, x + 1))
import operator
def not_rec_fact_super_fancy(x):
return reduce(operator.mul, range(1, x + 1))
Each of these compute factorial. Amazingly, there's even more ways than what I listed (the math wizards in the audience know what they are). Now, think about this: Computing factorial is dead simple. What happens when we get to harder problems? Being clever can actually get in the way of making the code easy to understand. It might even kill performance. Let's think back to the devilishly clever code. The performance of making Python tail recursive is awful. Sure, it's pure and tail recursive, but in production code that is deadly. What we want is simple and to the point. It's why I generally like solutions that need less code. There's less noise to get in the way of understanding and generally can mean better performance as well.
Real world problems are hardly ever as straightforward as factorial. The balancing act comes when you drop a solution because it's not working for whatever reason. Raising and catching exceptions so you can have a tail recursive factorial is overkill. It took more code than the non-recursive version. It begs for the programmer to know their tool set and to know how to solve problems in that tool set that are straightforward. Tail recursion is powerful in languages like Haskell and Erlang. But, there's always another way of doing things that can make more sense in the language you are using. In our case, the other ways were just as easy yet more scalable for our tool set. Food for thought the next time you go down the path of clever and end up writing more noise than solution.
Saturday, October 10, 2009
Freezing Objects in Python
Someone stop me. I like freezing simple objects or what Eric Evans calls "Value Objects" in his excellent "Domain Driven Design" book. Python doesn't have immutable objects (ala freeze in Ruby) explicitly, but we can easily create it. Python gives us the power to get under its covers. Here's my implementation:
Do you have to ask? Yes, I made a test. Here they are:
class ValueObject(object):
def __setattr__(self, name, value):
if name == 'value' and hasattr(self, 'value'):
raise AttributeError("Can not change value attribute")
else:
self.__dict__[name] = value
Do you have to ask? Yes, I made a test. Here they are:
import unittest
class Test(unittest.TestCase):
def testSimple(self):
class Cents(ValueObject):
def __init__(self, value):
self.value = value
def __str__(self):
return str(self.value) + " cents"
subject = Cents(5)
self.assertEquals(5, subject.value)
def set_value():
subject.value = 6
self.failUnlessRaises(AttributeError, set_value)
Monday, October 05, 2009
method_missing in Python
OK, I know there is a few implementations of this knocking around on the net already, but I'm in learning mode. So, here's my implementation of Ruby's method_missing:
Three objects and that's it! One to be a placeholder when an argument is missing. One object to represent the MethodMissingError and the last to be an abstract class to inherit from. But, you could easily just put these methods anywhere. Here's my tests:
You've seen my implementation. Here's another one that I found on the net: method_missing in Python
Um, this version is much shorter than mine. It gets the same job done and is more obvious of what it is doing. Simply returning a function on __getattr__ is the best way to go. It's what my PossibleArgumentMissing was doing. But, where I tried to get all fancy using __call__ to mimic a function which caused my version to be longer. I was also trying to get the argument to come back to return false in if conditions and be almost like None (again, I'm learning and was looking at what was possible). Simplest is best.
This was a fun thought exercise. I love all the hooks Python provides to allow you to get underneath the hood if need be. I will be soon doing a post on descriptors and even decorators. I find Python tends to sway heavier on the functional side of programming versus object-oriented. I'm loving the succinct, yet readable code. Too many languages get into the being so terse that they sacrifice readability or debugability (is that a word? Is now!).
class PossibleMissingAttribute(object):
def __init__(self, object, name):
self._object = object
self._name = name
def __call__(self, *args, **kwargs):
return self._object._method_missing(self._name, *args, **kwargs)
def __str__(self):
return self.__class__.__name__ + ": " + str(self._object) + "." + self._name
def __getattr__(self, name):
return None
def __nonzero__(self):
return False
class MethodMissingError(Exception):
def __init__(self, object, name, *args, **kwargs):
self.object = object
self.name = name
self.args = args
self.kwargs = kwargs
def __str__(self):
return repr(str(self.object) + "." + self.name)
class Missing(object):
def __getattr__(self, name):
return PossibleMissingAttribute(self, name)
def _method_missing(self, name, *args, **kwargs):
raise MethodMissingError(self, name, *args, **kwargs)
Three objects and that's it! One to be a placeholder when an argument is missing. One object to represent the MethodMissingError and the last to be an abstract class to inherit from. But, you could easily just put these methods anywhere. Here's my tests:
import unittest
class TestMissing(unittest.TestCase):
def test_missing(self):
class TestClass(Missing):
def __str__(self):
return self.__class__.__name__
def existing(self):
return "i am"
def _method_missing(self, name, *args, **kwargs):
return args[0]
self.assertEquals("am not", TestClass().missing("am not"))
self.assertEquals("i am", TestClass().existing())
self.assertEquals(None, TestClass().missing.something_else_missing)
self.assertFalse(TestClass().missing)
self.assertEquals("PossibleMissingAttribute: TestClass.missing", str(TestClass().missing))
def test_exception(self):
class TestClass(Missing):
def __str__(self):
return self.__class__.__name__
self.failUnlessRaises(MethodMissingError, lambda: TestClass().missing())
You've seen my implementation. Here's another one that I found on the net: method_missing in Python
class MethodMissing(object):
def method_missing(self, attr, *args, **kwargs):
“”" Stub: override this function “”"
raise AttributeError(”Missing method %s called.”%attr)
def __getattr__(self, attr):
def callable(*args, **kwargs):
return self.method_missing(attr, *args, **kwargs)
return callable
Um, this version is much shorter than mine. It gets the same job done and is more obvious of what it is doing. Simply returning a function on __getattr__ is the best way to go. It's what my PossibleArgumentMissing was doing. But, where I tried to get all fancy using __call__ to mimic a function which caused my version to be longer. I was also trying to get the argument to come back to return false in if conditions and be almost like None (again, I'm learning and was looking at what was possible). Simplest is best.
This was a fun thought exercise. I love all the hooks Python provides to allow you to get underneath the hood if need be. I will be soon doing a post on descriptors and even decorators. I find Python tends to sway heavier on the functional side of programming versus object-oriented. I'm loving the succinct, yet readable code. Too many languages get into the being so terse that they sacrifice readability or debugability (is that a word? Is now!).
Subscribe to:
Posts (Atom)

