Contributing To Open Source - C++ Edition

Majd Al Mnayer - Oct 8 - - Dev Community

Following last week, where I did my first straightforward open source contribution, this week I wanted to tackle bigger contributions.

For starters, I came across a C/C++ Library that prints the alphabet as ASCII art in different fonts.

However, afterward, I still wanted to do more, so I started looking for a larger project to contribute to. To my surprise, the very popular faker.js library has a C++ version in the works, faker-cxx, which I was very excited to work on!

First Contribution

For my first issue of this week, I was tasked with adding a new letter to the drpepper font in the ascii-art library.

This was a straightforward task because the file structure was very easy to navigate. There was a folder called fonts containing the supported fonts. Before I began the process of adding my new character, I examined how others had added characters to the font.

I simply followed the existing style, added my character, and then submitted my pull request.

However, I found the method of adding new fonts somewhat peculiar. Here's an example:

vs D()
{
    vs character = getCharGrid(4, 5);
    character[0][0] = character[0][4] = character[1][1] = character[1][3] = character[2][1] = character[2][3] = ' ';
    character[0][1] = character[0][2] = character[0][3] = character[3][1] = character[3][2] = character[3][3] = '_';
    character[1][0] = character[2][0] = character[2][2] = character[2][4] = character[3][0] = '|';
    character[1][2] = '.';
    character[1][4] = '\';
    character[3][4] = '/';
    return character;
}
Enter fullscreen mode Exit fullscreen mode

I'm not entirely sure why it was done this way, but it seems to allow for clear modifications if needed in the future.

Second Contribution

For my second issue in the faker-cxx library, I had to add a new faker function to the Date module. This function should mimic its JavaScript counterpart.

At first, I thought, "It's just one function... how hard can it be?" But I was wrong.

I had to read through a lot of code to understand how everything was structured, where things were declared, where they were exported, how tests were conducted, and how to add new ones.

The contributing guidelines emphasize that all additions must be thoroughly tested.

There were many custom types and reusable functions, so I had to familiarize myself with each one before I could start coding.

Once I identified the necessary files and became accustomed to some initially unfamiliar C++20 syntax, like:

void doSomething(const& auto param){}
Enter fullscreen mode Exit fullscreen mode

I realized this is syntactic sugar for templates! This use of auto allows the function's parameter type to be deduced without explicitly specifying it.

After understanding the internal functions (used internally for development but not exposed in the API), I added two functions. One accepts two ISO dates, and the other two timestamps. Luckily, there was already an internal function to generate a random date between two given dates, so I mainly needed to manage parameter conversions and preparations.

Once the functions were ready, I documented them for the API and created and ran the necessary tests. Adding tests was initially confusing, but after examining existing ones, it became straightforward.

However, running the tests proved challenging. I needed to build the entire library using cmake, which usually wouldn't be an issue, but I encountered a persistent error with the fmt dependency not found.

Despite following all the steps (on both Windows and Unix), I couldn't get the cmake build to succeed. After several hours of debugging, I decided to try another build method provided by the project, using bazel, which was much simpler.

After successfully building the project and running the tests, I was delighted to see all tests pass. I then tested my newly added function in a fresh C++ project, and it worked perfectly!

Finally, my pull request was accepted without any requested changes!

It felt really great contributing to a well known library like faker, that I have personally used at my job!

Final Thoughts

This week's contributions were incredibly rewarding and exciting. I feel more confident than ever in my ability to contribute to open source projects. Next week, I plan to explore contributions to a project in a different language!

. . . . . . . . . . . . .