<?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[smallprogtips - Medium]]></title>
        <description><![CDATA[Bite-sized programming tips and tricks - Medium]]></description>
        <link>https://medium.com/smallprogtips?source=rss----fbc4424e0fcb---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>smallprogtips - Medium</title>
            <link>https://medium.com/smallprogtips?source=rss----fbc4424e0fcb---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 21:55:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/smallprogtips" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Number readability with underscores in Python 3]]></title>
            <link>https://medium.com/smallprogtips/number-readability-with-underscores-in-python-3-abf1fe5bdc11?source=rss----fbc4424e0fcb---4</link>
            <guid isPermaLink="false">https://medium.com/p/abf1fe5bdc11</guid>
            <category><![CDATA[programming-tips]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[numbers]]></category>
            <dc:creator><![CDATA[Călina Cenan]]></dc:creator>
            <pubDate>Wed, 05 Jun 2019 12:50:03 GMT</pubDate>
            <atom:updated>2019-06-05T12:50:03.661Z</atom:updated>
            <content:encoded><![CDATA[<p>Here is a neat trick for Python numeric values: you can improve readability of large numbers by using underscores as separators, like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*ir-_I_VfVAYzmw5zni6llg.png" /><figcaption>caption taken from repl.it: <a href="https://repl.it/languages/python3">https://repl.it/languages/python3</a></figcaption></figure><p>Underscores will be “ignored” by Python when interpreting the numerical value, but for the programmer, this is much more easy on the eye. And yes, it works for floats too. Here is the <a href="https://www.python.org/dev/peps/pep-0515/">official link</a> with more examples.</p><p>PS: You are always free to opt for the scientific notation of large numbers, especially those with trailing zeroes, which are pretty straightforward to read. However, I find this notation to be very useful for accounting and financial apps, where large numbers are common but rarely used in their scientific form.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=abf1fe5bdc11" width="1" height="1" alt=""><hr><p><a href="https://medium.com/smallprogtips/number-readability-with-underscores-in-python-3-abf1fe5bdc11">Number readability with underscores in Python 3</a> was originally published in <a href="https://medium.com/smallprogtips">smallprogtips</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Django REST Framework — Programatically get related entity and serializer]]></title>
            <link>https://medium.com/smallprogtips/django-rest-framework-programatically-get-related-entity-and-serializer-638bb9c2c115?source=rss----fbc4424e0fcb---4</link>
            <guid isPermaLink="false">https://medium.com/p/638bb9c2c115</guid>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[Călina Cenan]]></dc:creator>
            <pubDate>Tue, 07 Aug 2018 11:59:53 GMT</pubDate>
            <atom:updated>2018-08-07T11:59:43.215Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uGeFnJKIdoDN0dUme5lCzw.jpeg" /><figcaption><a href="https://www.deviantart.com/tellofvisions/art/infinite-realities-nexus-point-254324401">infinite realities nexus point</a> by <a href="https://www.deviantart.com/tellofvisions">TellOfVisions</a></figcaption></figure><p>Let’s say we have an entity nexus point, i.e. many other entities that have o2m relations to the same entity. Some use cases would be implementing a Note model that relates to Users, Events, Projects etc. Adding foreign keys is fine up until one point, but, since all but one of these foreign keys will be always None, let’s implement a model where the entity is retrieved programatically.</p><pre>class Note(Date):<br>    ...</pre><pre>    entity_type = models.CharField(max_length=64)<br>    entity_id = models.IntegerField()</pre><p>To get the related Note for a User, we simply filter by Note.objects.get(entity_type=User, entity_id=user.id), similarly for other entities.</p><p>But, when retrieving the Note from a REST API, I want the related entity properly serialized, not just the type and id fields. With Django REST Framework, this only takes a few lines. In serializers.py:</p><pre>from django.apps import apps<br>from rest_framework import serializers</pre><pre><br>class NoteSerializer(serializers.ModelSerializer):<br>    ...<br>    entity = serializers.SerializerMethodField()</pre><pre>    def get_entity(self, obj):<br>        pk = obj.entity_id<br>        class_name = obj.entity_type<br>        model_class = apps.get_model(&#39;app&#39;, class_name)<br>        model = model_class.objects.get(pk=pk)</pre><pre>        serializer_class = class_name + &#39;Serializer&#39;  # assumes the &quot;ModelNameSerializer&quot; is the name of the serializer class in the current file</pre><pre>        return globals()[serializer_class](model).data</pre><pre>class Meta:<br>        model = Note<br>        fields = (..., &#39;entity&#39;, ...)</pre><p>Using the SerializerMethodField and defining a function prefixed with get_ will trigger the result of that function to be represented in the entity key of the parent NoteSerializer. Inside it, we simply retrieve our related model and serialize it using the ModelNameSerializer. This means a User will always be serialized as a User, an Event as an Event and a Project as a Project.</p><p>I’ve spent a couple of hours looking for this solution, so I hope it helps in similar use cases.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=638bb9c2c115" width="1" height="1" alt=""><hr><p><a href="https://medium.com/smallprogtips/django-rest-framework-programatically-get-related-entity-and-serializer-638bb9c2c115">Django REST Framework — Programatically get related entity and serializer</a> was originally published in <a href="https://medium.com/smallprogtips">smallprogtips</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sprintf is your friend]]></title>
            <link>https://medium.com/smallprogtips/sprintf-is-your-friend-3af2a63fe6d4?source=rss----fbc4424e0fcb---4</link>
            <guid isPermaLink="false">https://medium.com/p/3af2a63fe6d4</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[tips-and-tricks]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Călina Cenan]]></dc:creator>
            <pubDate>Mon, 08 May 2017 16:44:50 GMT</pubDate>
            <atom:updated>2016-11-29T17:29:40.377Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*VA6CZKUF09QotdKnsny1qg.jpeg" /><figcaption>Usain Bolt, famous sprinter. If he were a programmer, he’d definitely be a sprintf-er.</figcaption></figure><p>I’ve seen many developers steer clear of the sprintf function. In this short article I will try to convince you thatsprintf helps in many cases, using mainly PHP as an example (but I talk about other languages as well).</p><h3>Natural readability</h3><p>The following return statements are equivalent.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb9939893be0b7f3b4559750a269a34a/href">https://medium.com/media/eb9939893be0b7f3b4559750a269a34a/href</a></iframe><p>However, by the time you’ve finished reading the second one, you’re probably wondering about correct spacing, coding standards regarding line length and the meaning of life itself.</p><h3>Conversions and number formatting</h3><p>The following return statements are equivalent:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98b7f2c2178f8e13ea1a68f520bf1d84/href">https://medium.com/media/98b7f2c2178f8e13ea1a68f520bf1d84/href</a></iframe><p>However, the latter requires developers to know the number_format function and its parameter list. At least PHP is able to perform conversions, but, for example, Python disallows the following:</p><p>return “The number is “ + 2 because it doesn’t automatically convert 2 to the string “2”, which means the following solutions work:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/acb515f3bdedf5082c3547df2f12eea9/href">https://medium.com/media/acb515f3bdedf5082c3547df2f12eea9/href</a></iframe><p>Considering my first point about readability and how the two solutions scale to more numbers, I will let you decide which is better.</p><h3>Maintainability of conditionals</h3><p>Consider the following use case. Upon creating or updating a model in our application, we want to show a message: “Item was created” or “Item was updated.” Here are two solutions, separated by a line of comment characters (“#”). I am using the ternary operator because it’s more concise:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56fe2f5db3f74fee4155df92a3d12940/href">https://medium.com/media/56fe2f5db3f74fee4155df92a3d12940/href</a></iframe><p>The first solution contains text duplication, which means it is easy to forget to edit <strong>both</strong> messages. The second option is better because it does not duplicate the text, so modifying the message in just this one place will suffice.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3af2a63fe6d4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/smallprogtips/sprintf-is-your-friend-3af2a63fe6d4">Sprintf is your friend</a> was originally published in <a href="https://medium.com/smallprogtips">smallprogtips</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The manageable .gitignore file]]></title>
            <link>https://medium.com/smallprogtips/the-manageable-gitignore-file-414e8ac7c4b2?source=rss----fbc4424e0fcb---4</link>
            <guid isPermaLink="false">https://medium.com/p/414e8ac7c4b2</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[tips-and-tricks]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Călina Cenan]]></dc:creator>
            <pubDate>Mon, 08 May 2017 16:44:43 GMT</pubDate>
            <atom:updated>2016-11-28T16:24:43.532Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4b4qeqvfqY7CK6-nHTozsg.jpeg" /></figure><p>How do we keep a readable and manageable .gitignore in a team of many members? The following solution takes advantage of the 3 different files where ignored paths may be stored:</p><h3>Use the project’s .gitignore for:</h3><p><strong>Vendor modules, logs, upload folders and environment configuration files</strong>. They should be ignored because they are either not relevant, or otherwise automatically managed and updated on other repository clones.</p><h3>Use your global .gitignore for:</h3><p><strong>OS or IDE-specific indexing and description files.</strong> Your co-workers with a Windows system shouldn’t be concerned with your Mac-related settings (.DS_Store), just as users of Atom shouldn’t care about the .idea folder automatically created by IntelliJ. Adding these to the .gitignore will only crowd the file unnecessarily.</p><p>Your global ignore settings usually reside in ~/.gitconfig.</p><h3>Use the repository exclude file for:</h3><p><strong>Custom files you use in your workflow but should not be shared to other repositories</strong>. E.g. project-specific like documentation-todos.txt, language or framework-specific like*pyc.</p><h3>Need more help?</h3><p>Read more and find help generating a .gitignore:</p><ul><li><a href="https://git-scm.com/docs/gitignore">Git - gitignore Documentation</a></li><li><a href="https://www.gitignore.io/">gitignore.io</a></li><li><a href="https://zellwk.com/blog/gitignore/">What to add to your Gitignore File | Zell Liew</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=414e8ac7c4b2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/smallprogtips/the-manageable-gitignore-file-414e8ac7c4b2">The manageable .gitignore file</a> was originally published in <a href="https://medium.com/smallprogtips">smallprogtips</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Git checkout to latest branch]]></title>
            <link>https://medium.com/smallprogtips/git-checkout-to-latest-branch-8a185b14a87e?source=rss----fbc4424e0fcb---4</link>
            <guid isPermaLink="false">https://medium.com/p/8a185b14a87e</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[tips-and-tricks]]></category>
            <dc:creator><![CDATA[Călina Cenan]]></dc:creator>
            <pubDate>Mon, 08 May 2017 16:44:36 GMT</pubDate>
            <atom:updated>2016-11-16T17:25:28.806Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*WfnhsoLtdrlzy919tByZsg.jpeg" /></figure><p>I’m embarrassed to have only found out about this cool gimmick. Using the command line, when you cd to a given path, you can always revert to the previous path using cd - , saving precious time in retyping it.</p><p>In Git, there’s the exact same use case: switching between branch with longer names can be exhausting for any lazy-typing developer and sometimes you just need to go back, nothing fancier. My motto is “think more, type less”, so I now use (you’ve guessed it) git checkout - to switch to the previous branch. A few microseconds a day stack up!</p><p>So remember: git checkout — to switch to the previous branch.</p><p>Credits: <a href="http://stackoverflow.com/a/7207542/2887012">http://stackoverflow.com/a/7207542/2887012</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a185b14a87e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/smallprogtips/git-checkout-to-latest-branch-8a185b14a87e">Git checkout to latest branch</a> was originally published in <a href="https://medium.com/smallprogtips">smallprogtips</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MySQL UPDATE based on data from different tables]]></title>
            <link>https://medium.com/smallprogtips/mysql-update-based-on-data-from-different-tables-83a169514492?source=rss----fbc4424e0fcb---4</link>
            <guid isPermaLink="false">https://medium.com/p/83a169514492</guid>
            <category><![CDATA[migration]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[tips-and-tricks]]></category>
            <dc:creator><![CDATA[Călina Cenan]]></dc:creator>
            <pubDate>Mon, 08 May 2017 16:44:28 GMT</pubDate>
            <atom:updated>2016-11-04T16:04:52.316Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BBN93U5g3z6kCPcazk4Esg.jpeg" /></figure><p>Just like my previous article on the meaning of COALESCE, the following problem is one of those brain teasers that keeps developers busy for a while, frantically searching for answers, trying and re-trying various approaches and then… well, after solving, it seems stupidly simple. This recipe will definitely help those of you tackling delicate SQL data migrations. So, here goes:</p><p>Assume your database is going through the following changes: You have a Users table, containing first and last names. You also have Accounts, which up until now only had an AccountNumber (IBAN) and an OwnerId. So now there’s this new column, called Account Nickname, where people will be able to input a friendlier name for their accounts, which is irrelevant to the bank but meaningful to the user. For the moment, you want to populate all Accounts to contain “(FirstName) (LastName)’s Bank Account”.</p><p>In some SQL flavours, you can do an UPDATE directly from a virtual table. However, in MySQL it’s a bit different:</p><p>UPDATE `accounts`, `users` SET `accounts`.`nickname` = CONCAT_WS(‘ ‘, `users`.`first_name`, `users`.`last_name`, “ ‘s Bank Account”) WHERE `accounts`.`user_id` = `users`.`id`;</p><p>Note how both the tables involved are mentioned in the first part of the UPDATE statement and what you’d usually consider JOIN criteria goes inside the WHERE. Neat, huh? Credits for the idea: <a href="http://stackoverflow.com/a/12748310/2887012">http://stackoverflow.com/a/12748310/2887012</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=83a169514492" width="1" height="1" alt=""><hr><p><a href="https://medium.com/smallprogtips/mysql-update-based-on-data-from-different-tables-83a169514492">MySQL UPDATE based on data from different tables</a> was originally published in <a href="https://medium.com/smallprogtips">smallprogtips</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Parsing YAML files in Laravel 5]]></title>
            <link>https://medium.com/smallprogtips/parsing-yaml-files-in-laravel-5-bca2ead5242e?source=rss----fbc4424e0fcb---4</link>
            <guid isPermaLink="false">https://medium.com/p/bca2ead5242e</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[yaml]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Călina Cenan]]></dc:creator>
            <pubDate>Mon, 08 May 2017 16:44:21 GMT</pubDate>
            <atom:updated>2016-10-21T07:58:23.536Z</atom:updated>
            <content:encoded><![CDATA[<p>I decided to make this small guide for those in search of YAML support in Laravel 5, since most of the articles online describe outdated processes, using older Laravel versions or the need to install extensions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*i07vtyWsxFPMVE-JZ9f1SA.png" /></figure><p>Laravel 5 is a modern PHP web framework, one of the most popular ones, alongside Symfony, Codeigniter, Yii and others. Its creators are famous for their openness towards employing good solutions instead of reinventing the wheel, so Laravel is easily extensible and friendly to external component integration.</p><p>So, without further ado, here is how you get YML file support in Laravel 5:</p><ol><li>Install the Symfony YAML Component using composer:</li></ol><pre>composer require symfony/yaml</pre><p>2. In config/app.php under “aliases”, add the following entry:</p><pre>‘Yaml’ =&gt; ‘Symfony\Component\Yaml\Yaml’</pre><p>3. In your desired controller/service:</p><pre>use Yaml;</pre><pre>[ … ]</pre><pre>$yamlContents = Yaml::parse(file_get_contents(‘filepath’));</pre><p>And you’re done! Far simpler than any of the old solutions. This example covers only YAML parsing, but feel free to use any of the features documented in the <a href="http://symfony.com/doc/current/components/yaml.html">Symfony YAML Component Documentation</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bca2ead5242e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/smallprogtips/parsing-yaml-files-in-laravel-5-bca2ead5242e">Parsing YAML files in Laravel 5</a> was originally published in <a href="https://medium.com/smallprogtips">smallprogtips</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Untangling conditionals with IF-inversion]]></title>
            <link>https://medium.com/smallprogtips/untangling-conditionals-with-if-inversion-cbba9c5f8ba5?source=rss----fbc4424e0fcb---4</link>
            <guid isPermaLink="false">https://medium.com/p/cbba9c5f8ba5</guid>
            <category><![CDATA[learn-programming]]></category>
            <category><![CDATA[tips-and-tricks]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Călina Cenan]]></dc:creator>
            <pubDate>Mon, 08 May 2017 16:43:27 GMT</pubDate>
            <atom:updated>2016-10-19T07:18:22.973Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KlMHbNlpFwbQ0PM7hI5UIQ.jpeg" /></figure><p>This is a very common trick, but there are still some developers around who struggle with applying it. The idea is to cut through the complexity of IF/ELSE structures and make the code more understandable, by treating the exceptional cases first.</p><p>Here are two variants of the same validation function, which should reject a user’s application if they are underage or if they have a previously approved application. They are written in PHP but the concepts can be applied to any programming language:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec1de0de9f90b9b7f93dbdf344e011b0/href">https://medium.com/media/ec1de0de9f90b9b7f93dbdf344e011b0/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1010b1f3565571517673e5a45f7a9a40/href">https://medium.com/media/1010b1f3565571517673e5a45f7a9a40/href</a></iframe><p>The result is the same, as you can see for yourself by running the full-length gist from here: <a href="https://gist.github.com/calina-c/96a1e945a55a41cada6db92531df1d92">https://gist.github.com/calina-c/96a1e945a55a41cada6db92531df1d92</a></p><p>The steps are as follows: identify the possible failure cases. Treat them using a simple IF structure and use “return” to stop execution of the validation function. Rinse and repeat. In the end you are left with a single case, which can be treated directly, without the need for “else” blocks.</p><p>The length of the functions is more or less equivalent, but the readability is highly improved and your fellow developers will thank you for your clarity.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cbba9c5f8ba5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/smallprogtips/untangling-conditionals-with-if-inversion-cbba9c5f8ba5">Untangling conditionals with IF-inversion</a> was originally published in <a href="https://medium.com/smallprogtips">smallprogtips</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using COALESCE for neat SQL queries]]></title>
            <link>https://medium.com/smallprogtips/using-coalesce-for-neat-sql-queries-a625312b9de5?source=rss----fbc4424e0fcb---4</link>
            <guid isPermaLink="false">https://medium.com/p/a625312b9de5</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Călina Cenan]]></dc:creator>
            <pubDate>Mon, 08 May 2017 16:43:08 GMT</pubDate>
            <atom:updated>2016-10-19T06:31:43.695Z</atom:updated>
            <content:encoded><![CDATA[<p>A lot of times, when learning something in college, you may think “What good is this to me in real life?”. COALESCE is one of those SQL functions which puzzled some of my colleagues. But here is a very prominent use case:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LSPdlD_sjNrvHy0hUPqm0Q.jpeg" /><figcaption>It’s time for a Cover-up Story!</figcaption></figure><p>Assume you have a list of Article entities in your database, and each article has a Category field (classic VARCHAR, no relations or other complications involved). You want to write an API endpoint to get all the Categories of a list of Articles, based on a list of Article ids. But now, your database is iffy, and some of your Articles aren’t categorised, so they have a NULL instead of an actual Category string. You want all these articles to belong to the “Miscellaneous” category.</p><p>Bad idea: processing, processing, processing… Run an SQL query to select the Category strings, check if “Miscellaneous” is in the result, add it otherwise (you don’t want the “Miscellaneous” category appearing twice, in case some Articles are correctly labeled as such)</p><p>Neat idea: Use COALESCE in your query to convert any NULL values to “Miscellaneous”. You’ll have something like:</p><pre>SELECT DISTINCT(COALESCE(articles.category, “Miscellaneous”)) as category’)) FROM articles;</pre><p>And you’ve done it with no extra overhead in processing, which might have been difficult for others to read and understand.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a625312b9de5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/smallprogtips/using-coalesce-for-neat-sql-queries-a625312b9de5">Using COALESCE for neat SQL queries</a> was originally published in <a href="https://medium.com/smallprogtips">smallprogtips</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>