Also First Time I Work on Someone's Code
Tags: opensource, javascript, staticsitegenerator, markdown
Published: 2022-09-20 04:15:24

## Working on open source is truly different from working in a company 
Actually I should revert the title. It is not my first time to work on someone's code. I worked on existing code base in a co-op position. But I still feel like it's first time because it's different. In the work, I couldn't write much since it's a large code base and I didn't know much about all pieces of functionality. So usually I wrote a few lines of code at a time to fix a bug or improve something. I had to spend much time to figure out who the code works and was in the pressure to think about side effect and what I was potentially missing as it could affect a lot of people if I miss something. However, this open source experience gives me much more freedom and less pressure. Don't get me wrong. I also felt a great accomplishment when my code is shipped to production. Although it's just a few lines. 

## Javascript is fun and concise, but still want to write better Java
This week's task is to choose someone's repo and add markdown translation functionality to existing repo. It's a very straightforward assignment. Instead of working with the same partner, I decided to look at more projects to learn something from it. Unfortunately, no one used Java. As of now I see some amazing people wrote it with Go, Rust, C++. But mostly Javascript. I briefly checked all of other classmates' repo. Not like my Java's overloaded functions, code written in Javascript was so concise and pretty, thanks to rich set of library and framework. I tried to refactor my code and hide detailed implementation as much as possible. I still need to reduce many duplicate code. I wish I could write much concise good looking Java code. 

## Learning from others' code is great!
Going back to Javascript, a couple repos caught my eyes as they broke down the code nicely. Eventually I decided to work on Rohan's repo and he welcomed me too. My part is adding markdown heading and link parsing functionality. But he has another partner to add a feature to translate markdown to html which is a bit complicating since I had to develop a sub-feature without knowing how someone else is implementing main feature which is converting markdown to html. 

## Decided to do same feature, but it's not competition
So I needed to have my own way of filtering md files and reading the content of the file to implement and test my features. I thought about making a hard-coded string variable to simulate md file. However, I decided to implement my own markdown convert functionality as I want to do more practice with this project. Although I had to spend some time to pull the updated repo and redo my work, it was a good try to implement the feature. I will add this feature to my SSG later too. As a contributor, I tried not to change the original style and format. I enjoyed the thinking process to reduce the amount of change as much as possible. I think it's another fun aspect of programming. 

## How am I supposed to conquer regex? 
The hardest part was to figure out a way to parse markdown link into html. I had to find a regular expression to do this. But this is a way more complicated than anything that I tried before. I searched stackoverflow and many blogs. But it was hard to find one that satisfies my test cases. I tested many regex and finally I found one that is working for my cases. 

```javascript
const regex = /\[(.*?)\]\((.*?)\)/g;
line.replaceAll(regex, '<a href="$2">$1</a>')
```

It may not be the perfect regex as I don't have many test cases now. At least, it's working even for a string that has multiple links in it. So, it's time to send my PR!

## Writing PR message cautiously
When I commit to my own repo, I don't worry much. I know my code and I can quickly modify things that shouldn't be in my repo. But this is someone else's repo. Obviously I became very cautious and tried to write good PR message. Once I completed sending my PR, a couple of things that I should have mentioned came up in my mind. It's too late. But I will write a more careful PR comment next time. 

I wish someone also would send me a PR refactoring my code in a much concise way so that I can learn from it. Now I'm also adding a markdown converting feature to my SSG. I will be very happy to use my regex in my Java code!