<?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 Akshay Hegde on Medium]]></title>
        <description><![CDATA[Stories by Akshay Hegde on Medium]]></description>
        <link>https://medium.com/@akshaynhegde?source=rss-c566f3dea98------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*KIlqknvjp31i-ojHLV2D4w.jpeg</url>
            <title>Stories by Akshay Hegde on Medium</title>
            <link>https://medium.com/@akshaynhegde?source=rss-c566f3dea98------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 12:58:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@akshaynhegde/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[Demystifying Code Generation On iOS]]></title>
            <link>https://medium.com/gojekengineering/demystifying-code-generation-on-ios-a538f1894f1e?source=rss-c566f3dea98------2</link>
            <guid isPermaLink="false">https://medium.com/p/a538f1894f1e</guid>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[mobile-development]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[code-generation]]></category>
            <dc:creator><![CDATA[Akshay Hegde]]></dc:creator>
            <pubDate>Wed, 08 Dec 2021 05:21:56 GMT</pubDate>
            <atom:updated>2022-02-18T05:04:00.630Z</atom:updated>
            <content:encoded><![CDATA[<h4>This blog will give you an insight into the basics of code generation and relevant tools.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gxW0IO9zhnCktvwDc7bHzw.png" /></figure><p>This article is aimed at helping you learn the basics and understanding the tools for generating code. This is geared towards Swift/iOS developers. However, the underlying concepts of generating code remain the same irrespective of the programming language.</p><h3>Let’s address this first: Why generate code when you can write it?</h3><h4>Boilerplate code</h4><p>Often, code is written for standard protocol generations such as implementing Equality or Codable for model classes.</p><p>One may want to create enums and structs and avoid string-based APIs for your resources.</p><h4>Consistency and productivity</h4><p>The generated code will always look the same irrespective of who generated it or when. The same conventions and code styling can be maintained. Changing the rules and conventions is easier, as you can make the change in the code generation logic and all that code can just be generated again with no effort.</p><h4>Single source of truth and ease of portability</h4><p>The code generation logic can be written in such a way that porting to a different language and generating code is simple. The logic of code generation remains the same, just the output can be any language, swift, Kotlin, or Dart.</p><p>This allows us to keep some shared logic and a single source of truth of the data that results in the code for any platform. For instance, At Gojek, we use the same JSON file to generate all the theme classes in iOS and Flutter.</p><p>This theme is the basis that allows us to style according to the <a href="https://www.behance.net/gallery/126129043/Asphalt-Aloha-Design-System">Asphalt Aloha Design system</a></p><h3>The basics</h3><p>The premise of generating code can be thought of as:</p><p><strong>Input + Templates (Written in Templating Language) = Output</strong></p><p><strong>Input</strong> can be a JSON schema, code or annotations in code, or anything that has the information for which you want to generate the code.</p><p><strong>Templates</strong> are just plain text with “placeholders” that allow you to “fill in the blanks” with the data from the input</p><p><strong>Templating languages</strong> allow you to consume input, transform them and fill them in those templates.</p><p><strong>Output</strong> is always plain <strong>Text</strong>.</p><h3>Learn with a simple example</h3><p>At Gojek, we have design system tokens shared in a JSON format that are ported from Figma.</p><p>Now we want code to be generated from this JSON, because we would not want to write code every time tokens are modified or when a new theme is introduced.</p><p>Consider an example JSON structure of colors:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*gy-deKSDySGgRHVDqBeR6w.png" /></figure><p><strong>Here the input is a JSON.</strong></p><p>Our code generation logic is simple, for every color token/ key in the JSON we want to write a var someColorToken: ColorToken { get } in a protocol.</p><p>So we write a <strong>template</strong>. For Swift, you can use <a href="https://stencil.fuller.li/en/latest/">Stencil</a>. The reason we are using this is that it comes with some nice built-in functions to write or transform the input specific to Swift. There is <a href="https://github.com/SwiftGen/StencilSwiftKit">SwiftStencilKit</a> that given even more built-in filters and transformers that make our job easier.</p><p>This is our template, Written to ColorTokens.stencil file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*UE4gjw8-_e0OU5jtbIU3vA.png" /></figure><p>The steps are:</p><ol><li>Read from the theme JSON and create an array of all keys.</li><li>Pass this Array and the above template to stencil.</li><li>Generate.</li></ol><p>Let’s look at the template a bit more closely. Note that <strong>everything is just text,</strong> a protocol skeleton that is written in swift.</p><p>This is just a for loop that loops over the Array we just passed in by reading the JSON. {% for name in names %} ... {% endfor %}</p><p>This is transforming each of that name/key into a variable name;{{name|snakeToCamelCase|lowerFirstLetter}} Here a fill_active_primary becomes filleActivePrimary</p><p>To recap, <strong>the template just some text written in any language/syntax you want</strong>, they have “placeholders” that are filled from reading and transforming the input.</p><p>After calling try template.render(context), the <strong>string output</strong> is written Colortokens.swift file.</p><h3>Code as the input</h3><p>Take the popular example of <a href="https://github.com/krzysztofzablocki/Sourcery">Sourcery</a> and let’s look at the example of how it generates <a href="https://cdn.rawgit.com/krzysztofzablocki/Sourcery/master/docs/equatable.html">Equatable</a>.</p><p>Sourcery depends on reading annotations in code. We parse this code, pass them to stencil as an input that will be used in the template to “fill in the blanks”.</p><p>How do I read swift code and pass it to Stencil?</p><ol><li><a href="https://github.com/apple/swift-syntax">Swift Syntax</a></li><li><a href="https://github.com/apple/sourcekit-lsp">SourceKit</a> / <a href="https://github.com/jpsim/SourceKitten">SourceKitten</a></li></ol><p>The above tools enable you to parse and get syntax information or the structure of the swift code that you can use for further processing. For example as a JSON.</p><h3>Code generation for Resources</h3><p>We have .string and plist files for localization, we have JSON files for every item in the asset catalog.</p><p>So with the same principle, we can read/parse these files, write some templates and generate code.</p><p>That’s exactly what <a href="https://github.com/SwiftGen/SwiftGen">SwiftGen</a> does.</p><h3>Finally…</h3><p>There are a lot more examples out there for code generation. e.g., <a href="https://github.com/uber/mockolo">Mockolo</a> generates mocks by reading annotated classes with Swift-Syntax.</p><p>Stencil is not the only templating language. <a href="https://mustache.github.io/">{{ mustache }}</a> is more widely used and available for many other languages. Stencil follows a very similar syntax to mustache.</p><p>For instance, <a href="https://github.com/seanhenry/SwiftMockGeneratorForXcode">SwiftMockGeneratorForXcode</a> is an Xcode plugin that to generates mocks and stubs for swift classes uses SourceKit and <a href="https://github.com/groue/GRMustache">Mustache</a>.</p><p>Note that in all these cases the <strong>fundamental concept remains the same</strong>.</p><p>Now that you know the basics process of how code generation works, I hope you start recognizing parts of your own project code that can just be generated instead of writing them. 🙌</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IPWwoozDxeKoXMtXEyrtLw.png" /></figure><p>Want to read more stories? Click <a href="https://www.gojek.io/blog">here</a>.</p><p>Oh, we’re hiring! Check out open job positions here:</p><figure><a href="https://bit.ly/gojekjobsblogs2022"><img alt="" src="https://cdn-images-1.medium.com/max/700/1*c2_eIvR2WDUWnP6YmzcGpQ.jpeg" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a538f1894f1e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/gojekengineering/demystifying-code-generation-on-ios-a538f1894f1e">Demystifying Code Generation On iOS</a> was originally published in <a href="https://medium.com/gojekengineering">Gojek Product + Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[API Design: Here’s why mobile developers should care]]></title>
            <link>https://medium.com/gojekengineering/api-design-why-mobile-developers-should-care-c4a75ad89a0d?source=rss-c566f3dea98------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4a75ad89a0d</guid>
            <category><![CDATA[api-design]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[tech]]></category>
            <dc:creator><![CDATA[Akshay Hegde]]></dc:creator>
            <pubDate>Sun, 13 Sep 2020 12:55:27 GMT</pubDate>
            <atom:updated>2020-12-15T05:20:48.733Z</atom:updated>
            <content:encoded><![CDATA[<h4>The ‘why’ and ‘how’ of designing APIs to build great apps.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*soKVMjU3pG4KGSBeCNrG7g.png" /></figure><p>If you’re a mobile developer, do a mental replay of the discussion your team usually has before building a new feature or an app. What is discussed the most?</p><blockquote>What architecture to use? MVC? MVVM? VIPER?<br>What design patterns to follow for this feature?<br>What external libraries or frameworks to use?<br>In the iOS world: What dependency management solution do we choose?<br>Do we build UI on XIB? Code?</blockquote><p>Endless questions — all of them important to build something worthwhile. After intense meetings and deliberations, you find an answer to all these questions. Finally, you’re ready to code. 👩‍💻👨‍💻</p><p>Before everyone goes heads down in work, it’s crucial to understand one thing:</p><blockquote><strong><em>Every design pattern and architecture can be ruined with badly designed classes and APIs.</em></strong></blockquote><p>In the rush of completing the app and the fact that we spent a ton of time picking all the right patterns, we tend to ignore a subtle but important aspect of writing beautiful code.</p><p><strong>API design is not just designing restful APIs. Every class, framework, or module you code in the app has APIs.</strong></p><h3><strong>Clear, consistent, and predictable</strong></h3><p>Design the APIs not just for yourself or your team members, but for other developers and users. Consider that someone else will call this method or use this class.</p><p>Keep the naming convention across classes, variables, parameters, and methods consistent. For instance, you name certain kinds of classes as coordinator and then at another position name another class with a similar responsibility as a manager. Better yet, avoid generic naming such as ‘<strong>manager</strong>’ or ‘<strong>common</strong>’.</p><p>It’s not enough to say a class follows a single responsibility principle, if the name of the class doesn’t clearly define it. When you have a large team, some other developers in the future may interpret this responsibility differently than you initially did.</p><blockquote>Functions should always be predictable. By reading the signature, one should know what it does.</blockquote><p>Don’t handle every obscure edge case inside your function. It’s better to throw the right kind of exception from the function, instead of handling it in some very specific way, which may return undesired output. Provide different APIs that can cater to some special cases of inputs instead of handling everything in one.</p><h4><strong>A word on unit testing</strong></h4><p>A unit test is when we “test a unit of code for specific input and assert on it to match a known output”.</p><p>When you have an API written to do one thing, i.e., handle a specific input and return a predefined output, your tests will be more robust, have better coverage and you can trust them more.</p><h3><strong>Documentation</strong></h3><p>We’ve all heard someone say:</p><blockquote>“Your code itself should be the documentation.”</blockquote><p>While one should certainly aspire to write clear, easy to read, and understandable code, there needs to be legible documentation.</p><p>It’s vital to document APIs and classes. Use the documentation to write about the code complexity if there are exceptions that may be thrown, link to other related APIs. Even a simple usage code snippet will go a long way.</p><h3><strong>Access control and extension points</strong></h3><p><strong>Open, public, internal, private</strong>: Know that these access controls exist so we can design cleaner APIs to implement encapsulation principles.</p><p>Think for a moment about how you want your class to be extended. Do you expect users of your APIs to subclass your class and extend it? Or just expose it such that one can only extend without subclassing it?</p><p>If you don’t expose internal details or working of your classes, you can improve or change them without affecting the external usages of your APIs.</p><p>Using the right access controls will improve the performance of your code.</p><p>Compilers can make better optimisations when we declare a class as <em>final</em> or a method as <em>internal</em>. This will help reduce runtime complexities to find out which method/class is being invoked.</p><p>The more hints you provide to the compilers on the usage of your code, the better optimised the generated machine code will be.</p><h3><strong>API Evolution</strong></h3><p>Make<a href="https://docs.swift.org/swift-book/ReferenceManual/Attributes.html"> use of attributes</a> and mark old class or methods as deprecated/obsolete.</p><p>When you’re rewriting an existing class with a completely new logic, instead of changing multiple methods inside the class with if/else statements, just create a new class and name it as ‘MyOldClassNameV2’. Once ready, you can experiment using the new class and put the entire implementation behind a single <a href="https://en.wikipedia.org/wiki/Feature_toggle">feature flag</a>.</p><p>If the scope of this refactoring is only limited to a single function of a class, introduce a new function instead of an if/else statement or a new parameter inside the function. Once you’re ready, you can experiment and put the call for the new function behind the feature flag.</p><p>Remember that experimentations must always be done at the call site or where this class of functions is being used. Once you are happy with your function/change, deprecate the old method/class.</p><h3><strong>Endnotes</strong></h3><p>Learning by reading and being curious is an endless process, especially when we have access to an abundance of high-quality professional code.</p><blockquote>When you use this networking or database library in your code, pay attention to the method signatures, documentation of their classes, and conventions that are being followed.</blockquote><p>They teach you a lot and help you build a natural ability to write clean code.</p><p>In your team, do code reviews. It’s really useful and important even if it’s just a small team of three people. Read each other’s code and keep the API design guidelines in mind as you do.</p><p>Here are some of these guidelines mentioned above and many more explained with better examples. So I encourage you to just keep these at the back of your mind when you write your code next.</p><ul><li><a href="https://developer.apple.com/wwdc19/415">Modern Swift API Design</a></li><li><a href="https://swift.org/documentation/api-design-guidelines/">Swift API Design guidelines</a></li><li><a href="https://github.com/dotnet/runtime/blob/master/docs/coding-guidelines/framework-design-guidelines-digest.md">Framework design guidelines</a></li><li><a href="https://dart.dev/guides/language/effective-dart/design">Effective Dart: Design</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IPWwoozDxeKoXMtXEyrtLw.png" /></figure><p>Curious to know how we build our Gojek #SuperApp? <a href="https://blog.gojekengineering.com/">Here’s</a> how.<br>Want to build it with us? <a href="http://gojek.jobs/">Join us</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4a75ad89a0d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/gojekengineering/api-design-why-mobile-developers-should-care-c4a75ad89a0d">API Design: Here’s why mobile developers should care</a> was originally published in <a href="https://medium.com/gojekengineering">Gojek Product + Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>