<?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 Vibhanshu Sharma on Medium]]></title>
        <description><![CDATA[Stories by Vibhanshu Sharma on Medium]]></description>
        <link>https://medium.com/@vibhanshusharma_93861?source=rss-4633d272c9c1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LCrRjVbaAEUQaKPdB-VT1g.jpeg</url>
            <title>Stories by Vibhanshu Sharma on Medium</title>
            <link>https://medium.com/@vibhanshusharma_93861?source=rss-4633d272c9c1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 20:52:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vibhanshusharma_93861/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[Why there is no ternary operator in kotlin?]]></title>
            <link>https://medium.com/@vibhanshusharma_93861/why-there-is-no-ternary-operator-in-kotlin-80e9f1409a8e?source=rss-4633d272c9c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/80e9f1409a8e</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[operators]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Vibhanshu Sharma]]></dc:creator>
            <pubDate>Sat, 16 Oct 2021 07:45:27 GMT</pubDate>
            <atom:updated>2021-10-16T07:45:27.643Z</atom:updated>
            <content:encoded><![CDATA[<p>I was always curious that why kotlin does not have a ternary operator like java.</p><p>It was surprising for me as kotlin usually have additional features as compared to java, but it did not have an operator that is present in java.</p><p>Kotlin official documentation gave me the answer to this, the following is the reason given in the kotlin official documentation.</p><blockquote>In Kotlin, if is an expression: it returns a value. Therefore, there is no ternary operator (condition ? then : else) because ordinary if works fine in this role.</blockquote><p>I will try to explain the statement above using a coding example</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MbQRzwLFdR96HQq_rVD0iA.png" /></figure><p>So, we can see from the example using ‘<em>if as an expression’, </em>fulfils the use-<em> </em>case of the ternary operator, so why need an extra operator.</p><p>So in kotlin we are good to go without the ternary operator.</p><p>You can read more about kotlin conditional statements here-</p><p><a href="https://kotlinlang.org/docs/control-flow.html#if-expression">https://kotlinlang.org/docs/control-flow.html#if-expression</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80e9f1409a8e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Improve PDF rendering in Android app using Pagination]]></title>
            <link>https://medium.com/getpowerplay/improve-pdf-rendering-in-android-app-using-pagination-ba548112899f?source=rss-4633d272c9c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba548112899f</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[pdf]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[optimization]]></category>
            <dc:creator><![CDATA[Vibhanshu Sharma]]></dc:creator>
            <pubDate>Tue, 12 Oct 2021 09:07:55 GMT</pubDate>
            <atom:updated>2021-10-12T09:07:55.038Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently at <a href="https://medium.com/u/724960ebaa4d">Powerplay</a>, many users gave us feedback that their progress report was loading very slowly.</p><p>To give you all context, in the Powerplay app, a progress report is a pdf file that contains information about the user’s project.</p><p>So we decided to accelerate the rendering speed of PDF files in our app and we were able to decrease the rendering time of these files by almost 70 -80%.</p><p>In this blog, I will tell you how we were able to achieve this.</p><p>First I will tell you about the mistakes that we were doing earlier and then I will tell you how did we rectify them, which made PDF rendering in our app super fast.</p><p><strong>What we were doing initially?</strong></p><p>We got the s3 link(storage link of our pdf) from the server and then we downloaded the pdf(using <em>Download Manager API)</em> and after the download was finished, we used to convert each page of the downloaded pdf into images(Using Pdf Renderer library) and then we were showing the pdf to the user as a list of images by using recycler view.</p><p>Earlier we used to convert all the pages of pdf into images at one time and then populate the pages in the list.</p><p>The problem with this approach was that the function that returned us the bitmap of the page was a heavy function and it took some time to complete( around <em>5 seconds for each page to be precise</em>), so for large pdfs (having multiple pages), the process of converting all the pages to images first and then loading the list of images into recycler view was time taking and it lead to very slow loading of the report and hence hampered user experience.</p><p><strong><em>What we did to improve Pdf rendering speed?</em></strong></p><p>We came to a conclusion that there is no need of loading all the pages of the pdf at a time because usually, 2 pages of the pdf are in focus on the mobile screen at a time, so keeping all the pages of the user preloaded made no sense.</p><p>Now the challenge came how to develop our own custom pagination logic, for that we used the P<em>aging library of android</em>, and made our custom <em>datasource</em><strong> </strong>class.</p><p>This is the custom datasource class we made for developing our own custom pagination logic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddb07ae42a6de3406ffc773118bfcadd/href">https://medium.com/media/ddb07ae42a6de3406ffc773118bfcadd/href</a></iframe><p><strong>Quantitative Analysis</strong></p><p>To quantitatively define by how much we were able to make pdf rendering fast, let’s assume time taken by <em>getBitmap(page:Page) f</em>unction(it converted pdf page into image) to be T(we are considering the time of this function as this is the most time-consuming function and it will decide the rendering time of pdf) and let your pdf be of P pages.</p><p>So in the previous case pdf loading time was <em>(P*T)(all the pages are loaded at a time, </em>and in case of pagination, it takes <em>2*T(as only two pages are loaded a time) </em>time so our time is reduced by <em>(P-2)*T</em>.</p><p>In terms of percentage, the percentage of pdf rendering was reduced by</p><p><strong><em>(P-2)*T/P*T = (P-2)/P %</em></strong></p><p>where:<br>P =number of pages in pdf<br>T = time taken by getBitmap(page) function</p><p>After debugging I found that on average the getBitmap() function takes 5 seconds to execute.</p><p>Therefore T =5 seconds.</p><p>So we can see that for a pdf of P = 10 pages it would take <em>50 seconds</em>( P*T) and in the case of our new (lazy loading) approach, it takes only<em> 10 seconds</em>.</p><p>So we can see that we have reduced the loading time by <strong><em>80%.</em></strong></p><p>That’s the magic of pagination.</p><p>If you want to learn more about pagination you can refer to android documentation -</p><p><a href="https://developer.android.com/topic/libraries/architecture/paging/v3-overview">https://developer.android.com/topic/libraries/architecture/paging/v3-overview</a></p><p>Have any doubts or suggestions regarding the blog? You can write them in the comments section.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba548112899f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/getpowerplay/improve-pdf-rendering-in-android-app-using-pagination-ba548112899f">Improve PDF rendering in Android app using Pagination</a> was originally published in <a href="https://medium.com/getpowerplay">Powerplay</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding as T? and as? T operator in Kotlin]]></title>
            <link>https://medium.com/@vibhanshusharma_93861/understanding-as-t-and-as-t-operator-in-kotlin-6e27c97e5f3d?source=rss-4633d272c9c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e27c97e5f3d</guid>
            <category><![CDATA[type-casting]]></category>
            <category><![CDATA[nullability]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[operators]]></category>
            <dc:creator><![CDATA[Vibhanshu Sharma]]></dc:creator>
            <pubDate>Wed, 06 Oct 2021 06:44:39 GMT</pubDate>
            <atom:updated>2021-10-06T06:44:39.649Z</atom:updated>
            <content:encoded><![CDATA[<p>Initially, I used to use <em>as T?</em> and <em>as? T</em> interchangeably thinking they have the same use case.</p><p>At first glance because of ‘?’ we think that both are the same and both are used to cast a variable to the nullable variables(T?) but the working of both operators are very different.</p><p>The <strong><em>as? T</em></strong><em> </em>(or the safe cast operator) is used when we don&#39;t want our code to crash, in case the variable cannot be cast to type T.</p><p>In the case of<strong><em> as T? </em></strong>(or the unsafe cast operator), the app crashes if the variable cannot be cast to T?.</p><p>I have tried to show the differentiation of both the operators through the following images.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cJHxUQoJdHe5H7rCj9HboA.png" /><figcaption>In case of as? , null is returned as we fail to cast to Int</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g_If4prxHGRTBSu2NMDuEQ.png" /><figcaption>In the case of ‘as’, an exception is thrown.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CN1oIyccMjgJ0ACfTf4gWw.png" /><figcaption>In this also, null is returned because of as? operator</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b0taCq_EpBi5gSf8hM1oMw.png" /><figcaption>In this case also because of ‘as’, an exception is thrown.</figcaption></figure><p>You can read more about the unchecked cast here — <a href="https://kotlinlang.org/docs/typecasts.html#unchecked-casts">https://kotlinlang.org/docs/typecasts.html#unchecked-casts</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e27c97e5f3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[One line Swap Algorithm in Kotlin]]></title>
            <link>https://medium.com/@vibhanshusharma_93861/one-line-swap-algorithm-in-kotlin-e1cf6cc64708?source=rss-4633d272c9c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1cf6cc64708</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[swapping]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Vibhanshu Sharma]]></dc:creator>
            <pubDate>Tue, 28 Sep 2021 05:25:53 GMT</pubDate>
            <atom:updated>2021-09-28T05:37:03.511Z</atom:updated>
            <content:encoded><![CDATA[<h3>One Line Swap in Kotlin</h3><p>We all know that kotlin saves us from writing lots of boilerplate code that we have to write in java.</p><p>Swapping variables is one such example -</p><p>If we see a basic swapping algorithm, we have to create a temporary variable to sort and on top of it write 3 lines of code.</p><pre>var temp  = a;<br>   a  = b;<br>   b  = temp;</pre><p>Kotlin saves us from making a temporary object and also reduces the lines of code we have to write to swap.</p><p>We can swap two variables in just one line in kotlin(by using scope functions) as shown below</p><pre>secondVar = firstVar.apply{firstVar = secondVar} </pre><p>or</p><pre>secondVar = firstVar.also {firstVar = secondVar}</pre><p>Now you might be thinking, how do the above lines swap the two variables?</p><p>The fact that scope functions like <em>apply</em> and <em>also</em> create a temporary scope for a variable inside a lambda and returns the object which was scoped, enables the swapping of variables.</p><p>Let&#39;s see what is actually happening -</p><p><strong><em>Also</em></strong> and <strong><em>apply</em></strong> returns the object on which they are applied so when we do <em>.apply(or .also)</em> on <em>firstVar</em>, a temporary scope is created in which we can access <em>firstVar(by using this variable or directly) </em>and in that scope, we are reassigning the reference of <em>firstVar</em> to the object <em>secondVar</em> is pointing.</p><p>Finally, <em>secondVar </em>is assigned to the object that is accessible inside the scope i.e the object to which <em>firstVar</em> was previously assigned, as<em> also</em> and <em>apply</em> returns the object on which they were applied.</p><pre>fun main() {<br>    var firstVar= 5<br>    var secondVar=6<br>    <br>    //Swapping a and b<br>    secondVar = firstVar.apply{firstVar = secondVar} <br>    // can also be done by also</pre><pre>    print(firstVar) // prints 6<br>    print(secondVar) // prints 5 }</pre><p>If you want to know more about extension functions, <br>You can read about them here —</p><p><a href="https://kotlinlang.org/docs/scope-functions.html"><strong><em>https://kotlinlang.org/docs/scope-functions.html</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1cf6cc64708" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>