You must be registered to see the links
Good morning, evening or afternoon. In this Developer Log I'm going to be talking about code refactoring, what it is, why it's important and what are the pro's and con's of doing it.
What is Code refactoring?
Put simply, code refactoring is the process of restructuring existing code without changing its external behaviour.
Okay, so why am I talking about it?
Recently I looked back at the code used in chapter one, and compared it to the code I now use in chapter four. There is a distinct difference between the two. In Ren'py it's good practice to
call or
jump to new scenes rather than allowing them to run on.
In fact, it's commonly accepted best practice to use jump rather than call. Due to the choice-driven nature of most visual novels, if you call into every scene without putting in a 'return' you can create something called a code debt. And like all debts this will need to be resolved at some point, unless you want your engine to resolve it for you and possibly return users from a chapter four event to the start of chapter one!
The above also applies to other coding languages, but with Ren'py we can avoid having to keep track of calls (and code debt) by using jump. You can jump to any label at any point even back to previous chapters and so long as your 'exit' jump is correct the story will continue forwards.
Essentially, chapter one will benefit from being redone or refactored into the same style I now use for chapter four. Chapter two will most likely also be looked at. Chapter three doesn't need any work in that area.
What are the benefits to refactoring?
A quick search will show six main benefits to doing this:
- Enhance code readability
- Reduce complexity
- Improve source code's maintainability
- Improve extensibility
- Enhance performance
- Facilitate fast program execution
Some of those headings can be combined, performance and program execution for one. Code readability and complexity for another.
With
Dreams of Reality's code, I am only really interested in the first three of those benefits.
Dreams of Reality is not an overly complicated game to run when it comes to its code. Other than the achievement system, the per tick, or second to second demand on the runtime is very light. As such, there is nothing to be gained from trying to enhance performance or facilitate fast program execution.
Code readability and complexity.
I am the sole developer of
Dreams of Reality, but that doesn't mean I would not benefit from a tidy up of the code. Formatting the text to the correct indent in each label, allowing me to collapse those labels and have a better view of the entire script, is just one such advantage I will enjoy.
Another is removing the 'run-ons' and adding more jumps to labels. There are many elements to the script that are essentially the same, but only contain one or two differences (due to choices). I want to reduce these down to one label per scene with the choices dictated via variables.
This is not a good way to do this.
There are others who do look at the code, from Mod makers to the Walkthrough provided by ZoeyRaven - having the code be easier to follow would make their life just that bit easier.
All of this is to say, if the code was less complex with less repetition, and followed a standard format, I would spend much less time trying to read it.
Source code maintainability.
At a basic level, if the entirety of the code reads the same, I will better be able to make small adjustments and corrections to text being done in only one place, rather than three or four!
...and the cons?
Time. I have precious little, and going through the entire code for chapter one and two is a time investment that could be better spent elsewhere. However, reducing the complexity, and increasing the legibility of the code will save me a considerable amount of time in the long run. It's for this reason I believe it's worth doing.
Kimiko doesn't care about code refactoring.
In closing.
This code refactoring is going to run in parallel to the development of the next update. The refactor of chapter two will likely be in place for the first update of chapter five. If all goes to plan, you, as the players, will not notice any difference to your experience.
I hope this has proven illuminating. Until next time,
Stay safe all,
Cenc