Tuesday, November 27, 2018

Why all Javascript Developers Should Learn Typescript

First of all, I wanna talk about my own motivations behind learning Typescript. Having been coming from statically-typed language background, when I first introduced to dynamic language (Javascript ES6) in a non-toy project, it felt positive in many ways. It was particularly powerful for prototyping a program where the structures weren't well determined yet. In my case, it was due to business requirement that was still ambiguous, so things were inevitably changing. It was also convenient because dynamically-typed language is often also a scripting language. Being able to save the code and immediately run it without having to wait for compilation time is just so convenient.

But as soon as my code-base grew large, Javascript became hard to maintain. Scaling up my medium-size code base became very hard because:
1. Special discipline needed to be ensured in order to make the code readable and maintainable (i.e. by manually documenting input and output of each functions)
2. Refactoring became very hard as there's no easy way to 100% ensure where a function or a class is being referenced from, which implied hardship when renaming a function, class, or file, and also while changing a function's parameter or return type.
3. The absence of type-checker also caused many mistakes that would have been easily caught during the compile phase of a statically-typed language to be hidden until run-time, hence requiring super rigorous testing. (don't forget how Javascript is sometimes crazy, i.e. == vs. ===)
4. Overseeing and reviewing beginner developers who didn't yet have the discipline was also very hard, particularly because the language did not "force" them to think about data types and structures before implementing something, which often times resulted in them writing spaghetti code.

Typescript is wonderful because it combines the best of both static and dynamic typing. It is a superset of ES6, which means all ES6 code will work as-is without requiring any changes at all. This allows one to quickly prototype a code using regular dynamically-typed Javascript, and then slowly adding type definitions into it as the requirement steadily becoming more mature. Typescript code also interacts fine with Javascript code. So yeah, you're not gonna run out of libraries to use as you can use everything you find on NPM. It still does require a lot of discipline in order to make the type system useful though, because one can simply turn a ".js" file to ".ts" without actually leveraging the power of the type system, duh! But I guess an effectiveness of a tool always depends on the hands of its users anyway. But yeah, it's a real solution to keep a large JS code-base more maintainable while still being versatile when needed.

Visual Studio Code is another reason why one would wanna use Typescript. It supports all the fancy stuffs that static language IDE supports such as auto-refactor, auto-complete, jumps to definitions, symbols, etc. If you ever used and came to appreciate typical Microsoft IDEs, this feels similar. And not only that, this time Microsoft chooses to publish its source code on GitHub! So yeah, we no longer have the worries that typically come with using closed-source programs: fell in love then got heart-broken because things didn't go as hoped.

Another thing to love about Typescript is how powerful its typing system is. Even if you only read its documentation, you'll probably guess that Microsoft had a bunch of world-class programming language experts and PhD holders working on this super-smart typing system. One thing I enjoy is its structural typing system where a type is compatible with another as long as it has the property expected out of it. So this is kinda like duck-typing, but typed, meaning that the transpiler is going to yell if you try to pass in incompatible types. It also has supports for type union, intersection, etc, just like what one would expect out of a typical modern programming languages.

In the next blog post, I'm gonna write about how I refactored my NodeJs ES6 code-base into Typescript.

How I Come Back from a Debilitating Back Pain to be a Productive Programmer

First of all, I want to tell you that I'm not somebody with medical background. I'm just a regular software engineer who happened to suffer from mild herniated disc a few years ago, perhaps due to my profession which involved a lot of sitting on a regular basis.

About 3 years ago in 2015, I began feeling mild lower back pain everyday at the end of my work day. It wasn't painful enough that it impaired my life, but it was uncomfortable enough that I would sleep on a carpeted-floor every night thinking it would help. At that moment, I didn't know what it was. I just assumed I lacked exercises and stretches.

3 years fast-forward to early 2018, my lower-back got painful enough that I can no longer sit down for more than 15 minutes. After 10-15 minutes of sitting, the urge to get up was too much due to the pain. Here I began to go see different doctors. One semi-traditionalist Chinese doctor practicing bone-related therapy told me it was because I had a mild scoliosis. He observed that my back bend to the right side a little bit. I went for therapy with him for a few months without much improvement. At this time, it had been hard to continue my life as a programmer as I can't barely sit down. Even when laying down in bed -- something considered the best remedy for back-pain sufferer -- I still felt uncomfortable. Everyday had been a real struggle that I can only offer up to God.

One day, somebody suggested me to see a nerve-specialist doctor. I did, and after describing my symptoms: stinging pain that went from the leg up to the lower-back on sitting down and stiff lower-back muscles, she immediately told me to get MRI and X-Ray. Turned out that I had a mild herniated disc. The pain I experienced was a sciatica, a pain that has something to do with the sciatic nerve. In my disc herniation case, the sciatic nerve got pinched by the discs on my back, between L4-L5 and L5-S1 sections. Apparently this sciatic nerve is one of the largest nerve root in the body, spanning from the leg all the way up to the back. This perfectly explained why I felt stinging pain on my legs when sitting down. I had always been confused as to why my legs felt painful on sitting!

My nerve doctor told me that although my pinching is still mild, if it's not treated seriously, it can get pretty bad. One might not even be able to have a kid as impaired sciatic nerve could cause erectile dysfunction! Scary, scary.. She told me I had to go swimming back-stroke for 1-hour a day, everyday. It had to be back-stroke, no other forms. Breast-stroke is particularly prohibited because it'd put strain on the lower back. She said she didn't want me to go for a surgery because it's still mild and should be treatable organically. She also didn't want me to go to chiropractor or some similar means, because if it's not done properly, it could have significant consequences. And getting it done properly is a challenge as there could be different root causes for different people.

So I did what my doctor suggested. I diligently swim everyday for like 3 months. After that I felt a lot better. I could sit down for 30-60 minutes although I would still feel very painful afterwards. I had to lay down on the bed for about 1 hour after I finished a 45-60 minute session of a sitting session. Although the struggle was still there, I was very grateful. I could still imagine how impaired my life was before meeting the doctor.

At this point, I began experimenting. I tried watching many self-help videos on Youtube and tried googling around. I highly recommend Athlean-X Youtube channel. The guy who made the videos is a physiotherapist by profession, specializing in sport injuries. I wouldn't say that doing the exercises suggested in those self-help videos cured me, but some of them had definitely helped me alleviating the pain. Generally, I suggest being very cautious about experimenting with self-help videos, though. Try to read on the comments and try to critically study all the suggestions advised. Also try to consult with your doctor before experimenting with the move or exercise!

At this point, I'm still at that 30-60 minutes of maximum sitting. And I still had to lay down on the bed afterwards too as the after-sitting pain was unmanageable otherwise. I still swam regularly about 2-3 times a week, but I felt it hadn't been as helpful anymore. Not happy with my progress, I began to Google around about weight-related exercise that might help with sciatica, and I came across lateral pull-down. A particular video warned me that doing it with a bad form and posture could make the sciatica worsen instead of helping. On the flip side, doing it properly can be very helpful. The guy on the video explained that the key is to do it properly: when exercising, the back had to be 'locked' in a proper posture all the time. The weight shouldn't be too much that the back had to 'overbend' in order to manage it. I considered doing it, because I had pain on my right side while my left side was generally much better. I figured that perhaps my pain was due to the discs 'bending' to the right side, which was confirmed by the MRI scan. In my own theory, lateral pull down would be useful because it would balance out the discs that were bend to the right. I thought that back-stroke swimming hadn't been anymore useful because my discs would need more pressure to balance themselves out. After all, swimming can only exert limited amount of force, right..?

I started hitting the gym 3-5 times a week for the past 2 months. There are three moves that I've always put in the routine: Mckenzie exercise, plank, and lateral pull down. Mckenzie exericse is one of the most commonly known exercises to alleviate sciatica. Plank is a core-strengthening exercise. Lower-back issue is commonly associated with the lack of core strength -- the muscles whose job is to support the lower back aren't strong enough to do the job, hence the back bone is affected, and therefore a back pain! And lateral pull down is to "fix the unbalanced discs" I theorized earlier. And you know what? I am now able to sit down for 2 hours straight without much significant pain! I feel a slight discomfort when sitting, but it is totally manageable. I also do not longer need to lay down on the bed after every sitting session. I can thank enough all the people who have shared the information to help me getting where I am now. I think I am now ready to come back to a full-time programming job :)

P.S. Again, by all mean, I'm not a medical professional nor trained in the area. I am only sharing this to spread out what I feel has been helping me out. If you ever decide to try out what I do, please first consult with your physicians.