Solidity Chronicles | Code Refactoring [0]

A Series of Code refactors

The Solidity repo utilizes its Github projects really well, and most of the code that I had done before
was tackling some basic issues that were needed to get the next version out.

Incredibly, I ended up being thanked on the 0.5.0 release. I didn’t expect it but it happened. I really enjoy spending time in that repo, everyone is very
intelligent and very kind.

Now onto the issue.

0.5.1 is the next update, and there’s an issue thats opened up regarding code refactoring. Made up of many sub issues, basically the goal is to enhance the performance of the language and to remove redundancy. Here’s one of the sub issues I worked on.

Issue:

When a function is declared with an override remove the redundant virtual.

Side Issues:

I’d realized before starting this relatively major refactoring (36 pages changed), that I had screwed up my repository. I had built from source following the Solidity instructions, and I had built my original repo that worked on the three other issues in Powershell on Windows. I had ended up with around 500+ build files that were shown as changes that needed to be added to Git.

My previous issues were small in size, working mostly in 1 to 2 files, so it wasn’t a problem to avoid adding them. The issue came from needing to make sure I properly found and removed every instance of virtual partnered with an override. Having so many changed files would make it difficult for me to navigate and understand which files I needed to add, and which I didn’t. A lot of unecessary clutter.

Here’s how you avoid that issue if you are a Windows user.

Important: run all of these in Command Prompt, not Powershell.

Clone the Solidity repo, follow the pre-reqs, run the external dependancies bat as described in building from source.

mkdir build from root of the repo

cd build

cmake -G "Visual Studio 15 2017 Win64" ..

cmake --build . --config RelWithDebInfo

If you are rebuilding a lot, make sure to rebuild in the build folder, always.

Then run the tests in root folder. .\build\test\RelWithDebInfo\soltest.exe -- --no-ipc --no-smt

The repo will then ignore all the build related files properly.

The next issue I ran into was replacing the redundant statement. I had to remove virtualbut only if there was an override following it. Following cppreference, it seemed to not negatively affect the virtual-ness of a function if there was only override present.

Solidity being a language, you get quite worried about accidentally introducing some unforeseen vulnerability in the language. Especially since it handles real, valuable currency on a daily basis.

VSCode is the editor I’m using at the moment. CTRL+SHIFT+F nets you a repo-wide search and extends to a replace functionality. You can write which files to ignore as well. Now, you can extend this search with a regex toggle which was extremely useful for me working on this issue.

But there was an unforseen limitation of VSCode’s replacement functionality. Let me break down the issue:

I used VSCode’s regex functionality to find a sentence with virtual first and override second

Regex: virtual.*override, meaning match any character repeated 0 or more times.

The functions look like this: virtual function name blah override; I want to keep the middle part and remove the virtual

The replace section does not have regex support. Meaning if I do:virtual.*override | Search.*override | Replace withvirtual function name blah override; ——Becomes——-> .*override;
The regex becomes part of the replacement rather than keeping the middle part and removing the virtual. This happens no matter what.

Meaning I had to go in manually into every file and do a local search (Ctrl+F) and replace by doing:virtual | Search
` ` | Replace with (empty)

That works but I had to manually analyze that the highlighted repo search matches did in fact contain override, which some of them didn’t.

I believe this could be remedied by adding this functionality to VSCode, because I reckon I’m not the only one who ran into this case before. I also accidentally removed a virtual from an exception that was needed but the maintainers fixed that (thanks).

Anyways, onto the solution.

Solution:

CTRL+SHIFT+F in VSCode

Regex match virtual.*override

Manually go into the matches that pop up and locally replace with emptiness

Rebuild from source, make sure it compiles, run the tests and throw it up on the repo to run it through the Travis build (in which CodeCov yelled at me).

Benefits:

Makes the code look nicer and less need to process the virtual keyword