Skip to main content
Published in Engineering Insights

Gnarly Learnings from November

Gnarly Learnings from November
Table of contents
  1. The Javascript RORO pattern
  2. The Invisible Javascript Backdoor
  3. How do you spot an invisible character in a code review?
  4. The Value in Value Objects
  5. State Management Showdown: Redux vs. Context API
  6. Importing SVGs as React Components
  7. Does the Create React App SVG import still work?
  8. Ordering NULLS in PostgreSQL
  9. Where do nulls sort by default in a PostgreSQL ORDER BY?
  10. How do you use PostgreSQL ORDER BY NULLS FIRST and NULLS LAST in a query?
    1. How do you sort null values in descending order?
  11. What is the difference between ORDER BY and GROUP BY in PostgreSQL?
  12. How does ORDER BY sort rows in PostgreSQL?
  13. Can a PostgreSQL index store nulls first or last?
  14. Do MySQL, SQLite and Oracle sort nulls the same way as PostgreSQL?
  15. Converting Large Applications to TypeScript
  16. Importing SCSS variables in Next.js
  17. What does Next.js need before a component can read a Sass variable?
  18. What's in a name? Falsehoods That Programmers Believe About Names
  19. Contributors

We love reading, watching, and listening to constantly update our skills and learn new perspectives. Here are some of the exciting pieces we learned from this month.

This roundup is for software developers and the engineering leads who review their code. Each piece links what we read, and the longer sections answer one question with what the docs and the original sources say today. The two longest answers cover where PostgreSQL puts null values in a sorted list, and how an invisible character can hide a backdoor in code that looks clean in review.

The Javascript RORO pattern#

RORO ("Receive Object, Return Object") is a javascript pattern that seeks to take advantage of the benefits of ES6, making functions that have easily constructed props and easily deconstructed return values.

The Invisible Javascript Backdoor#

While hiding things in plain sight isn't really a new concept in programming, especially for attacks, I found this one interesting because of The Gnar's use of trailing commas in our style guide. Normally, the commas are good for reducing noise in git diffs, but in this case, the author demonstrates how they can be leveraged for malicious purposes.

How do you spot an invisible character in a code review?#

Use an editor that highlights invisible and look-alike characters, and treat any non-ASCII character in code as a question. VS Code's November 2021 release notes (version 1.63) say uncommon invisible characters are now highlighted by default. Certitude's backdoor article goes further for teams that write code in English. It says "It might therefore be a good idea to disallow any non-ASCII characters."

The post found this trick interesting "because of The Gnar's use of trailing commas in our style guide." Certitude's backdoor hides a character called "HANGUL FILLER" (0x3164) after what looks like a trailing comma. "As this character is considered to be a letter, it has the ID_Start property and can therefore appear in a JavaScript variable". Certitude shows the line with the character written as its escape sequence: const { timeout, \u3164 } = req.query;. The article adds that this "cannot be detected through syntax highlighting as invisible characters are not shown at all".

Three checks come straight from the article and the VS Code release notes:

  1. Keep editor highlighting on. The 1.63 notes say "All uncommon invisible characters in source code are now highlighted by default". Characters "that can be confused with ASCII characters are also highlighted". The notes also warn that "this feature does not detect all possible Unicode spoofing attacks, as this depends on your font and locale settings."
  2. Review untrusted code in Restricted Mode. The same notes give this advice: "VS Code's Workspace Trust Restricted Mode should be used to review source code, as all non-ASCII characters are highlighted in untrusted workspaces."
  3. Take extra care with outside code. Certitude writes: "Unicode should be kept in mind when doing reviews of code from unknown or untrusted contributors."

The Value in Value Objects#

We were discussing value objects vs. reference objects for one of our projects. This article from Martin Fowler is a nice summary of how to make the decision about value vs. reference, as well as some gotchas to look out for in particular languages.

State Management Showdown: Redux vs. Context API#

When the ContextAPI was released, it showed a world that embraced reducers, and in so doing, it sidestepped redux. When is Redux still useful, and when might you want to reach for it?

Importing SVGs as React Components#

We've often seen or written React components to render SVGs, or used img tags with an SVG src attribute. This section of the Create React App documentation describes how to import React components from SVG file paths directly!

Does the Create React App SVG import still work?#

Yes, the import still works inside an existing Create React App project. The docs still show import { ReactComponent as Logo } from './logo.svg'; and say "this feature is available with [email protected] and higher, and [email protected] and higher." React deprecated Create React App for new apps on February 14, 2025. So the import belongs to existing projects, and React sends new ones elsewhere.

The post's tip starts from a habit: "We've often seen or written React components to render SVGs". The docs explain what makes the shortcut work. "The ReactComponent import name is significant and tells Create React App that you want a React component that renders an SVG, rather than its filename." They also warn: "Don't forget the curly braces in the import!" One tip covers accessibility: "The imported SVG React Component accepts a title prop along with other props that a svg element accepts."

React's post says Create React App "will continue working in maintenance mode." It adds: "we've published a new version of Create React App to work with React 19." For new work it says "We recommend creating new React apps with a framework." It lists guides for existing apps in two groups:

  • Frameworks: Next.js' Create React App migration guide, React Router's framework adoption guide, and the Expo webpack to Expo Router migration guide.
  • Build tools: the Vite, Parcel and Rsbuild Create React App migration guides.

Ordering NULLS in PostgreSQL#

This section of the Postgres documentation covers sorting queries, and a particularly valuable excerpt helped sort where the records with null sort attributes are returned in the query.

The NULLS FIRST and NULLS LAST options can be used to determine whether nulls appear before or after non-null values in the sort ordering. By default, null values sort as if larger than any non-null value; that is, NULLS FIRST is the default for DESC order, and NULLS LAST otherwise.

Where do nulls sort by default in a PostgreSQL ORDER BY?#

PostgreSQL sorts a null as if it were larger than any other value. So ORDER BY x and ORDER BY x ASC put nulls last, and ORDER BY x DESC puts them first. Add NULLS FIRST or NULLS LAST after the column to pick the end you want. The PostgreSQL 18 manual's sorting page uses the same words as the 8.3 excerpt quoted above.

The post calls that paragraph "a particularly valuable excerpt" because it settled where records "with null sort attributes are returned in the query." Here is each case side by side. The long forms in the last column are the ones the manual's index chapter spells out.

You writeWhere nulls goThe long form
ORDER BY xLastORDER BY x ASC NULLS LAST
ORDER BY x DESCFirstORDER BY x DESC NULLS FIRST
ORDER BY x DESC NULLS LASTLastAs written
ORDER BY x ASC NULLS FIRSTFirstAs written

The SELECT reference gives the same rule. It sums it up this way: "the default is to act as though nulls are larger than non-nulls".

Watch for one trap with more than one column. The sorting page says "the ordering options are considered independently for each sort column." Its example: ORDER BY x, y DESC means ORDER BY x ASC, y DESC, "which is not the same as ORDER BY x DESC, y DESC." So a DESC on y leaves x in ascending order, with its nulls last.

The syntax itself is old. The PostgreSQL 8.3 release notes, dated February 4, 2008, list "Add control over whether NULLs sort first or last". They add: "The syntax is ORDER BY ... NULLS FIRST/LAST."

How do you use PostgreSQL ORDER BY NULLS FIRST and NULLS LAST in a query?#

The NULLS FIRST and NULLS LAST options go right after the column and its direction in the ORDER BY clause, as in ORDER BY estimate DESC NULLS LAST. Here is a small example. A tasks table has an estimate column in hours, and two tasks have no estimate yet, so their estimate is null:

CREATE TABLE tasks (title text, estimate integer);

INSERT INTO tasks VALUES ('Ship invoice export', 5), ('Fix login bug', 2), ('Write onboarding docs', NULL), ('Update billing page', NULL);

Each select statement below sorts rows by estimate, then by title, so tasks with the same estimate come back in a fixed order. The result sets come from running these statements on PostgreSQL 16.4 through PGlite, a build of PostgreSQL that runs inside Node.js.

The query sorts byRows in the order the query returns them
ORDER BY estimate, titleFix login bug (2), Ship invoice export (5), Update billing page (null), Write onboarding docs (null)
ORDER BY estimate DESC, titleUpdate billing page (null), Write onboarding docs (null), Ship invoice export (5), Fix login bug (2)
ORDER BY estimate DESC NULLS LAST, titleShip invoice export (5), Fix login bug (2), Update billing page (null), Write onboarding docs (null)

How do you sort null values in descending order?

A plain DESC puts nulls first, so add NULLS LAST after it. The second query shows why. The DESC keyword alone puts both unestimated tasks at the top of the descending sort order, because PostgreSQL treats a null as larger than any number. An ascending list keeps unestimated work at the bottom with no extra words. A "biggest estimate first" list that should do the same needs DESC NULLS LAST written out in its ORDER BY clause, as the third query does.

The order of the non-null values comes from the column's data type. A footnote on the sorting page says "PostgreSQL uses the default B-tree operator class for the expression's data type to determine the sort ordering for ASC and DESC." The same footnote notes that "a user-defined data type's designer could choose to do something different." Text adds one more input. The SELECT reference says "Character-string data is sorted according to the collation that applies to the column being sorted."

What is the difference between ORDER BY and GROUP BY in PostgreSQL?#

ORDER BY changes the order of the rows, and GROUP BY changes how many rows come back. The SELECT reference says the "ORDER BY clause causes the result rows to be sorted according to the specified expression(s)." It also says "GROUP BY will condense into a single row all selected rows that share the same values for the grouped expressions." The chapter on table expressions puts it this way: "The GROUP BY clause is used to group together those rows in a table that have the same values in all the columns listed."

The two clauses often work together. The following query counts the tasks in the table above for each estimate: SELECT estimate, count(*) FROM tasks GROUP BY estimate ORDER BY estimate;

Its result table has three rows: an estimate of 2 with a count of 1, an estimate of 5 with a count of 1, and one null row with a count of 2. The GROUP BY clause put both unestimated tasks in one group. The ORDER BY clause then placed that group last, as a plain ascending sort does. That count is the query behind a filter list such as "Estimate: 2, 5, not estimated". If the "not estimated" group belongs at the top of that list, write ORDER BY estimate NULLS FIRST. If the list should put the biggest estimates first and keep that group at the bottom, write ORDER BY estimate DESC NULLS LAST, the same fix as the task list above.

How does ORDER BY sort rows in PostgreSQL?#

ORDER BY sorts the rows of a query's result by one or more sort expressions, in ascending order unless you write DESC. Without ORDER BY, PostgreSQL makes no promise about row order. The sort can use any expression valid in the query's select list, or the label or number of an output column. The PostgreSQL 18 sorting page says: "After a query has produced an output table (after the select list has been processed) it can optionally be sorted. If sorting is not chosen, the rows will be returned in an unspecified order." A Gnar engineering post on finding the last record says the same in its TL;DR: "Unless you explicitly tell the database the order in which to return records, it'll just give you the records you requested in whatever order it wants." The post then shows that ActiveRecord's .last does not always add that order: "if the association is already loaded into memory, we'll return the last record of the association in memory" instead of running the ordered query.

The same page says what can follow ORDER BY. "The sort expression(s) can be any expression that would be valid in the query's select list." "A sort_expression can also be the column label or number of an output column", as in SELECT a + b AS sum, c FROM table1 ORDER BY sum;.

To sort nulls as if they held a stand-in value, replace them inside the sort expression. The manual's conditional expressions page says "The COALESCE function returns the first of its arguments that is not null." It adds: "It is often used to substitute a default value for null values when data is retrieved for display". In a sort, ORDER BY COALESCE(x, 0) sorts a null x as if it were 0, next to any rows whose x really is 0.

A paged list in an app needs an ORDER BY that puts the rows in a unique order. The PostgreSQL 18 manual's LIMIT page says: "When using LIMIT, it is important to use an ORDER BY clause that constrains the result rows into a unique order."

Can a PostgreSQL index store nulls first or last?#

Yes, a B-tree index accepts NULLS FIRST or NULLS LAST when you create it, so it can store its entries in that null order. The manual's chapter on indexes and ORDER BY says an index "may be able to deliver them in a specific sorted order." That "allows a query's ORDER BY specification to be honored without a separate sorting step."

The post's tip points to the documentation section that "covers sorting queries." The index chapter covers the same options from the index side. It says "only B-tree can produce sorted output" of the index types PostgreSQL supports today. The planner still picks between the index and "doing an explicit sort." The chapter then pairs each index order with the queries it can serve:

  • A default index, scanned forward. "By default, B-tree indexes store their entries in ascending order with nulls last". A forward scan gives ORDER BY x ASC NULLS LAST.
  • A default index, scanned backward. A backward scan gives ORDER BY x DESC NULLS FIRST.
  • An index built with nulls first. "An index stored in ascending order with nulls first can satisfy either ORDER BY x ASC NULLS FIRST or ORDER BY x DESC NULLS LAST depending on which direction it is scanned in."

The chapter's two examples are CREATE INDEX test2_info_nulls_low ON test2 (info NULLS FIRST); and CREATE INDEX test3_desc_index ON test3 (id DESC NULLS LAST);.

The chapter ends on cost. "Whether it's worth maintaining such an index depends on how often you use queries that require a special sort ordering." The Gnar Company's own Rails and Postgres performance post heads its index section "Indexes in Moderation". It says that with indexes in Postgres, "oftentimes less is more."

Do MySQL, SQLite and Oracle sort nulls the same way as PostgreSQL?#

Oracle sorts nulls the same way as PostgreSQL, but MySQL and SQLite do not. Oracle puts nulls last in an ascending sort and first in a descending one. MySQL and SQLite do the opposite, with nulls first going up and last going down. A query moved between these databases can return its nulls at the other end of the list.

The post's tip quotes the PostgreSQL rule that "null values sort as if larger than any non-null value". The table sets that rule next to each manual's own sentence on the default.

DatabaseASCDESCThe manual's words
PostgreSQL 18Nulls lastNulls first“NULLS FIRST is the default for DESC order, and NULLS LAST otherwise.”
Oracle DatabaseNulls lastNulls first“NULLS LAST is the default for ascending order, and NULLS FIRST is the default for descending order.”
MySQL 8.4Nulls firstNulls last“NULL values are presented first if you do ORDER BY ... ASC and last if you do ORDER BY ... DESC.”
SQLiteNulls firstNulls last“NULLs naturally appear at the beginning of an ASC order-by and at the end of a DESC order-by.”

The SQLite SELECT page explains its rule in one line. "SQLite considers NULL values to be smaller than any other values for sorting purposes." Its page adds that "This can be changed using the "ASC NULLS LAST" or "DESC NULLS FIRST" syntax." That syntax is newer in SQLite than in PostgreSQL. The SQLite 3.30.0 release log, dated October 4, 2019, says "Add support for the NULLS FIRST and NULLS LAST syntax in ORDER BY clauses."

Converting Large Applications to TypeScript#

TypeScript continues to be one of our favorite tools at The Gnar. One great feature is the ability to modulate the strictness of the TypeScript configuration in your application. In general, a stricter configuration will result in a higher incidence of program correctness but sometimes, that may not be the best option right out of the gate. One example of when a less strict configuration might be preferable is when migrating from JavaScript to TypeScript, as it allows TypeScript to be adopted incrementally. This is valuable when you have over 17,000 JavaScript files in your application, which is exactly what Etsy had to contend with when they were making the switch. Their approach is detailed in the article above.

Importing SCSS variables in Next.js#

The tooling for frontend developers is constantly evolving, and this small but "next level" feature is a great example. NextJS allows you to access SASS variables in your components easily by simply exporting them!

What does Next.js need before a component can read a Sass variable?#

Next.js needs a Sass package installed and the variable exported from a CSS Module file. Put the variable in a file such as variables.module.scss and list it in an :export block. The component imports that module and reads the value as a property. The Next.js Sass guide shows this exact pattern.

The post's tip says "NextJS allows you to access SASS variables in your components easily by simply exporting them!" The guide adds the setup that comes before the export works:

  1. Install Sass. The guide says "Next.js has built-in support for integrating with Sass after the package is installed". It says "First, install sass", and adds that "By default, Next.js uses the sass package."
  2. Use a CSS Module file. "Next.js supports Sass variables exported from CSS Module files." The guide names "the .module.scss or .module.sass extension" for those files.
  3. Import the module and read the property. The guide's app/page.js imports it with import variables from './variables.module.scss' and then reads variables.primaryColor.

This is the guide's variables file, app/variables.module.scss, as it prints it:

SCSS
$primary-color: #64ff00;

:export {
  primaryColor: $primary-color;
}

The same Sass Variables section appears in the Pages Router version of the guide, with the import in pages/_app.js.

What's in a name? Falsehoods That Programmers Believe About Names#

Accessibility comes in many forms, and this article does a good job of taking one common aspect of modern websites (recording a user's name) and explores the many assumptions developers may have about it. It is a useful read for those looking to think critically about the kind of things we do (or do not) require and record in our applications.

Contributors#

Learn more about how The Gnar builds software.

Frequently asked questions

Where do null values sort by default in Postgres?

Null values sort as larger than any non-null value. NULLS FIRST is the default for DESC order, and NULLS LAST is the default otherwise.

Can SVG files be imported as React components?

Yes. The Create React App documentation describes importing React components directly from SVG file paths.

How can NextJS components access SASS variables?

The post says NextJS lets components access SASS variables by exporting them.

Why review assumptions about user names?

The linked resource examines assumptions developers make when recording a user's name. It encourages considering what applications require and record.

Does PostgreSQL 18 still put nulls first in a DESC sort?

Yes. The PostgreSQL 18 manual says NULLS FIRST is the default for DESC order and NULLS LAST otherwise, in the same words as the 8.3 manual. It sorts nulls as if larger than any other value. Write DESC NULLS LAST to move them to the bottom.

Can a PostgreSQL index keep nulls first?

Yes. A B-tree index can be created with NULLS FIRST. The manual says an index stored in ascending order with nulls first can serve ORDER BY x ASC NULLS FIRST or ORDER BY x DESC NULLS LAST, depending on the scan direction.

Does MySQL sort nulls the same way as PostgreSQL?

No. The MySQL 8.4 manual says NULL values come first in an ascending sort and last in a descending one. PostgreSQL does the opposite, because it sorts nulls as if larger than any other value.

Is Create React App deprecated?

Yes, for new apps. React announced it on February 14, 2025 and pointed existing apps to a framework or to a build tool such as Vite, Parcel or Rsbuild. Create React App keeps working in maintenance mode, with a new version for React 19.

Does PostgreSQL return rows in a set order without ORDER BY?

No. The PostgreSQL 18 manual says that if sorting is not chosen, "the rows will be returned in an unspecified order." That order depends on the scan and join plan types and the order on disk, so add ORDER BY whenever the order matters.

What is the difference between ORDER BY and GROUP BY in PostgreSQL?

ORDER BY sorts the result rows. GROUP BY condenses rows that share the same values into one row per group. A query can use both. GROUP BY forms the groups, and ORDER BY then sorts the grouped rows.

How do you keep nulls at the bottom of a descending sort in PostgreSQL?

Write NULLS LAST after DESC, as in ORDER BY estimate DESC NULLS LAST. Without it, PostgreSQL puts nulls first in a descending sort, because it treats a null as larger than any other value.

Stay In The Loop

Never miss an update. Get the latest blogs, webinars, and expert insights straight to your inbox from The Gnar.