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. Can a PostgreSQL index store nulls first or last?
  11. Do MySQL, SQLite and Oracle sort nulls the same way as PostgreSQL?
  12. Converting Large Applications to TypeScript
  13. Importing SCSS variables in Next.js
  14. What does Next.js need before a component can read a Sass variable?
  15. What's in a name? Falsehoods That Programmers Believe About Names
  16. 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.

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."

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.

Stay In The Loop

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