I've been thinking about the next project for this blog.
The point of all this has never been to write a program that allowed me to drop sticks on a non-existent board. I wanted to practice with a programming methodology that allowed me to create code in a reliable way. This is what I came up with:
Class code, wrapped in Add-Type, existing in separate files.
Code signatures documented.
Some comment-level documentation.
Testing code wrapped in #if #endif preprocessor commands.
Testing scripts that live in a directory and give output only when they fail.
I should be able to leverage PowerShell's string-based scripting should be useful for something. Hey, let's try something.
The Get-Content cmdlet will let us look at the file contents:
blog>get-content matchcode.ps1
This displays the whole file contents.
So, suppose I want to pull out all the lines with comments – I could do this:
get-content matchcode.ps1 | select-string "//"
Here's the last few lines in the output:
// METHODS -- PRIVATE
if (candidate < 1) return false; // checks for -1, which is used for non-location positions in the nodes
if (x.currentNode(candidate)) return true; // Checks all the nodes to see if a candidate exists.
// TESTING CODE
// Clone method: for now, just used in testing
// Just used in testing
return new Stick(false); // Return to this later
Of course, these aren't the javadoc-like comments I added for the method signatures. Those started with “/*”:
get-content matchcode.ps1 | select-string "/*"
I was really beating my head against the wall (figuratively) about why this wasn't working. It was returning all the lines. The asterisk is being used as a wild card.
I went through the documentation for select-string to see about PowerShell's regular expressions. After futzing around with it for a while, I came up with the following function:
function doc ($x) {
if (select-string -inputobject $x "/\*" -quiet)
{
$global:b = (1 -eq 1)
}
if (select-string -inputobject $x "\*/" -quiet)
{
$global:b = (1 -eq 0)
}
if ($b) {$x}
}
This is pretty good – pulls out all the stuff. Let me play with this a little longer.
OK, I moved things around a little:
function doc ($x) {
if (select-string -inputobject $x "\*/" -quiet)
{
$global:b = (1 -eq 0)
}
if ($b) {$x}
if (select-string -inputobject $x "/\*" -quiet)
{
$global:b = (1 -eq 1)
}
}
The way that I had it originally meant that lines with “*\” printed. With a little reordering, I got it to display only lines without either “/*” or “*?”. Thanks Grimm Brothers! (No joke. The Grimm brothers described the ordering of the first and second Germanic sound shifts in order based on the effect the sound shifts had in the language.)
After adding some lines with seperators, this is the output:
START METHOD DESCRIPTION:
Gets the stick object at a specified board location.
loc: Location.
returns: Stick at loc.
TESTING CODE: DO NOT CALL EXCEPT FOR TEST
This code refers directly to the grid array. As such, it is subject to out-of-bounds errors.
The private method getStick is not subject to such errors.
END METHOD DESCRIPTION:
START METHOD DESCRIPTION:
Gets the node object specified by 0-based index.
loc: Node to be returned
returns: Node at specified index.
TESTING CODE: DO NOT CALL EXCEPT FOR TEST
END METHOD DESCRIPTION:
And then I added this line, in the middle:
if (select-string -inputobject $x -quiet "public") {" "; $x; " "}
Here's the last few lines using the script as is:
START METHOD DESCRIPTION:
Causes a stick to fall from the specified location to a lower location.
location: Stick to fall.
Returns true if a stick can fall. False if the stick is stopped
for any reason.
END METHOD DESCRIPTION:
public bool test_dropStick(int location)
START METHOD DESCRIPTION:
Gets the stick object at a specified board location.
loc: Location.
returns: Stick at loc.
TESTING CODE: DO NOT CALL EXCEPT FOR TEST
This code refers directly to the grid array. As such, it is subject to out-of-bounds errors.
The private method getStick is not subject to such errors.
END METHOD DESCRIPTION:
public Stick test_getStick(int loc)
START METHOD DESCRIPTION:
Gets the node object specified by 0-based index.
loc: Node to be returned
returns: Node at specified index.
TESTING CODE: DO NOT CALL EXCEPT FOR TEST
END METHOD DESCRIPTION:
public Node test_getNode(int loc)
public bool test_isValid(int candidate)
That was all fun.
To get back to the goal of this post, I was thing of doing another small project using this methodology from the ground up. Something simple, like my odometer example from a few weeks back. Include a main() method, so I can compile it outside PowerShell if I really want to. Maybe try to do it over the course of a week or so.
So, tomorrow, project design.
No comments:
Post a Comment