background preloader

Elegant Code

Elegant Code

TheStrategyTemplatePattern - Pattern Repository Wiki From Pattern Repository Wiki Motivation You have redundant structure in the forms of switches spread across several existing classes and methods. Encapsulation The Strategy Template pattern encapsulates the type of object being used from all of the switches involved. Procedural Analog We're replacing a series of ifs with a group of related strategies. Non-Software Analog When I teach a course there are many similar things I have to do - but how I do them depends upon the situation I am in. * check with course owner * get manuals ready * get directions for course * check out course layout * put out materials to name a few. * course at our offices * public course * course out of town * course at a local company Using this structure gives me a template for the actions, based (organized) around the cases I have. Implementation Forces Number of switches. Example Problem Example Solution Cost-Benefit (gain-loss) Notice how Foo becomes much more readable.

Charming Python: Using state machines What is Python? Python is a free, high-level, interpreted language developed by Guido van Rossum. It combines a clear syntax with powerful (but optional) object-oriented semantics. Python is widely available and highly portable. Back to top What is a state machine? An overly accurate description of a state machine is that it is a directed graph, consisting of a set of nodes and a corresponding set of transition functions. But an abstract mathematical description (like the one I just gave) does not really illustrate what use a state machine might serve in practical programming problems. Let's try using an example more appropriate to the actual task at hand. Following this example, let's look at a practical heuristic definition of a state machine. The state machines discussed in this article are high-level machines intended to demonstrate a programming solution to a class of problems. A text-processing state machine myObject = SomeClass(this, that, other) When not to use a state machine

Illustrated GOF Design Patterns in C# Part VI: Behavioral III - Download source files - 2 Kb Abstract Design Patterns, Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides [also known as the Gang of Four (GOF)] has been a de facto reference for any Object-Oriented software developer. This article is the conclusion of a series of articles illustrating the GOF Design Patterns in C#. Background In Design Patterns, each pattern is described with its name (and other well-known names); the motivation behind the pattern; its applicability; the structure of the pattern; class/object participants; participant collaborations; pattern consequences; implementation; sample code; known uses; and related patterns. A design pattern is not code, per se, but a "plan of attack" for solving a common software development problem. This article is meant to illustrate the design patterns as a supplement to their material. Behavioral Patterns Strategy many related classes differ only in their behavior. Template Method

Evidence Based Scheduling Evidence Based Scheduling by Joel Spolsky Friday, October 26, 2007 Software developers don’t really like to make schedules. Usually, they try to get away without one. “It’ll be done when it’s done!” Most of the schedules you do see are halfhearted attempts. Hilarious! You want to be spending your time on things that get the most bang for the buck. Why won’t developers make schedules? Over the last year or so at Fog Creek we’ve been developing a system that’s so easy even our grouchiest developers are willing to go along with it. The steeper the curve, the more confident you are that the ship date is real. Here’s how you do it. 1) Break ‘er down When I see a schedule measured in days, or even weeks, I know it’s not going to work. This forces you to actually figure out what you are going to do. If you are sloppy, and pick big three-week tasks (e.g., “Implement Ajax photo editor”), then you haven’t thought about what you are going to do. 2) Track elapsed time You can’t, really. Anyway. Summary

Refactoring Finds Dead Code One of the many things that I just love about my job as a consultant/mentor is when I actually get to sit down with programmers and pair program with them. This doesn’t seem to happen nearly as often as I would like, so when two developers at a recent client site asked me if I could look at some legacy code to see if I could figure out how to get some tests around it, I jumped at the opportunity. We acquired a room equipped with a projector and a whiteboard. I visit a lot of different companies and see a lot of absolutely ghastly code. As we walked through the code, I noticed that there were classes in the project directory ending in ‘Test’. The developers clarified that they wanted help in testing private methods. The question of testing private methods comes up frequently whether I’m teaching a class or consulting on a project. It looked like my work was done here. As I said earlier, the code wasn’t horrible. The overall structure of the method we were examining was

The Joel Test: 12 Steps to Better Code by Joel Spolsky Wednesday, August 09, 2000 Have you ever heard of SEMA? It's a fairly esoteric system for measuring how good a software team is. No, wait! The neat thing about The Joel Test is that it's easy to get a quick yes or no to each question. A score of 12 is perfect, 11 is tolerable, but 10 or lower and you've got serious problems. Of course, these are not the only factors that determine success or failure: in particular, if you have a great software team working on a product that nobody wants, well, people aren't going to want it. 1. 2. If the process takes any more than one step, it is prone to errors. For this very reason, the last company I worked at switched from WISE to InstallShield: we required that the installation process be able to run, from a script, automatically, overnight, using the NT scheduler, and WISE couldn't run from the scheduler overnight, so we threw it out. 3. Read more about daily builds in my article Daily Builds are Your Friend. 4. 5. 6. 7. 8. 9. 10.

The Guerrilla Guide to Interviewing (version 3.0) by Joel Spolsky Wednesday, October 25, 2006 A motley gang of anarchists, free-love advocates, and banana-rights agitators have hijacked The Love Boat out of Puerto Vallarta and are threatening to sink it in 7 days with all 616 passengers and 327 crew members unless their demands are met. The demand? A million dollars in small unmarked bills, and a GPL implementation of WATFIV, that is, the esteemed Waterloo Fortran IV compiler. (It’s surprising how few things the free-love people can find to agree on with the banana-rights people.) As chief programmer of the Festival Cruise programming staff, you’ve got to decide if you can deliver a Fortran compiler from scratch in seven days. Can you do it? “Well, I suppose, it depends,” you say. On what? “Um, will my team be able to use UML-generating tools?” Does that really matter? “I guess not.” OK, so, what does it depend on? “Will we have 19 inch monitors? Again, does this matter? “I guess not. Right. “Who are they?” Does that matter? “Sure! That’s it.

Related: