<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hugo Grochau on Medium]]></title>
        <description><![CDATA[Stories by Hugo Grochau on Medium]]></description>
        <link>https://medium.com/@hugogrochau?source=rss-43d5bc013bf2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*8s-2Hhj7xGAkJVAo28v7OA.jpeg</url>
            <title>Stories by Hugo Grochau on Medium</title>
            <link>https://medium.com/@hugogrochau?source=rss-43d5bc013bf2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 03:13:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hugogrochau/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How we reduced our React Native app size by 60% with a few simple fixes]]></title>
            <link>https://medium.com/swlh/how-we-reduced-our-react-native-app-size-by-60-with-a-few-simple-fixes-3d59adc2ed3d?source=rss-43d5bc013bf2------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d59adc2ed3d</guid>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[optimization]]></category>
            <dc:creator><![CDATA[Hugo Grochau]]></dc:creator>
            <pubDate>Mon, 11 May 2020 13:51:19 GMT</pubDate>
            <atom:updated>2020-05-11T22:51:06.325Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K6GST4_K5matVCgPC9tbGA.png" /></figure><p>I work at <a href="https://mutual.club/">Mutual</a>, a peer to peer lending Fintech in Brazil. We connect borrowers looking for fair rates to lenders looking for above-market returns. Being a very broad product with a diverse amount of users in a large country, our React Native iOS and Android apps are downloaded by all sorts of devices.</p><p>The majority of those users, however, are on low-end devices. We can see this using Facebook’s <a href="https://github.com/facebookarchive/device-year-class">device-year-class</a> library which, given a device model, shows in what year it would be considered a high-end device. For example, the most popular phone for our users is the Samsung Galaxy A10, which although was launched in March 2019, would only be considered a high-end phone in 2013. Looking at all user’s devices, 85% of those would only have been high-end in 2015 or before. Because of this, we put a premium on optimizing our app so that even with a limited device, our users can still have a great experience.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*PGAtjAj2pwVlnyjQNSnfIQ.png" /><figcaption>Percentage of devices considered a flagship in each year</figcaption></figure><p>On that note, we’ve more recently taken a hard look at our app size which was 26.8 MB on Android. While that size isn’t completely out of control, it is definitely above our peer’s median, which the Google Play Console reports as 16.3 MB. Size can be a deal-breaker for users who have limited data plans or little to no available disk space and have to choose which apps to keep or uninstall. This is especially important at Mutual since borrowers have to pay their monthly installments through the app. When a borrower uninstalls the app, their chances of paying on time decreases drastically, directly impacting investors’ returns in our marketplace.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*e0XXZ1aWrScU3ORywM6hiQ.png" /><figcaption>Mutual (26.8 MB) is much larger than peers’ apps in the same segment</figcaption></figure><p>Not only does app size affect uninstall rates, but it has a great impact on the <strong>install</strong> conversion rate also. There’s a <a href="https://medium.com/googleplaydev/shrinking-apks-growing-installs-5d3fcba23ce2">great article</a> by the Google Play team on the subject where they show the importance of shrinking your app.</p><blockquote>For every 6 MB increase to an APK’s size, we see a <strong>decrease in the install conversion rate of 1%</strong>.</blockquote><p>They go on to reveal that in developing countries, where low-end devices are the norm, this effect is even greater:</p><blockquote>The removal of 10 MB from an app’s APK size in emerging markets correlates <strong>with an increase in install conversion rate by ~2.5%</strong>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QP5Kh-1pz2Uo731oII92LQ.png" /><figcaption>Install conversion rate increase per 10MB decrease in APK size by country (Google internal data)</figcaption></figure><p>We were very motivated by the potential improvements to our install conversions and uninstall rates. So we got to work on reducing our app size as much as possible without degrading the user experience. The first step was looking through the <a href="https://developer.android.com/topic/performance/reduce-apk-size">official resources</a> available for Android developers.</p><h3>Android App Bundle</h3><p>Reading through that page, we are told that the simplest way to reduce app size is to try out the new <a href="https://developer.android.com/guide/app-bundle">Android App Bundle</a> (AAB) method of distribution. Up until this point, we were distributing our app by compiling a good old <a href="https://developer.android.com/guide/components/fundamentals">Android Package</a> (APK) file that could be run in most android devices and uploading it to the Google Play Console. An AAB bundle, however, contains only your compiled code and resources. Therefore when you upload it, Google Play itself becomes responsible for generating an optimized APK for each device type, knowing its specifications and CPU architecture.</p><p>So with a simple change to our build pipeline, we can get a massive size reduction for no cost? Sounds too good to be true!</p><p>After looking through the documentation, all we did was change the React Native Gradle build script to run bundleRelease instead of the current assembleRelease . Just like that, we have our AAB file. After some further modifications to the <a href="https://fastlane.tools/">Fastlane</a> config’s <a href="https://docs.fastlane.tools/actions/supply/">supply</a> action to automatically upload directly to the Play Store, we are good to go and our new reduced release shows up on the Google Play Console.</p><p>With just this change we cut 9.1 to 12.4 MB of our delivered APK size. Turns out it was really too good <strong>and</strong> true!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dlw3IceAuZ3iKFgWF6AKYA.png" /><figcaption>Old APK at 26.8MB compared to the new AAB at 14.4 to 17.7 MB</figcaption></figure><p>Be careful though: if you’re using React Native with <a href="https://hermesengine.dev/">Hermes</a>, you might have to update your soloader dependency as per <a href="https://github.com/facebook/react-native/issues/26400#issuecomment-539395814">this issue</a> or you risk delivering an app with a critical bug to your users. Thankfully we were able to catch this problem by testing in the alpha release track. But it could’ve easily slipped through as it doesn’t show up locally or when you build an APK.</p><h3>Optimizing assets with the Android Size Analyzer</h3><p>The next suggestion on the list is the <a href="https://developer.android.com/topic/performance/reduce-apk-size#size-analyzer">Android Size Analyzer</a>. A command-line tool that analyzes an Android app to point out opportunities for size reduction. After running it using the command size-analyzer check-bundle [BUNDLE].aabwe receive a list of <strong>large assets</strong> and <strong>images we can optimize.</strong> We are also told to <strong>configure </strong><a href="https://developer.android.com/studio/build/shrink-code"><strong>ProGuard</strong></a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nq5Wyh3NVxTwbsh1EgOvnA.png" /><figcaption>The output of the size-analyzer command</figcaption></figure><h4>Proguard</h4><p>Proguard is a tool to shrink, obfuscate, and optimize your Java bytecode. We have not yet explored this avenue, as we’ve read about possible incompatibilities with other Android libraries. As we are looking for quick and easy size reductions, we chose to leave this optimization for the future.</p><h4>Large assets</h4><p>Running the command again with a -d flag, we get a list of each asset ordered by size. As the size-analyzer tool doesn’t know our app’s user flow, it lets us decide which ones we can remove or bundle dynamically.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Shn_tgyrDkqxSWg1w3VQWw.png" /><figcaption>List of large assets ordered by size</figcaption></figure><p>The first and largest item is the React Native JavaScript bundle. It is not currently possible to split and dynamically load this, but we will see how we can shrink this later on. Following further down the list of suggestions, we see many large fonts (TTF) and images (JPG and PNG) assets.</p><h4>Unneeded pictures</h4><p>Our attention is immediately drawn to four huge JPG pictures being used in our internal <a href="https://storybook.js.org/">Storybook</a> tool. They added an extra 2 MB of trash to our production APK. What an embarrassing blunder! When things like this happen we feel pretty foolish. But in the complex world of Software Engineering, we all make mistakes. I believe in sharing them with our peers so we can all learn from them. Chances are if you don’t track the growing size of your applications you might be making some of these mistakes too.</p><h4>Fonts</h4><p>After quickly getting rid of these large pictures, we kept looking at the rest of the list. It’s clear to see that there is an abundance of fonts that were being bundled. After talking to the design team they told us that many old components did not strictly follow the typography guidelines. So we identified which components could be removed and which could use a comparable updated font. With this, we cut down our font usage from six to four.</p><p>Another thing we noticed is that our font asset sizes are huge! They are clocking in at almost 670 KB each. which means that our four fonts represent a whopping 2.7 MB of our uncompressed bundle size. Thankfully there is a tool called <a href="https://fontforge.org/en-US/">FontForge</a> which allows us to take a deeper look and modify those font files. Opening these up, we can see that the majority of the asset size can be explained by the expansive Cyrillic script and other unneeded glyphs. We can remove all of these since our app is completely in Portuguese. With this change, we shrunk our font sizes from 670 KB to 70 KB each, a 90% reduction!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*GsB8NbzXId-sk_ZcmLOlfA.png" /><figcaption>Example of some glyphs being included in our fonts</figcaption></figure><p>Removing unneeded fonts and optimizing the remaining ones totaled a 3.8 MB decrease, which translates into a cool 2 MB reduction to the final APK size.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*t76orb12PhgiX3921op6Ww.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*3tHPqMpRyZK6V72om3hSMg.png" /><figcaption>Before and after removing two fonts and optimizing the rest</figcaption></figure><h4>Optimizing images</h4><p>Taking a look at the remaining images some of them are quite large. We ran a couple of them through an image optimizer tool (<a href="https://tinypng.com/">tinyjpg</a>) and saw large reductions in size. After that, we decided to optimize all 41 JPG and PNG assets used within our app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G333vq4XCmnylzx1O8j6jQ.png" /><figcaption>Before and after of one of the images we optimized</figcaption></figure><p>That brought us down from a total of 2.5 MB in image assets to 756 KB, a 70% reduction. However, because the images were not optimized, they were already being compressed in the process of generating the final APK. So, in practice, we only cut 500 KB to the end-user.</p><p>After this, we realized that we’ve already depleted all of the easy low hanging fruit optimizations. All further asset optimizations would either take a lot more effort or result in only marginal improvements.</p><h3>Optimizing our React Native JavaScript bundle</h3><p>Now that we are done looking at the native assets, it is time to analyze our JavaScript bundle. This is an especially good thing to optimize for three reasons. Firstly, it reduces the bundle size of our finished APK. Secondly, it also results in a faster app startup as the JS virtual machine needs to parse less code. Finally, and most importantly, it speeds up over the air (OTA) updates that we ship out multiple times a week through <a href="https://microsoft.github.io/code-push/">CodePush</a>.</p><h4>Bundle analyzer</h4><p>To decide how we will reduce the size of our bundle, first, we need to be able to see what is taking up the most space. For this we will count on another great open-source tool: <a href="https://github.com/IjzerenHein/react-native-bundle-visualizer">react-native-bundle-visualizer</a>. Running it against our project, we get a visualization of every folder and dependency of our application and their respective sizes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qvhkvFCbE9yR49fjMnwQ0w.png" /><figcaption>Snapshot of the libraries and folders of Mutual’s front end code base with their respective sizes</figcaption></figure><p>We can see that the app bundle totals 5.49 MB, with 57.8% of that being from node_modules dependencies, 27.5% from the application code, and the rest the tool wasn’t able to map. The bundling process already removes unused code paths, so what we see here is the code that is actually used by our app. Even so, there is always still room for improvement.</p><p>The biggest dependency that we have is math.js which as the name implies implements many mathematical operations. We shouldn’t need this dependency since we execute all sensitive calculations in the server and only send the results to the app. Taking a closer look at the front end code, the library was being used for some simple operations. Most likely it was used out of habit by a developer who had also worked on the back end code. We quickly extracted these methods from the library and took it into our codebase, removing the dependency entirely. This brought down the bundle size to 4.64 MB. We got a 15.5% reduction from removing one lib!</p><p>As mentioned before, we use Storybook for developing and testing components independently. However, it should only be available in the local and staging environments. No end-user should be able to see it. Because of this, we use an ENVIRONMENT variable to control whether we enable this part of the app. While this works for restricting access, the bundler has no way of knowing the value of this variable. Because of this limitation, all of the Storybook code ends up going to our production bundle.</p><p>To remedy this, we isolated the import of this section to a single file. Then we created two versions of the file: one that includes Storybook and another one for production that just has a dummy component. To toggle between those when targeting production, we wrote a script that runs before the bundling step that swaps the two files. Through this method we were able to completely remove the Storybook code path from production, eliminating the node_modules dependency as well as all internal code configuring each story.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/362/1*asesI8TLiQyhcMbhYoOU3Q.png" /><figcaption>The updated storybook folder with two versions of the index file</figcaption></figure><p>With both these changes, we were able to bring our bundle size from 5.49 MB to 4.2 MB. Meaning our users will have faster app startup speeds and update downloads.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*77bkAU-9XM9fzBMd2LI-iw.png" /><figcaption>Final bundle totaling 4.2 MB in size</figcaption></figure><p>After all these improvements, we uploaded our app to the Play Store again. It was now reporting that our final APK size would be between 10.5 to 13.7 MB. An incredible ~60% reduction from our original 26.8 MB! This means we could potentially increase our install conversion rates by 3.75%, as indicated by the Google Play team’s article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uCxcMK-ctgpB4JatQwSJOw.png" /><figcaption>Comparison between our initial APK and the final AAB version with all improvements</figcaption></figure><h3>Closing thoughts</h3><p>As business-oriented Software Engineers, we know that sometimes the best decision for the company is to accumulate technical debt to evolve the product faster. This is especially true in early-stage startups like Mutual that are trying to find their product-market fit. But without monitoring this debt, you could make some big blunders, like bundling 2 MB of testing pictures and using a huge library unnecessarily. Don’t let technical debt get out of control and blow up in your face!</p><p>It is also common to tunnel vision and let pass by quick and easy opportunities to optimize what you already have. So a good idea is to periodically take a step back. Ascertain that you’re not missing out on quick improvements to the size, speed, or any aspect of your application. It took us only two days to analyze, plan out, and execute all of the above improvements which brought the size of our app down by up to 60%. It’s hard to think of anything that would bring so many tangible results for so little effort.</p><p><em>A special thank you to my colleagues at Mutual for proofreading and creating an all-around great environment for all of us to grow and improve every day.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d59adc2ed3d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/how-we-reduced-our-react-native-app-size-by-60-with-a-few-simple-fixes-3d59adc2ed3d">How we reduced our React Native app size by 60% with a few simple fixes</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[When not to follow best practices: Abstractions]]></title>
            <link>https://medium.com/@hugogrochau/when-not-to-follow-best-practices-abstractions-d06f0293537e?source=rss-43d5bc013bf2------2</link>
            <guid isPermaLink="false">https://medium.com/p/d06f0293537e</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[abstraction]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[startup]]></category>
            <dc:creator><![CDATA[Hugo Grochau]]></dc:creator>
            <pubDate>Mon, 19 Feb 2018 20:51:32 GMT</pubDate>
            <atom:updated>2018-08-05T18:24:59.519Z</atom:updated>
            <content:encoded><![CDATA[<p>As developers, we know the importance of following <em>best practices</em> and <em>design patterns</em>. They are the byproduct of decades of smart software engineers solving real problems. We use them as a way to manage complexity as our codebase grows. And also to make it easier for others ­­ — and our future selves ­ — to reason about our code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LmBD9OaRAJPnBYBoZwyZMw.jpeg" /><figcaption>Why is it so hard to find a stock image without spaghetti code?</figcaption></figure><p>But there’s no silver bullet. As with anything, there are costs and trade-offs. These become more noticeable when time is a scarce resource. For example, in an early-stage startup, the product is in a constant state of change. It’s more important to validate the idea than worry about writing perfect code. Thus we must adapt our development strategy accordingly.</p><h3>Abstractions are good… until they aren’t</h3><p>I like to think of abstractions as a black box. A good abstraction can conveniently hide inner complexity by exposing a simplified interface. Remembering every minuscule characteristic would make it impossible to build any large application. Imagine you had to know the whole of TCP/IP whenever you wanted to send a network request. You’d spend most of your time trying to figure out what the heck is an SYN-ACK instead of shipping something meaningful for your users. Every important software in history depends on well thought out abstractions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*Wg4516mszA0EN5i1YbvANQ.gif" /><figcaption>TCP handshake lifecycle</figcaption></figure><p>Yet there’s a caveat. Sometimes abstractions can be more trouble than they are worth.</p><p>Let’s suppose you’re in charge of developing the new site for your company. The team’s designer has drawn most of the screens already. Looking at these, you identify a pattern with all the text on the site. The sizes are all 12px, 14px, or 16px and they can be bold and/or italic. This seems like a good place to make an abstraction. So, since you’re making this site with React, you create a generic text component:</p><pre>&lt;Text size={&#39;small&#39;|&#39;medium&#39;|&#39;large&#39;} bold={true|false} italic={true|false} /&gt;</pre><p>and pat yourself on the back for a day’s work well done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2iGlGTSkn54dv0zWQFBF1A.jpeg" /><figcaption>Good job!</figcaption></figure><p>Next week, the product owner decides that the main page needs some improvement. The designer re-draws it with a 24px title and a subtitle that’s 18px and has a light-gray color. You then update your component to something like this:</p><pre>&lt;Text size={&#39;small&#39;|&#39;medium&#39;|&#39;large&#39;|&#39;xlarge&#39;|&#39;xxlarge&#39;} bold={true|false} italic={true|false} light={true|false} /&gt;</pre><p>The component is now starting to get a bit complex. The sizes do not grow in neat increments of 2 like before. And there are 3 different booleans that can be set independently along with the sizes. That means 2³ * 5 = 40 possible states to reason about.</p><p>Shortly after, someone points out there’s no “Terms and Conditions” page. The CEO doesn’t want users to actually read the terms and realize they are signing their digital soul over to the company. So you’re tasked with making the text 8px and reducing the height between lines. Reluctantly, you alter your component for the new specification.</p><pre>&lt;Text size={&#39;xsmall&#39;|&#39;small&#39;|&#39;medium&#39;|&#39;large&#39;|&#39;xlarge&#39;|&#39;xlarge&#39;} bold={true|false} italic={true|false} light={true|false} lineHeight={&#39;small&#39;|&#39;medium&#39;} /&gt;</pre><p>Now your once neat and tidy component has become an unwieldy monstrosity. You have 6 different sizes that map to arbitrary real (px) values and 96 total possible states. And if the pattern continues, it will get even worse in the future.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*__fkK7kotJv3VFIvYZ3PfA.jpeg" /><figcaption>Flying spaghetti monster (i.e. most codebases)</figcaption></figure><p>Now suppose you were to do the “dumb” thing: forget the abstraction altogether and just use CSS.</p><pre>.Title {<br> font-size: 24px;<br> font-weight: bold;<br> color: black;<br>}</pre><pre>.Subtitle {<br> font-size: 18px;<br> font-style: italic;<br> color: lightgray;<br>}</pre><p>Sure, it’s more repetitive and definitely not as sexy. But since you don’t know all the possible cases, you need something flexible. With CSS you get that flexibility, at the cost of more repetitiveness and verbosity. But now you can make changes without having to worry about keeping a neat abstraction (i.e. a Text component).</p><p>Don’t get me wrong, there are cases where making a Text component would be the best approach. Your designer could’ve made a well-defined styleguide from the start. Or you could be rewriting an old codebase with a new technology stack. But in both those cases, you have previous knowledge of the product before you implement it. Most of the time, teams are figuring it out as they go along.</p><p>Making good software is hard. Writing concise, correct, and readable code is difficult enough. But you also have to think about how it will fit alongside future changes. So before making any kind of abstraction I like to ask myself the following questions:</p><ul><li>Do I understand the domain? (Can I predict most of the use cases?)</li><li>Will it be stable? (Do I believe this abstraction won’t have to change to accomodate new use cases in the future?)</li><li>Will it be a lot better? (Is what I’m using causing enough pain to be worth the effort and risk of creating something new?)</li></ul><p>If the answer to any of these questions is “No”, then you might want to think twice. Remember: No abstraction is better than the wrong abstraction.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d06f0293537e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>