Israel
Marching in the streets.
Never thought I’d see the day –
Brown shirts.
Stop.
Listen.
Look.
Fight back!
Israel, Israel, we belong to you.
Israel, Israel –
To God Most High.
Stand and Fight!
The other day when I was scrolling LinkedIn, I found a post arguing that exceptions are not the best way to handle errors. Most languages don’t force you to handle or re-throw exceptions at each level of the call stack, which means that it can be difficult to track down the source of a given exception when it bubbles up to the top level. In addition, exceptions should only ever be used to address situations that are, well, exceptional, and not situations that can easily be anticipated – say, a record not being found in a database.
Error Statuses in JSON Data
I’ll share the solution that was suggested in the post momentarily, but first, I’d like to share an old solution from the days before I knew much about the REST maturity model. In those days, an “Ajax” request was just a POST with a uniform JSON response. Since it hadn’t occurred to me to use HTTP response codes, I needed to indicate success and error statuses in my JSON, so my responses usually ended up looking something like this for a “failure”:
{ error: "User not found" }
And something like this for a “success”:
{ result: { name: "Bob", password: "oops, we shouldn't be sending this to the front end!" } }
In other words, successful calls populated the “result” field, and unsuccessful calls populated the “error” field. It was easy enough to check for the existence of an “error” or “result” on the front end, and proceed accordingly.
Returning a Result Object
The aforementioned LinkedIn post inspired me with an idea: if an API endpoint can return this type of response, why can’t a function? Couldn’t we have our functions return an object, like this:
type Error = object
message: string
# The Nim language uses the [T] syntax for generics
type Result[T] = object
value: ref T
error: ref Error
# A Nim 'proc' is just a function, and the equals sign
# at the end of this line works like '=>' in other languages.
proc isSuccess[T] (this: Result[T]): bool =
this.error == nil and this.value != nil
proc isFailure[T] (this: Result[T]): bool =
not this.isSuccess
proc failure[T] (error: Error): Result[T] =
result.error = new Error
result.error[] = error
proc success[T] (value: T): Result[T] =
result.value = new T
result.value[] = value
… instead of throwing an exception? Then we could check the results, like this:
proc getUsernameFails (_: int): Result[string] =
result = failure[string](Error(message: "User not found."))
proc getUsernameSucceeds (_: int): Result[string] =
result = success[string](Error(message: "User not found."))
var usernameResult = getUserNameFails(999)
# [] is Nim's dereference operatorif usernameResult.isSuccess: echo usernameResult.value[]
else: echo usernameResult.error[]
usernameResult = getUserNameSucceeds(999)
if usernameResult.isSuccess: echo usernameResult.value[]
else: echo usernameResult.error[]
Of course, maybe you’ve already noticed the potential issue with this approach. Accessing the error
field of a Success result, or the value
field of a Failure result, without first checking for isSuccess or isFailure, will result in a null pointer exception (hardly ideal, considering the point of this exercise is to do away with exceptions!), or worse, a buffer overflow.
Discriminated Unions
Instead of a Regular old, Unassuming Result Objects (RUh-ROh’s…), the suggestion on LinkedIn involved a Union of two types. One type contains a value
field, and the other contains an error
field. The Union can exist in either a Success or a Failure state, and, depending on which state it’s in, it will contain one or the other field.
The Nim language accomplishes something similar by allowing you to use a case statement within an object declaration, to determine what fields the object should have. Think of it as syntatic sugar for the abstract factory pattern:
type ResultKind = enum
Success,
Failure
type Result[T] = object
# Nim infers from this case statement that a Result object has a
# ResultKind enum field, called 'kind' case kind: ResultKind:
of Success:
# a Result object will only have this field when its kind field is set to ResultKind.Success
value: T
of Failure:
# a Result object will only have this field when its kind field is set to ResultKind.Failure
error: Error
Then, a function can indicate success or failure by returning one or the other variation of the Result object:
proc getUserById(id: int): Result[int] =
if database.getUser(id) == nil:
result.kind = Failure
result.error = Error(message: "User not found")
else:
result.kind = Success
result.value = user.id
However, there’s an issue with this approach, as well. It’s true that you’re no longer dealing with null pointer access when you try to inspect the value
field of a Failure object, or the error
field of a Success object. But the determination as to which fields of a given Result object are currently available can only be made at run-time; so, when the user tries to access a non-existent field, it throws an exception.
And so we’re back where we started.
Multiple Return Values
What we really want is a way to force the user to check the success status of a function call before trying to use the result. Go and React both come really close, by returning multiple values (Go), or an array (React). In Go, you do something like this:
error, result := myLib.DoAThing()
if !error {
doSomethingWith(result)
}
While in React, you do something like this:
let [error, result] = doAThing()
if (error) console.log(error)
else doSomethingWith(result)
In Nim or Python, you can accomplish the same result using a tuple:
let (error, result) = doAThing()
if not error: doSomethingWith(result)
else: echo error
(Okay, so my example only works in Nim. But why would you use Python anymore, when you can get the same expressiveness from a language that also allows you to write compilers and OS kernels, and that also cross-compiles, at your option, to Javascript?)
A common complaint I see with this approach, especially in Go, where it is used most often, is that now you have to do an if !error check for nearly every line of code you write, which is tedious and ugly. To be fair, the LinkedIn solution – using discriminated unions – has the same issue.
… and that’s when I realized why Nim’s readLine
function works the way it does. And was reminded of a best practice my dad (- another programmer -) told me about from back in the days before C++ and Java had unseated C as the only reasonable choice for Serious Programmers –
The Result Argument.
Nowadays, most languages default to pass-by-value arguments, like this:
let manipulateString = (str) => {
str = str.toUpperCase()
}
let myString = "hello"
manipulateString(myString)
console.log(myString) // "hello"
You can try this in your browser’s developer tools. myString
will remain unchanged, because the str
argument contains a copy of the original value. Nim functions (known as “procs” – since Nim is a procedural language) work in a very similar way:
proc maniuplateString (str: string) =
str = str.toUpperAscii() # does not compile - str is an immutable copy
let myString = "hello"
manipulateString(myString)
echo myString # hello
You can give manipulateString the power to modify str, however, by turning it into a mutable reference, using the var keyword:
proc maniuplateString (str: var string) =
str = str.toUpperAscii()
var myString = "hello"
manipulateString(myString)
echo myString # HELLO
The ability to pass by reference means that we can store the result of a successful function call in its last argument, and use the function’s return value to indicate success or failure, like this:
proc getUserName (id: int, username: var string): bool =
let user = database.getUserById(id)
if user == nil:
return false
else:
username = user.username
return true
var username # = ""
if not getUserName(999, username):
return false # pass along the failure status
echo username
Nim auto-initializes all declared variables to sensible defaults; it also does not allow users to ignore a function’s return value – you have to explicitly discard it, with the self-documenting discard keyword:
var username
discard getUserName(999, username)
echo username # ""
This forces you to think about what you want to do with the true/false value returned by getUserName
, and it forces you to think about what – if anything – is being stored in the username variable. If you absolutely refuse to think, and getUserName can’t get the username from the database, then we fall back on an empty string, which follows the principle of least surprise.
The real power of this convention manifests in a loop:
var input = ""
while stdin.readLine(input):
doSomethingWith(input)
Your function can even use an enum value, to indicate exactly what went wrong:
type errno = enum
# "errno" is a callback to that old best practice from C
Success,
InvalidUserId,
UserNotFound,
KeyCollision
proc getUserName(id: int, username: var string): errno =
if id < 0: return InvalidUserId
# ... etc. ...
I’m not sure how I feel about this way of doing things as opposed to exceptions. And I wish Nim had something like C++’ & operator to more clearly signal to the reader when we are and are not passing by reference. But I think I will try this way out for awhile in my side projects, and see how it holds up.
Iranian leaders have denied the legitimacy of the internationally recognized sovereign state of Israel at every UN meeting they have ever attended, and called for its destruction.
Today, they called the leaders of a terror group that has killed hundreds of Americans (and thousands of Lebanese and Syrians, and which, within recent weeks, internationally targeted 12 Druze children for the crime of playing soccer in Northern Israel), “our martyrs,” and attacked Israel in “retaliation” for their assassination.
In doing so, Iran explicitly identified with a military organization, Hizbullah, that is currently engaged in an active campaign to drive the Jewish people living in Israel from their homes. It amounts to an open declaration of war, and proves that their commitment to the destruction of Israel is more than just words.
If Israel is to survive, it has no choice but to address the immediate, existential threat posed by Iran.
The world is really simple – and that makes everything complicated. Lots and lots – billions – of simple pieces – very simple pieces – all moving around and interacting at the same time. It’s very complicated.
I hate playing chess. I used to think I was smart, but then I realized how simple chess was, and how bad I was at it. I want chess to be something that smart people can just be good at – but it’s a skill, like everything else – so I’m not good at it, which means I’m not a smart as I thought I was, because I can’t just pick it up, look at it, and figure it out. So I hate it.
Everything is like that. The things that were made by smart people, for smart people, usually end up getting mastered – and taken over – by stupid people who want the benefits of playing the same closed games we smart people invented in order to keep them out. And then it isn’t fun anymore, because before long, the stupid people get to be smarter than the smart people, and we have to find something else to do.
It’s like the rubiks cube. I tried to figure out how to figure out how to solve it – without algorithms. I don’t want to memorize a set of steps, I want to see how smart I am. But after I solved it a few times, I started to realize that there are certain sets of steps you can follow that work every time… and then it got boring, because all I was doing, at that point, was memorizing a set of steps.
Everything eventually becomes a science – which means that the more magic you practice, the less magic there is in the universe. At some point, all the magic in the universe will just become science and then life won’t be worth living anymore.
If you really want to be special you have to invent something. A religion, a martial art, a bit of software. A book. To be the first one – but, even then, it’s not guaranteed. Even if you’re the first one, someone else with no talent can come along and sell your thing and take all the credit.
You also have to be careful not to fall into the trap of mass producing other people’s inventions for money – artists end up drawing the same pictures, again and again – software developers inevitably find themselves “specializing” in writing and rewriting the same application, again and again, in different languages and for different companies. At some point, you stop inventing altogether and become a full time salesman – someone with no talent selling other people’s ideas and taking all the credit. But if it puts food on my children’s plates…. ?
Here’s another thing that happens when you are a creator. An idea gets into your head, and it won’t go away. Most of the time you can drink another cup of coffee, or hit the weights again, or watch another YouTube video, or go for another walk, or smoke another cigarette, or… and then, after all that, if the idea still won’t go away, so that you can sit down and get busy doing whatever it is you’re really supposed to be doing, for whoever is paying you to do it, what then?
If I have to write one more paragraph about the election, or one more RESTful CRUD service… draw one more couple holding hands in front of a sunset on the beach… or stand in this horse stance for one more minute…
I had a melt-down last night. They’ve been happening more and more frequently.
Maybe I should stop drinking so much coffee.
Imagine a world where there was a group of people who decided which ideas were “correct,” and made an example out of anyone who expressed dissent.
Imagine a world where people were not permitted to organize around a shared set of spiritual beliefs, or to share those beliefs with others.
Imagine a world where you had to surrender yourself as a slave to those who had access to the goods and services necessary for your survival, instead of engaging with them in an equitable, voluntary system of exchange.
Imagine a world where, once you had spent your time – which is your life – to earn money, that money and the things you bought with it could be taken away from you by people who you had no say in appointing.
Imagine living in a world where others could determine your status as a person, based on arbitrary criteria designed specifically to exclude you.
Being American means being willing to give your life to prevent that from happening.
And being against Israel means support for those who are willing to kill and die to ensure it happens to the Jewish people.
It actually makes sense that some Jewish people support “Palestine,” and here’s why. Jewish people follow a set of teachings and belong to a community where compassion and justice are highly valued. Jewish people have a long history of standing on the side of those who they see as having been oppressed, “because we know what it’s like.” In addition, Jewish individuals are generally free to – no, they are encouraged to – think for themselves and support whatever causes seem the most reasonable. This is actually an idea that “The West” copied from Judaism (indirectly, by way of Protestant Christianity).
Now I’m going to Google to see how many activist groups I can find with names like “Muslims for Israel,” or “Zionist Qataris,” or “Gazans against antisemitism.” Wish me luck. 🙂
… until He has revealed His secret to His servants, the prophets.” — Amos 3:7
I’m learning that in the old days, before the Bible was written, Jah transmitted His infallible truth through the mouths of the prophets. Those very prophets wrote down whatever He told them to, and it became the Bible. Now, that Bible has become the final source of God’s authoritative word for us.
I’m pretty sure there were other prophecies, too, that weren’t meant for us; but everything that was, can now be found in the scripture.
Another thing I’m learning is that the Lord has specific, significant things that He is still planning to do in the future; “secrets,” if you will. That doesn’t mean He necessarily told the prophets what I’m going to have for breakfast tomorrow, or how many children I’m going to have, or even necessarily what they (or I) should do for a living.
But the prophecies recorded in the Bible do have something to say about my life specifically, and about the course that human history is going to take in general. There are specific plans that He wants to share with me, and specific things that He wants to make me aware of, at specific points in time.
This is where modern “born-again’s” get the idea (and I have to apologize, but “born-again” will have to do until I can think of a better shorthand for referring to my chosen spiritual path) – but, it’s where we get the idea of finding “promises” in the Bible, or of getting directions from God’s Word that extend beyond its explicit, general wisdom, moral teachings, and lore.
What I’m describing here isn’t bibliomancy; it’s a process of trying to discern the voice and direction of God, guided by Spirit (you know – the One He promised to pour out on all flesh?), and the prophecies He instructed His prophets to record for us, their sons and daughters.
By immersing ourselves in those ancient writings, we can be convicted of sin. We can learn sound doctrine, transcendent wisdom, and Jewish and Christian history and heritage; and, we can learn God’s general (and specific!) will for our lives.
Protestants love to talk about “biblical” things – biblical teaching, biblical morality, biblical living. I’m going to coin a new phrase, and start talking about things that are “Bible-informed,” instead. Scripture wasn’t meant to be treated like a creed or a text book; it wasn’t meant to serve as the basis of a new sect of Christianity. All it is, is a collection of infallible prophecies – recorded in the past – to which we can refer for insight into what God wants to say to us now – always with a full understanding that, unlike what was transmitted in writing by those ancient prophets, our own apprehension of God’s message will never be perfect.
I want to allow scripture to shape the way I think and the way I relate to God and my fellow humans. I want it to transform me in subtle ways, as I am cultivated by it into a branch, with deep roots in the Vine, that can eventually begin to produce Spiritual fruit.
This is the deep spirituality I have been searching for all my life. This scripture – this prophecy – is the water I have been longing to swim in.
Now, I am finally home.
Hear what is just, O Jehovah; listen to me.
Give ear to the prayer of my unfeigned lips.
Let my sentence come out of Your presence;
Let Your eyes see what is right.
You have examined my heart.
You have visited me in the night.
You have tested me and found nothing.
I have resolved that my mouth will not transgress.
As for the deeds of men –
By the word of Your lips,
I have kept myself
From the ways of the violent.
May my steps remain in Your paths;
Then my feet will not slip.
I have called on You, for you will answer me, O God.
Turn Your ear to me; hear my speech.
Show Your marvelous loving kindness –
You, Who, by Your right hand,
Save those who trust in You
From those who rise against them.
Keep me as the apple of Your eye;
Hide me under the shadow of Your wings,
From the wicked who oppress me,
From my mortal enemies who surround me.
They have walled themselves in with comfort;
With their mouths, they speak proudly.
They have surrounded us in our steps, now;
They have set their sights, crouching on the ground,
Like a lion greedy for his prey –
Like a young lion, lurking in a thicket.
Rise up, Jehovah; deter him –
Bring him low.
Deliver me from the sword of the wicked one –
From the hand of worldly men, O Jehovah –
Whose portion is in this life;
Whose bellies are filled with their due;
Whose children will be filled with it;
Who leave the rest to their little ones.
But I –
I will see Your face in my innocence.
I will be satisfied,
When I awake, to visions of You.
a paraphrase