In reference to Steve McConnell's Code Complete and its reference to "Programming into a language" versus "Programming in a language" (see this blog entry by Jon Skeet if you don't have the book handy), I would like to know your favorite/best examples of where you programmed into a language. I'll include my own for good measure.
My current favorite example of this is for VB/ASP.NET web projects. I hate massive "if" statements in the page load and then trying to figure out the logic on postbacks. Therefore, I have created a convention to separate code that loads on first requests, post backs, and a section for both. Applying this to all aspx.vb files helps me keep sane regarding this. I put this code in even if the subs are empty, as it makes it extremely easy to alter the code later.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
handlePostBack(sender, e)
Else
handleFirstTimeHere(sender, e)
End If
' do this regardless
End Sub
Private Sub handlePostBack(ByVal sender As Object, ByVal e As System.EventArgs)
' stuff to do on postbacks
End Sub
Private Sub handleFirstTimeHere(ByVal sender As Object, ByVal e As System.EventArgs)
' stuff to do on non postbacks
End Sub
I recently had to implement (as a proof of concept) a finite state machine in RPG II. This isn't a natural match (to say the least) but by doing exactly what McConnell suggests, the code was reasonably straight forward:
C*
C* Factor 1 Op Factor 2 Result Comments
C*
C* -----------State 0 --------------------------------------------
C*
C STATE IFEQ 0
C CURCHAR CASLT'0' REJECT
C CURCHAR CASGT'9' REJECT
C CAS STATE=1
C END
C ELSE
C*
C* -----------State 1 --------------------------------------------
C*
C STATE IFEQ 1
C CURCHAR CASLT'.' STATE=2
C CURCHAR CASLT'0' ACCEPT
C CURCHAR CASGT'9' ACCEPT
C END
C ELSE
C*
C* -----------State 2 --------------------------------------------
C*
C STATE IFEQ 2
C CURCHAR CASLT'0' REJECT
C CURCHAR CASGT'9' REJECT
C CAS STATE=3
C END
C ELSE
C*
C* -----------State 3 --------------------------------------------
C*
C STATE IFEQ 3
C CURCHAR CASLT'0' ACCEPT
C CURCHAR CASGT'9' ACCEPT
C END
C ELSE
C*
C* ----------------------------------------------------------------
C*
C END End for state 3
C END End for state 2
C END End for state 1
C END End for state 0
C*
C* ----------------------------------------------------------------
C*
C STATE=1 BEGSR
C Z-ADD1 STATE
C ENDSR
C*
C STATE=2 BEGSR
C Z-ADD2 STATE
C ENDSR
C*
C STATE=3 BEGSR
C Z-ADD3 STATE
C ENDSR
C*
C ACCEPT BEGSR
C ENDSR
C*
C REJECT BEGSR
C ENDSR
The key was breaking the problem into two parts: figure out what you want to express, and then figure out how best to express it in the target language. I fear a pure RPG II style solution would have been an utter mess.
I missed the concepts of Blocks (from Smalltalk) in Java and went through a phase where I did something like this (this general type of back in JDK 1.1, updated for generics):
interface Block<T>
{
void perform(T data);
}
public class Main
{
public static void main(final String[] argv)
{
final ForEach<Integer> each;
final List<Integer> values;
each = new ForEach<Integer>();
values = new ArrayList<Integer>();
each.iterate(values,
new Block<Integer>()
{
public void perform(final Integer data)
{
System.out.println(data);
}
});
}
}
class ForEach<T>
{
public void iterate(final Iterable<T> values,
final Block<T> block)
{
for(final T value : values)
{
block.perform(value);
}
}
}
This is probably one of the most useless things I've programmed, but here it is:
This is a documented, unit tested PHP implementation of Ruby's Enumerable mixin. All methods (except .entries and .to_set) listed here are implemented: ruby-doc.org::Enumerable
The motivation for doing this came from this StackOverflow question, "How can I use PHP closures like ruby blocks?". This class emulates ruby blocks using anonymous functions, and it's more a proof of concept than anything else.
Source code and examples available here.