<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: kamaaaal</title>
    <description>The latest articles on DEV Community by kamaaaal (@kamaaaal).</description>
    <link>https://dev.to/kamaaaal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1047958%2Fdca48113-beb0-4d62-af19-e848ca08612e.jpg</url>
      <title>DEV Community: kamaaaal</title>
      <link>https://dev.to/kamaaaal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kamaaaal"/>
    <language>en</language>
    <item>
      <title>I turned an old phone into a Linux homelab</title>
      <dc:creator>kamaaaal</dc:creator>
      <pubDate>Sun, 21 Dec 2025 14:21:44 +0000</pubDate>
      <link>https://dev.to/kamaaaal/i-turned-an-old-phone-into-a-linux-homelab-54cf</link>
      <guid>https://dev.to/kamaaaal/i-turned-an-old-phone-into-a-linux-homelab-54cf</guid>
      <description>&lt;p&gt;I wanted to set up a homelab and first explored Raspberry Pi boards. That search led me to postmarketOS, a Linux distribution for smartphones. OnePlus 6T had good mainline Linux kernel support in postmarketOS.&lt;/p&gt;

&lt;p&gt;I found a OnePlus 6T where the display had been replaced with an LCD, lock and volume buttons weren’t very clicky. Even though it was a bit of a gamble, I bought the device out of curiosity to try postmarketOS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages of using OnePlus 6T as a homelab
&lt;/h3&gt;

&lt;p&gt;The main advantage was the price — I bought this device for less than the cost of a Raspberry Pi 5 (excluding the power supply and case).&lt;/p&gt;

&lt;p&gt;It also has a built-in battery, which effectively makes it a portable server and a relatively powerful processor for its size. The device even has a display, although it didn’t work in my case because it was an LCD replacement. postmarketOS currently supports the original AMOLED display, so I assume the display driver mismatch caused this issue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv2vdrtlksjnh8p1mxqvm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv2vdrtlksjnh8p1mxqvm.jpg" title="these lines comes and goes randomly on reboots" alt="Image of OP6t Running postmarket os" width="800" height="1773"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;these lines comes and goes randomly on reboots &lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Another major advantage was postmarketOS Edge. Unlike Android-based setups, postmarketOS provides a real Linux environment based on Alpine Edge, which is lightweight and minimal. The Edge version includes systemctl support and has relatively good compatibility with tools like k3s.&lt;/p&gt;

&lt;h3&gt;
  
  
  Workloads Running on the OnePlus 6T
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;llama.cpp&lt;/strong&gt; – Using llama.cpp with Qwen2.5-Coder-0.5B, I was able to run a fast and efficient local LLM. The Qwen2.5-Coder-0.5B model is only around 650 MB, which makes it suitable for constrained hardware. With the Continue VS Code plugin, I could chat with the model directly from VS Code, although code autocompletion did not work reliably.&lt;/p&gt;

&lt;h4&gt;
  
  
  k3s
&lt;/h4&gt;

&lt;p&gt;Setting up k3s was straightforward using the apk package manager. I ran Jellyfin inside the k3s cluster, Jellyfin is a media server similar to Plex.&lt;/p&gt;

&lt;p&gt;I created a persistent volume and mounted it to the Jellyfin pod, where I stored my media files. Jellyfin also supports multiple users and SyncPlay, which is similar to Spotify Jam.&lt;/p&gt;

&lt;h3&gt;
  
  
  Headless Setup: Accessing the Device Without a Display
&lt;/h3&gt;

&lt;p&gt;Since the device supports USB networking, I can connect it to any laptop and SSH into it. Through this connection, I configure and connect the device to a Wi-Fi network.&lt;/p&gt;

&lt;p&gt;I also connect the OnePlus 6T to my main Android phone Hotspot and SSH into the postmarketOS device from there. This allows me to access my homelab even while traveling, and even in situations where I don’t have a laptop with me.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is It Worth It?
&lt;/h3&gt;

&lt;p&gt;This device has a lot of limitations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Without a display, I always have to rely on another device to SSH into it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Because of the ARM architecture, I am not able to run some Docker images.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Networking is the biggest challenge — I have to create flannel configurations and dummy network interfaces to make k3s work reliably on the OnePlus 6T hotspot. While doing this, I learn a lot about Linux networking.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, many of these problems also exist with a Raspberry Pi. The main difference is that on a Raspberry Pi, I can install almost any Linux distribution, whereas on the phone I am mostly restricted to Alpine Linux through postmarketOS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Finally
&lt;/h3&gt;

&lt;p&gt;I did this mainly for fun and learning, and along the way I ended up with a setup that actually works. I can now use SyncPlay with friends, and the device runs reliably as a small, always on system.&lt;/p&gt;

&lt;p&gt;This may not be a traditional homelab, especially since installing Linux on a phone is a different process, but it served its purpose. If you’re curious or have ideas for other workloads to try on this setup let me know.&lt;/p&gt;

&lt;p&gt;I ll write follow-up posts with in-depth setup details for the workloads I run on this homelab.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>kubernetes</category>
      <category>homelab</category>
      <category>devops</category>
    </item>
    <item>
      <title>Full stack Postgres</title>
      <dc:creator>kamaaaal</dc:creator>
      <pubDate>Sun, 22 Jun 2025 12:43:32 +0000</pubDate>
      <link>https://dev.to/kamaaaal/full-stack-postgres-1840</link>
      <guid>https://dev.to/kamaaaal/full-stack-postgres-1840</guid>
      <description>&lt;p&gt;I tried an experimental project with this stack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;    Database – PostgreSQL&lt;/li&gt;
&lt;li&gt;    API – PostgREST&lt;/li&gt;
&lt;li&gt;    Templating language – pg_render&lt;/li&gt;
&lt;li&gt;    Front-end – of course SQL can't render buttons, so I used HTMX to avoid writing any JS and keep the stack constrained.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Project:
&lt;/h3&gt;

&lt;p&gt;A simple inventory app that lets you add items, increment or decrement their counters, and delete them when needed — basically a good old to-do-style app, but each item has a counter&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff0ybq5sh48jq409c09os.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff0ybq5sh48jq409c09os.png" alt="Image of the application" width="800" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Breaking It Down
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Docker
&lt;/h4&gt;

&lt;p&gt;I used Docker to keep things clean. Especially for pg_render, which isn’t available as a standard Postgres extension, Docker made setup much easier, so I leaned on this excellent example &lt;a href="https://github.com/mkaski/pg_render_example/blob/master/Dockerfile" rel="noopener noreferrer"&gt;pg_render example&lt;/a&gt; let’s just say my Dockerfile is... heavily inspired.😉&lt;/p&gt;

&lt;h4&gt;
  
  
  Postgres
&lt;/h4&gt;

&lt;p&gt;PostgREST is not a direct extension of PostgreSQL — it’s a separate application that connects to a Postgres database and exposes a RESTful API for all the available resources.&lt;/p&gt;

&lt;p&gt;It automatically turns your tables, views into API endpoints that support standard operations like GET, POST, PATCH, and DELETE.&lt;/p&gt;

&lt;p&gt;For custom logic, you can write RPC functions using PostgreSQL’s procedural languages. These functions can then be called through the API using the following pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /rpc/function_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgREST will handle the request and execute the corresponding function, returning the result as JSON.&lt;/p&gt;

&lt;p&gt;Here’s the table that stores items of the app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create table items (
    id serial primary key,
    name text not null,
    stock_count integer not null default 0,
    created_at timestamp default now()
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgREST automatically exposes this as a REST resource at /items.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    GET /items — list all items
    POST /items — create a new item (with JSON body like { "name": "pen" })
    PATCH /items?id=eq.3 — update item with id = 3
    DELETE /items?id=eq.3 — delete item with id = 3
    POST /rpc/increment_stock — custom RPC function to increment stock_count for an item
    POST /rpc/decrement_stock — same, but decrements instead
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  pg_render
&lt;/h4&gt;

&lt;p&gt;pg_render is a templating extension for PostgreSQL. It lets you render HTML templates with placeholders (using {{ }}) by filling them with values from a JSON object or a database row.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    render(template, row/json) – renders a single template

    render_agg(template, setof rows) – renders a list of rows into one big HTML string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Template used for item&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;insert into templates (id, template) values (
  'item',
  $$
  &amp;lt;div class="item-card" id="item-{{ id }}"&amp;gt;
    &amp;lt;div class="item-info"&amp;gt;
        &amp;lt;h4&amp;gt;{{ name }}&amp;lt;/h4&amp;gt;
        &amp;lt;p&amp;gt;Stock: &amp;lt;span class="stock-count" id="item-{{ id }}-count"&amp;gt;{{ stock_count }}&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div class="item-actions"&amp;gt;
        &amp;lt;button 
          hx-post="/rpc/increment_stock" 
          hx-vals='{"item_id": {{ id }} }' 
          hx-target="#item-{{ id }}-count" 
          hx-swap="innerHTML"&amp;gt;+&amp;lt;/button&amp;gt;

        &amp;lt;button 
          hx-post="/rpc/decrement_stock" 
          hx-vals='{"item_id": {{ id }} }' 
          hx-target="#item-{{ id }}-count" 
          hx-swap="innerHTML"&amp;gt;-&amp;lt;/button&amp;gt;

        &amp;lt;button 
          hx-delete="/items?id=eq.{{ id }}" 
          hx-target="#item-{{ id }}" 
          hx-swap="outerHTML"&amp;gt;🗑️&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
  $$
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be rendered with render function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select render(
  (select template from templates where id = 'item'),
  i
)
from items i
where id = 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  htmx
&lt;/h4&gt;

&lt;p&gt;HTMX is a JavaScript library used on the client side to build dynamic web apps without writing JavaScript.&lt;/p&gt;

&lt;p&gt;It provides JavaScript-like functionality by extending HTML with attributes like hx-post, hx-get, and hx-target to make API calls and update parts of the page.&lt;/p&gt;

&lt;p&gt;HTMX follows the principle of hypertext-driven applications, where the server returns rendered HTML fragments instead of JSON, and the browser swaps them into the page.&lt;/p&gt;

&lt;p&gt;HTMX POST to &lt;code&gt;/items&lt;/code&gt; -&amp;gt; This triggers a POST request with body of &lt;code&gt;{"name": "input_value"}&lt;/code&gt; when the form is submitted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form 
    id="create-item"
    hx-post="/items" 
    hx-trigger="submit" 
&amp;gt;
    &amp;lt;h3&amp;gt;Create New Item&amp;lt;/h3&amp;gt;
    &amp;lt;input type="text" name="name" placeholder="Enter Item Name" required /&amp;gt;
    &amp;lt;button type="submit"&amp;gt;➕ Add Item&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Increase the count of an item and swap the latest count&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="item-info"&amp;gt;
    &amp;lt;h4&amp;gt; {{ name }} &amp;lt;/h4&amp;gt;
    &amp;lt;p&amp;gt;Stock: &amp;lt;span class="stock-count" id="item-{{ id }}-count"&amp;gt;{{ stock_count }}&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;button 
        hx-post="/rpc/increment_stock" 
        hx-vals='{"item_id": {{ id }} }' 
        hx-target="#item-{{ id }}-count"
        hx-swap="innerHTML"&amp;gt; +
&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;hx-post specifies the RPC function to be called.&lt;br&gt;
hx-vals is the payload sent with the request.&lt;br&gt;
hx-target points to the element where the response will be inserted.&lt;/p&gt;

&lt;p&gt;The RPC call returns the updated count after incrementing, and that value replaces the content inside the target element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create or replace function increment_stock(item_id int)
returns INTEGER
language sql
as $$
  update items
  set stock_count = stock_count + 1
  where id = item_id
  returning stock_count;
$$;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Wrapping up
&lt;/h3&gt;

&lt;p&gt;GitHub link – &lt;a href="https://github.com/kamaaaal/fullstack_pg" rel="noopener noreferrer"&gt;https://github.com/kamaaaal/fullstack_pg&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nginx can be used to hide the /rpc/index route and serve the initial page as just /.&lt;/p&gt;

&lt;p&gt;This project has a few limitations (bugs) as of now — for example, adding a new product doesn’t immediately reflect on the page and requires a manual refresh. This could be fixed by using a custom RPC function that returns the newly added item as rendered HTML.&lt;/p&gt;

&lt;p&gt;I feel this is a pretty cool idea — would love to hear your thoughts on what kind of applications could be built with this stack.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The problem with Higher Order Array Functions (js)</title>
      <dc:creator>kamaaaal</dc:creator>
      <pubDate>Tue, 15 Aug 2023 05:26:36 +0000</pubDate>
      <link>https://dev.to/kamaaaal/the-problem-with-higher-order-array-function-js-41gf</link>
      <guid>https://dev.to/kamaaaal/the-problem-with-higher-order-array-function-js-41gf</guid>
      <description>&lt;h2&gt;
  
  
  So What are higher order functions ?
&lt;/h2&gt;

&lt;p&gt;Higher order functions are functions which accepts an argument as a function or a function which returns another function. In JS we use lot of HOF(Higher Order function) and functional programming patterns. some examples&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I'm using use the term &lt;code&gt;functions&lt;/code&gt; interchangeably with &lt;code&gt;methods&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Function accepting callback function&lt;/strong&gt;&lt;br&gt;
Array.map, Array.filter, Array.reduce&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function returning function&lt;/strong&gt;&lt;br&gt;
Function.apply, Function.call, Function.bind &lt;/p&gt;
&lt;h2&gt;
  
  
  Use cases
&lt;/h2&gt;

&lt;p&gt;Functional programming patterns provide easier expressive way to write code. Using Array methods helps us a lot by not writing a for loop statements by ourselves.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Array Methods&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// map&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;squred&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// filter (only even numbers)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;evenNums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;For loops&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;squared&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;evenNums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

&lt;span class="c1"&gt;// map&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;squared&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// filter (only even numbers)&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;evenNums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Array Methods provide a easier way to create a new object with .map method and with filter we can create a new array with desired elements &lt;/p&gt;

&lt;p&gt;Array methods returns themselves(Array object), Allowing us to chain them with multiple methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="c1"&gt;// square all numbers and then sum them&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code sample combining array methods and arrow function allows us to perform lot of operations in single line.&lt;/p&gt;

&lt;p&gt;Obviously, it is more expressive and readable than traditional loops.&lt;/p&gt;

&lt;h2&gt;
  
  
  So What are my Concerns
&lt;/h2&gt;

&lt;p&gt;My concerns raised whenever I'm chaining array methods. Chaining Array methods looks good to our eyes but not to the JS Garbage collector.&lt;/p&gt;

&lt;p&gt;Let's tackle a simple problem. We have an array of numbers representing people's incomes. Our job is to calculate a 5% tax on incomes that are above 500 and then create a new array containing those incomes along with their corresponding taxes only.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;incomes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;600&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;incomes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;income&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;income&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;income&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;income&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;income&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;output&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [ 630, 840 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above is the straightforward implementation. but we miss out the fact that to get the desired output we loop through the whole array twice and an intermediate array is also created. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgowzxuqr82y7evcu78u6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgowzxuqr82y7evcu78u6.png" alt="Image of output arary" width="800" height="171"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So algorithmically speaking as we loop through the array twice the algorithmic complexity is O(2*n) &lt;code&gt;I know the constant gets dropped in Big O, but let me dramatize&lt;/code&gt; and That filtered array(intermediate) array has to be garbage collected by the v8.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can be done
&lt;/h2&gt;

&lt;p&gt;Chaining Array methods with smaller arrays will not cause much of performance impact. but when doing those with larger arrays we can write traditional loops instead of array methods, so that we don't have to loop through multiple arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;incomes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;600&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;incomes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;incomes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;withTax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;incomes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;incomes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;withTax&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//[ 630, 840 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in the above we are processing only the incomes above 500 and just pushing them to result array. Of course, this looks little longer than the array methods implementation.&lt;/p&gt;

&lt;p&gt;we can use &lt;code&gt;for(of)&lt;/code&gt; loops or &lt;code&gt;.foreach()&lt;/code&gt;  to make it little nicer.&lt;/p&gt;

&lt;h2&gt;
  
  
  So is this the perfect implementation ?
&lt;/h2&gt;

&lt;p&gt;This approach appears to be a good one. However, there is a potential issue with this code in the following line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;        &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;withTax&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;.push&lt;/code&gt; method is used to add elements to an array, but internally, the JavaScript engine needs to make memory allocation calls to the operating system to allocate memory for the array. Making multiple operating system calls can be an expensive task, and this is abstracted by the &lt;code&gt;.push&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;So, this should hold true for the &lt;code&gt;.map&lt;/code&gt; implementation. But it's not the same. When we call the &lt;code&gt;.map&lt;/code&gt; method on an array and the output array is going to end up with the same size, the V8 engine actually makes just one memory allocation call for the entire array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;so which is the faster way, It's always better to use HOF's if we are not chaining multiple methods. but I can't answer whether chaining multiple array methods or dynamically pushing elements into an array is faster. that might require multiple benchmarks and analysis.&lt;/p&gt;

&lt;p&gt;I apologize if I made any grammatical mistakes.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>functionalreactiveprogramming</category>
      <category>node</category>
    </item>
    <item>
      <title>Weird Type Coercions</title>
      <dc:creator>kamaaaal</dc:creator>
      <pubDate>Sun, 19 Mar 2023 09:26:35 +0000</pubDate>
      <link>https://dev.to/kamaaaal/weird-type-coercions-4l10</link>
      <guid>https://dev.to/kamaaaal/weird-type-coercions-4l10</guid>
      <description>&lt;p&gt;Hey Folks, I've been using multiple programming languages recently and I noticed the different behavior of type coercion in different languages.  &lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript
&lt;/h2&gt;

&lt;p&gt;We all know how weird is JavaScript so I'm not going to explain JavaScript type coercion lot. &lt;br&gt;
but here is a simple snippet&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="c1"&gt;// results "55"&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;  &lt;span class="c1"&gt;// results "5"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here the number and Array[] are implicitly converted to a string,&lt;br&gt;
so the behaviour is it does either addition or concatenation at certain operands.&lt;/p&gt;
&lt;h2&gt;
  
  
  Python and Typescript
&lt;/h2&gt;

&lt;p&gt;Python and Typescript seems to be strict. they don't allow type coercion we need to explicitly convert them to a certain type to do addition or string concatenation.&lt;/p&gt;
&lt;h3&gt;
  
  
  Python
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"5"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# results 10, does addition. 
&lt;/span&gt;&lt;span class="s"&gt;"5"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# results "55", does concatenation
&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"55"&lt;/span&gt;     &lt;span class="c1"&gt;# throws TypeError
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Typescript
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// results 10, does addition. &lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// results "55", does concatenation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The above examples avoid unexpected behavior. but it may feel odd for others at first who had lot of experience with other languages.&lt;/p&gt;
&lt;h2&gt;
  
  
  CSharp / .NET
&lt;/h2&gt;

&lt;p&gt;I've been learning asp.net Framework (2014) for my college semester. That's a sad story university never minds updating the curriculum.&lt;/p&gt;

&lt;p&gt;back to CSharp, when I was writing an if else &lt;br&gt;
statement checking for an empty string gave me an error saying that "Cannot implicitly convert type 'string' to 'bool' "&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;emptyStr&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;emptyStr&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="c1"&gt;//error : Cannot implicitly convert type 'string' to 'bool'  &lt;/span&gt;
   &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;emptyStr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and i'm sorry for not including boilerplate "public static void Main" 🥺.&lt;/p&gt;

&lt;p&gt;I didn't expect this because for a long time JavaScript, typescript and python were evaluating zeros(0) and empty string("") to be falsy. Then I realized that I didn't touch other typed languages for a long time 😅. &lt;/p&gt;

&lt;p&gt;after this tried the same example in java got the same error. But the c++ evaluated empty string(char[]) as true because every pointer is evaluated to truthy value I guess. however, std::string throwed an conversion error.&lt;/p&gt;

&lt;p&gt;After that I had a strong belief that Csharp is one of a safest strictly-typed language. That didn't last long.&lt;/p&gt;

&lt;p&gt;Even though CSharp being strict on implicit conversion from converting string to bool.&lt;br&gt;
It didn't care converting an int to string implicitly when using + operator&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"no."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;+&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// results no.10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Php
&lt;/h2&gt;

&lt;p&gt;I'm currently doing a project in php for my friend. That's the reason made me to write this post.&lt;/p&gt;

&lt;p&gt;Php has a dedicated operator for string concatenation which is ".". having a concatenation operator, it isn't a surprise that it implicitly converts an integer to string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello "&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="s2"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// results "Hello World"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello "&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// results "Hello 10"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, because of having a concatenation operator it doesn't allow "+" to do concatenation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// results 20&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="s2"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Unsupported operand types: string + string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This seems to be somehow same behavior like other languages,but the next example isn't&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$str1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"10"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$str2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"20"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$str1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nv"&gt;$str2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// results 20 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though both the operand being string here, both of them are converted to integer because of the operator "+".&lt;/p&gt;

&lt;p&gt;Without knowing this behavior, I tried to concatenate two strings containing a number 🙂.&lt;/p&gt;

&lt;p&gt;I think I can't say it's a bad behavior , because it has a dedicated operator for concatenation, but still a newbie developer like me who have been practicing other languages for some time it seemed weird.&lt;/p&gt;

&lt;p&gt;Thank you for reading this. I apologize for any grammatical errors in my writing.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>php</category>
      <category>dilemma</category>
      <category>csharp</category>
    </item>
  </channel>
</rss>
