Refactoring in Julia
The first real Julia program I ever wrote is a a wrapper for interacting with ycashd (for those unfamiliar, this is the Ycash Daemon). You…
The first real Julia program I ever wrote is a a wrapper for interacting with ycashd (for those unfamiliar, this is the Ycash Daemon). You may view the source code here. It has several issues that have remained untouched for quite awhile and today is the day for me to address them. First, is the dependency mess you see at the top of the screen.
Let’s create a file called “deps.jl” and literally just cut and paste all of this stuff into it. Open up the “deps.jl” file, and you will see some user prompts at the bottom. I’m going to totally delete all of this nonsense.
After deleting, save everything and run it…
Ok, so it can’t find “DataFrames”… Let’s add only our Julian dependencies at the top.
If you run it, you will encounter this next…
The program asks us for the path to ycashd on every single run. This just seems like unprofessional and horrible structure. This should be in a setup file. We already have one, but we named it “deps”, let’s change the name to “setup.jl” delete this code from “ycash.jl”.
In “setup.jl” we’ll add this function.
Running “ycash.jl” at this point in time should give this error:
I’ll add the following code to “ycash.jl” so it knows where to to find ycash-cli.
Change the “ycashcli()” to look like this:
Next comes work on the actual functionality. When using ycash-cli by itself, it can take multiple arguments.
Let’s run it using “ycashcli()”.
This error is because “ycashcli()” can only take one arg. Let’s fix this. First let’s write a test. Because we’re going to be testing a ton of stuff within the entire script, let’s put the tests in a separate file, “tests.jl”.
The first assert is just to test if we have tests, afterwards we say hello from the test file and run the command that just gave us an error. At the bottom of “ycash.jl” let’s add this.
Run it
Time for a little TDD. Let’s add our first failing test.
The large integer you see in our assert is the UNIX timestamp of block 0. Now to make it pass, let’s overload the “ycashcli()” function.
Run and it runs through the end of the script. The output is far to long to post on here. So we wrote our test, it failed, we made it pass and now it’s time to refactor. Well it would be but we already refactored “ycashcli()” when we changed the path earlier. Time to finish the large scale refactor we started earlier. Remove the “build_deps()” function from the setup file because we include the Julian dependencies in “ycash.jl”. After adding the proper setup prompts, the final “setup.jl” will look like this:
We’ll also remove this stuff from “ycash.jl”, YAGNI (you ain’t gonna need it).
Now let’s remove all of this stuff because “ycashcli()” takes multiple args and we don’t need them.
The next mess we need to take care of is the “z_send()” function, I’ll get to that in the next piece because it is a big mess. It works, but it’s a really big mess.























