How to Coda with Sign()

Shorten and simplify your code

Christiaan Huizer
6 min readOct 26, 2023

--

In discussion with the great Coda connaisseur Joostmineur we both developed our own solution to split duration into years, months, days and minutes. My approach was to relate years, with months and days as I described in the blog below.

Joost followed a different path. He isolated each element to only bring it together at the end. Both our solutions work. Besides there is a third — non Coda — solution created by Pablo Felip who showed me his amazing sheets based contribution. We ended up with three different solutions for one challenging puzzle. It was a great exercise for all of us.

In this blog I focus on a part of the contribution of Joostmineur

The context

When you want to have the duration expressed in years you need to look before or after the end date. I repeat the example I shared previously.

When you want to know the duration between April 25, 1985 and Oct 8, 2008, we look first for April 25 in 2008. We count the years (23). When we look for the delta between 8 Oct 2008 and April 3 2011.Oct 8, 2011 is too late, because that is after the date. In this case we need the year 2010 .

April 25 is before Oct 8, but April 3 is after October 8. These examples showcase two situations. A third would be that the startdate and the end date are equal. This happens when you calculate a birthday. For most people this is always on the same date. The lucky onces never growing old born on Feb 29, enjoy a little variation.

The take away is that you look before, on or after the end date. You can rewrite the outcome as negative, neutral or positive.

The Sign() simplicity

The formal Coda explanation you find here. I had seen the function before but it was Joostmineur who showed me its hidden super power. What it does is rather simple, it signs if a number is positive (1), negative (-1) or neutral (0). That is all. The examples provided are:

the Coda explanation

Sign() logic applied

When we ask for the years between the end date and the start date it goes as follows and the outcome is an integer like 0,1,2,3,4 …, etc

simple subtraction

These are mainly positive numbers since the end date comes after the start date or a zero when the years are equal.

However we need something else. We need the shortest time span possible between the end date and the start date that is always less than one year. The year value of the startdate is in the year of the start date or in the year before, as in my example (Oct — April and April — Oct). We get there by using Substitute() and it looks as follows.

getting the delta days and time

You see that the outcome is expressed in days and time. The two examples I wrote about are found in row one and two, first a negative number and second a positive number. On line 8 you notice a zero value.

When the outcome is positive or zero, we have the correct year, only when the outcome is negative we need to subtract one year.

Previously I would have written this as an If statement, check the outcome and if negative, year minus one, in all other cases keep the year.

The function Sign() helps us to shorten the code snippet because it already has the values we need (-1,0,1). Actually we have one too many. We don’t need the plus 1 because when the outcome is positive, the year is already okay.

Apply Sign() to sign the number value

To avoid issues with the one, we add a second function named Min() and it takes the lowest value in a list of numbers. We add a zero to this function and that means that when we add a positive number (the one), due to the present zero, nothing happens. It neutralises the positive outcome.

add Min() to Sign()

The result is a value we can work with. We return to our initial subtraction and we add the signed value and off we go.

the final solution to get the years

Min() and Sign() help us to avoid If statements, the formula gets shorter, but maybe not easier to read. However when you see through and you enjoy the beauty, elegance and power of these functions, you will apply it rather often in my experience.

That brings me back to a birthday calculation a wrote about earlier:

It is possible to shorten this function as well with Sign() and this time Max() . We took the start Date as the birthday. The code is compact.

apply Sign and Max to get the next birthday really easy

I also applied Sign() in a function to get the age, see below. On the left the standard one and right the signed version. I believe that in this scenario the signed version is harder to understand, it becomes unnecessary difficult for most users.

My name is Christiaan and blog about Coda. Since the summer of 2023 mainly about how to Coda with AI to support organisations dealing with texts and templates. Why I focus on Coda AI you can read here: ⤵️

I hope you enjoyed this article. If you have questions feel free to reach out. Though this article is for free, my work (including advice) won’t be, but there is always room for a chat to see what can be done. You find my (for free) contributions to the Coda Community and on Twitter.

Coda comes with a set of building blocks ー like pages for infinite depth, tables that talk to each other, and buttons that take action inside or outside your doc ーso anyone can make a doc as powerful as an app (source).

Not to forget: the Coda Community provides great insights for free once you add a sample doc.

--

--

Christiaan Huizer

I write about how to Coda . You find blogs for beginners and experienced makers. I publish about 1 / week. Welcome!