PurelyFunctional.tv Newsletter 353: Tip: String literals are interned

Sign up for weekly Clojure tips, software design, and a Clojure coding challenge.

Issue 353 - November 25, 2019 ยท Archives ยท Subscribe

First Annual PurelyFunctional.tv Survey! ๐Ÿ“‹

The first annual PurelyFunctional.tv Survey is still open. Your answer to this quick survey will help me understand how to improve my videos and help you master Clojure faster and more deeply.

There are only four questions. If you've watched any of my video content, please take a few minutes to fill this out. I appreciate any answer you can give. And a big "thank you" to everyone who has already submitted an answer.

Fill out the survey

The survey will run for two more weeks.

Clojure Tip ๐Ÿ’ก

String literals are interned

String literals (strings that appear directly in code) are interned in Java. That means that there is a lookup table in memory where they are stored. If you use the same string literal twice (two strings that are equal), Java will look it up in the intern table and reuse the existing one.

Clojure does the same thing. Try this at the REPL:

> (identical? "a" "a")
true

Java and Clojure do this to increase performance in two ways. First, interned strings can be compared much more quickly since, in the case where they are equal, they will have identical pointers. If the compiler can tell that all of the strings you are comparing are interned, it can use only pointer equality.

Second, interned strings use less memory since the same string is reused. In general, Strings in the intern table can be garbage collected when they are no longer accessible from the program. All of this is possible because Strings are immutable.

Note that a lot of the strings you use are not interned by default. When you read lines as strings from a data file, those are not interned. When you concatenate two strings, those are not interned. Etc. If for some reason you want two equal strings but don't want them to be identical, you should construct a new one using the String constructor.

> (identical? "a" (String. "a"))
false

However, it's hard to think of why you would want that in production code.

Clojure Media ๐Ÿฟ

Clojure/conj 2019 videos

Folks, it was a great conference. I didn't catch all the talks, so I'll be watching them online. I often prefer that anyway. Conferences are for meeting each other, and I met some great people. See you online until next time.

Awesome book ๐Ÿ“–

The Unicorn Project by Gene Kim.

Gene Kim, the author, spoke at the Conj this past week. This book coming out Tuesday, November 26. I was lucky enough to be offered a preview copy. I read it and loved it. Nothing shows the developer's experience inside the enterprise bureaucracy better than this novel.

Currently recording ๐ŸŽฅ

Property-Based Testing with test.check is now split into three courses, Beginning, Intermediate, and Advanced.

The launch sale is still going. It will end in one week, at which point the price will go up. If you want them, now's your chance. They will never be this inexpensive again!

Book update ๐Ÿ“–

I have been getting such great stories from readers of Grokking Simplicity. People tell me they are making their code more organized and testable. After all of the work I put into the book, it feels good that it's having such a great impact.

You can buy the book and use the coupon code TSSIMPLICITY for 50% off.

Thanks for reading.

Clojure Challenge ๐Ÿค”

Last week's challenge

The challenge in Issue 352 was to write a program to find all possibilities of putting a +, a -, or nothing between the numbers 1-9, such that it equals 100.

You can check out the submissions here.

This week's challenge

improvement of daily work

What editor do you use? Whatever the answer, you can probably improve how you use it.

The challenge this week is to try to improve your editing experience in one small way. Find a new setting that you think might make your life better. Read about a new command and try to learn it. Or maybe change your caps lock key to control. You choose!

This week's challenge is not a coding challenge. It's a personal challenge to improve your daily work. Of course, some things you try won't improve anything. They might make it worse. But it's always worth trying.

BTW, improvement of daily work is the third ideal in The Unicorn Project.

As usual, please reply to this email and let me know what you tried. I'll collect them up and share them in the next issue. If you don't want me to share your submission, let me know.

Rock on!
Eric Normand