Last time, I wrote about how Phind blew my mind.
Now that I found my tool, I’ma use it in the next logical task, where I left off last time, to implement Excel file caching.
I used it to write a copy method, for use in my BaseRecordHandler
(and derived classes), that takes two args: sourceFileName
and destinationFolder
. It worked for sanity case, but I knew that using it would break my other use case (e.g. for SubmitBenchmarkRecordHandler
, which writes a spreadsheet by the name of the test case that called it, directories and all)…
The code it came up with looked like this:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public void cacheExcelFile(String sourceFilename, String destinationFolder) throws IOException {
// Get the source file
File sourceFile = new File(sourceFilename);
// Create the destination folder if it doesn't exist
Path destinationFolderPath = Paths.get(destinationFolder);
if (!Files.exists(destinationFolderPath)) {
Files.createDirectories(destinationFolderPath);
}
// Copy the source file to the destination folder
Path sourceFilePath = Paths.get(sourceFilename);
Path destinationFilePath = Paths.get(destinationFolder, sourceFile.getName());
Files.copy(sourceFilePath, destinationFilePath, StandardCopyOption.REPLACE_EXISTING);
}
So I started fleshing out the unit test cases against the feature…
To my horror, only one was passing…
So I started tweaking the method…and tweaking…and debugging…and tweaking some more…
No matter what, I couldn’t get all the four test cases I wrote to pass, and that I
lost a day or two to this 😨😩
I was also getting stuck on the regular expression to pass to a split()
that was being used in my SMDFileUtils
method…
Yesterday morning, I asked ChatGPT for help on the regex. It worked again!
I asked Phind for help on implementing the copy method such that it would pass all test cases, and provided the state-space of the args. It gave me some complicated method, that was also tightly coupled to particular values… 😣
See for yourself below:
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
void copyFile(String sourceFileName, String destinationPath) {
// Create Path objects for the source and destination paths
Path sourcePath = Paths.get(sourceFileName)
Path destinationFolderPath = Paths.get(destinationPath)
// Get the file name and directory paths from the source and destination paths
File sourceFile = new File(sourceFileName)
String fileName = sourceFile.getName()
String sourceDirectory = sourceFile.getParent()
String destinationDirectory = destinationFolderPath.toAbsolutePath().toString()
// If the destination folder is .cache, use the source directory as the destination directory
if (destinationDirectory.endsWith('.cache')) {
destinationDirectory = sourceDirectory
}
// If the destination path is not ., append the destination folder to the destination directory
if (destinationPath != '.') {
destinationDirectory += File.separator + destinationFolderPath.getFileName()
}
// Create a Path object for the destination directory
Path destinationDirectoryPath = Paths.get(destinationDirectory)
// Create the destination directory if it doesn't exist
if (!Files.exists(destinationDirectoryPath)) {
Files.createDirectories(destinationDirectoryPath)
}
// Create a Path object for the destination file
Path destinationFilePath = Paths.get(destinationDirectory, fileName)
// Copy the file to the destination directory
Files.copy(sourcePath, destinationFilePath, StandardCopyOption.REPLACE_EXISTING)
}
yikes.groovy
!!
Then I was like "fuck all this! Let’s delete the method, dust off my whiteboard, and draw-out some state table consisting of the state of a field that was being used dataSourceFilename
(sourceFileName
contains it), the two args, and the expected paths to Files.copy()
”…
Lo and behold, the code was not only writing itself, but I found myself writing actual code on the whiteboard, that I typed into the method, and it worked! I even through out all but one of the utils methods I created in trying to get it working…!
The method ended up looking like this:
protected void copyExcelFile(String sourceFileName, String destinationFolder) throws IOException {
final Path destinationFilePath = Paths.get(destinationFolder, this.dataSourceFilename);
final String destinationFileParentFolder = SMDFileUtils.GetParentPathname(destinationFilePath.toString());
Files.createDirectories(Paths.get(destinationFileParentFolder));
Files.copy(Paths.get(sourceFileName),
destinationFilePath,
StandardCopyOption.REPLACE_EXISTING,
);
}
I have util method SMDFileUtils.GetParentPathname()
defined to be:
public final class SMDFileUtils {
public static final String PathPartSeparatorsPattern = /[\/\\\\]/;
public static final String CurrentDirectorySymbol = '.';
public static String GetParentPathname(String filename) {
String parentPath = new File(filename).getParent();
if (parentPath == null)
return null;
StringBuilder builder = new StringBuilder();
parentPath.split(this.PathPartSeparatorsPattern)
.eachWithIndex { String token, int idx ->
if (token.equals(''))
return;
if (idx == 0) {
builder.append(token);
return;
}
if (token.equals(this.CurrentDirectorySymbol))
return;
builder.append("${File.separator}${token}");
}
return builder.toString();
}
}
This is all the way simpler!
Moral of the story:
I gotta learn when it’s time to throw everything away, even AI-generated code, and go back to the literal drawing board…
Also, sometimes the AI will make things more complicated. Part of being developer, is being smart enough to know when to ask it for code, and when to toggle to writing it yourself.
I should prolly go back and try to engineer the prompt better until I get something like what I ultimately came up with.