Content AND Presentation

(Note to English-speaking readers: the links entitled Komentarze na tej stronie lead to comment pages.)

2016-08-22 Jan Góra OP on work and inspiration

I was reading an article (in Polish) by Agnieszka Chrostowska about Jan Góra OP, the legendary Dominican friar who died last year. It is difficult to summarize who Father Góra is for people not in the know, and (not really being one of his spiritual children, and in fact not really being a huge fan of him) I won’t even dare to try. I have to admit, though, that his vision, his love for Christ, for the Church, and for the people – especially the young – is undeniable. I consider him a real Catholic: well grounded in reality and at the same time totally in love with God (in fact, the latter implies the former). Not being without his sins (who of us is free of them anyway?), I am quite convinced he is safe in heaven now, and I wouldn’t be surprised to learn the he is continuing his job of bringing the young people to God and to maturity.

One quotation from the article really did resonate with me. In my poor translation, the author says this:

He often told us that a real artist does not wait for inspiration, but gets to work at once, and only corrects when inspiration comes. It sometimes annoyed us that he seemed content with the first, sketchy versions of music, graphics, texts, and circulated them immediately to everyone in the world. But he used to say that what we had then, even imperfect, is better than the perfect we’d never achieve. He was usually right […].

(To be fair, the author continues with a lovely anecdote about how “usually” is not “always”, but that does not mean that the above rule of thumb is wrong!)

If you need or want inspiration from a person who looks like a workaholic but clearly is not (since for a workaholic, work is everything, and for a healthy person, work is a means to some higher end), Jan Góra OP might be a good candidate to read about. If you tend to sometimes justify your disobedience to legitimate authorities with excuses like “good intentions” etc., he might not be the best candidate to read about. But (even justified) criticism of his flaws (and again, I’m pretty sure mine are much worse) aside, I am grateful to God for that extraordinary man.

CategoryBlog, CategoryEnglish, CategoryFaith

Comments on this page

2016-08-15 JavaScript callbacks

Note: this is an introductory article on JavaScript callbacks. There will be a bit more advanced follow-up – an introduction to promises.

When programming a computer, you often want your program to do something first and something else next. The player shoots, and after some time the bullet hits or misses. You enter a formula in a spreadsheet, and after a while the whole spreadsheet is refreshed. You get the idea. The usual way to express such a sequence in time is to write some statement and then write another one after that.

There is at least one situation, though, when this way of coding is a Very Bad Idea™. Assume you have an environment which can only execute one program (thread) at the time (like, say, Emacs, or a web browser). Assume that you have a task for your program which can take a significant amount of time to finish (like, say, fetching some data over the network). What’s more, the lengthy part is not really done by your program – some external service does it, and the only thing your program has to do is waiting for it to complete its task. Instead of blocking the whole application for a few seconds needed to finish the data retrieval, it would be better to “launch” it and get to other task, which can be done before the data arrive. (Many modern websites do things like that. For instance, if yoy ask Facebook to “show more comments”, you can still do things on the page while the comments are being loaded.) In other words, you want your program to do other things while waiting for the results of the long operation.

The usual solution to this problem is via callbacks. The idea is that instead of waiting doing nothing while the long operation is being performed, our program can do other things. Only after the job is done, we “get back” to the point where we started it and do whatever we wanted with e.g. the data retrieved.

There’s one catch here, though. Since our program is executed by one thread, it can’t really do two things at a time. So how it’s possible that we continue doing what we were doing after telling the system to so something that takes a long time to complete, and then get back to process the results of that long operation? Shouldn’t we somehow interrupt the execution of the former code?

Indeed, twenty years ago the right mechanism for this mode of operation would probably be so-called interrupts, a low-level trick that enabled the processor to suspend whatever it worked on, deal with the “interrupt”, or signal, telling it that something needs its attention now, and get back to its work when the interrupt processing was finished.

In case of web applications in JavaScript, which are usually interactive, things are a bit different. An important characteristic of interactive applications (JS or not) is that they don’t do anything most of the time. Usually they wait for user input (mouse clicks, key presses etc.), then do what the user told them to do, and get back to the “waiting” state again. And that’s the moment when they can easily finish whatever they had started earlier.

OK, so enough theory. Let’s try it practically. Since this post is about JavaScript, we’ll use node, which can be easily run from the command line. We will simulate the long-running operation using a while (true) loop, breaking out of it when the time comes.

var wait_busily = function(time, log) {
	var end = Date.now() + time;
	while (true) {
		if (Date.now() > end)
			break;
	}
	console.log(log);
};

(I found it somewhere on StackOverflow, with the caveat that you should never use something like that in production code, since it blocks the thread completely for the given amount of time. This is exactly what I need for this experiment, though – to simulate a program which is really busy. In a moment we’ll see how a function which waits a designated amount of time and then does something should be implemented.)

You may now prepare a file wait.js with that function and then two lines saying e.g.

wait_busily(1000, 'a');
wait_busily(1000, 'b');

and launch node wait from the console. Good: we wait a second, get an “a”, wait another second and get a “b”.

Now imagine that our first function, which takes a second to complete, is really retrieving something from a database or the Internet. It still takes a second to complete, but someone else physically does it. In other words, our thread is free during that second. Instead of just idly waiting for the long operation to complete, it can do a whole lot of more useful things, like launch other similar requests, or just react to user input (if we are in a browser instead of node.js and the user clicks something, it is rather lame to make him wait a second for any reaction!).

Let us simulate the “wait for the result, but do something different” scheme. One question immediately springs to mind: if we just launch our long operation and proceed to other things right after that, how do we (a) know when the long operation completed and (b) get its result?

Enter callbacks. Many functions which may take a long time to finish and do not need the local processor to actually do the job accept a special kind of parameter, which is a function to be invoked later when the long operation is complete (and the thread has nothing else to do). This function often accepts an argument, which is the result of our long operation. (In fact, usually it accepts two arguments: while this is not enforced by the langauge, a common practice is that the former argument is the error (or null if the operation succeeded), and the latter the result.)

Coming back to our example, we are going now to write a function wait_lazily, which (like wait_busily) will accept time and a character for logging purposes, but also a callback launched when the desgnated time passes. (JavaScript already has a very similar function, called setTimeout, but we want also the “visual logging” capability, i.e., printing a character after we’re done.) Since we won’t have any meaningful “result” of the waiting, the callback will not accept any arguments (or more precisely, will ignore them).

var wait_lazily = function(time, log, callback) {
	setTimeout(function() {
		console.log(log);
		if (callback)
			callback();
	}, time);
};

Try now this code:

wait_lazily(1000, 'a');
wait_lazily(2000, 'b');

Of course, in practice it might happen that we wanted to do something after, say, “a” finished its job:

wait_lazily(1000, 'a', function() {
	wait_lazily(2000, 'c');
});
wait_lazily(2000, 'b');

This way “c” could use the result of “a” (but not “b”). Of course, “b” does not have access to the results of any of “a” and “c”.

It is not difficult to imagine that this can, well, escalate quickly: if we have several functions, each needing the result of the previous one(s), things may get complex. Also, it is not immediately obvious how to handle a situation when “c” needs the results of both “a” and “b” (and we want “a” and “b” to run in parallel, but we don’t even know which is going to finish first). In such situations, when using plain vanilla callbacks is cumbersome, one can use promises – but that will have to wait for another post.

CategoryEnglish, CategoryBlog, CategoryJavaScript

Comments on this page

2016-08-08 Two books on personal finance

As I mentioned some time ago, I have been using Ledger for personal (or rather home) accounting. In particular, this means that I’m quite interested in the subject of personal finance. Today I’m going to write about two books on this topic I find worth reading.

Disclaimer. What I write in this post is in large part my personal opinion. While I do my best not to claim any false statements, please be warned that I’m going to write about issues I’m no real authority on. Should you find any mistake on my part, please let me know. In any case, filter what I’m writing through your knowledge and common sense, which may well both be much better than mine.

The first book is not out yet, and when it’s out, it will be probably of no use for most of my readers (sorry!), since it is written in Polish. The book is called Finansowy Ninja (Financial Ninja), which is quite an evocative title;-). I preordered it from the author, Michał Szafrański (who self-publishes it), and so far got a pdf with the first chapter. I have to say that I really liked it, even though it’s only an introductory part. One thing I especially liked was that it is very concise: no unnecessary stuff, no repeating the same with other words many times. So far it’s thumbs up from me – we’ll see how the other chapters go (though I assume from reading the blog of the author that it’s going to be full of very concrete advice, backed by hard data).

While Financial Ninja concentrates mostly on the technical side – accounting, budgeting, frugal spending etc. – Howard Dayton’s Your money counts expands the ethical side of earning, spending, saving and – last but not least! – giving away money. The subtitle – The Biblical guide to earning, spending, saving, investing, giving and getting out of debt – pretty much says it all. While this book also covers some technical issues (budgeting, for instance) – its goal is completely different (though complementary) to Financial Ninja: it is about how to apply the Biblical, Christian perspective to managing your finance. I read it with pleasure, even though the editorial side of the Polish version is beyond terrible. One gripe I had with the book was that while it was well grounded in the Bible (no surprise here), there was nothing from the teaching of the Holy Church. Not that I’m astonished – I strongly suspect that the author is a Protestant. The book has an imprimatur, though, and it seems that it is mostly theologically and morally correct (perhaps with one exception, where the author talks on investing, and praises the compound interest; on the other hand, usury, i.e. lending money on interest higher than the costs of lending said money, was criticized by popes and other Catholic authorities), so I can recommend it (with that one caveat – though of course, I’m no theologian). Still, I long for a really Catholic book on the subject.

While it is rather difficult for me to somehow sum up Financial Ninja before I’ve actully read it;-), I guess I could do it with Your money counts. The main takeaway from that book (at least for me) is the simple (and obvious in retrospect!) but crucial observation that I do not own anything – all “my” money and possessions are actually God’s property. This is quite liberating (as usually with religious truths, at least in Catholicism) – I do not really have to (and shouldn’t indeed) worry about having enough money too much (note, though, that “not worrying” is completely different from “not caring”!). On the other hand, it means that my responsibility for the money and possessions I happen to earn, use and spend is much larger than if it were merely “mine”: I am only a manager, and God himself is the Owner. Wow, I’d better not spend it on stupid things.

Also, while the book does not explicitly mention that, an interview I’ve recently read on a similar subject contained a thought that really resonated with the book: that if I have some money (especially a lot of it), some portion of it (the one above my fair amount of posession, is that is the right term – probably not, please remember again that as far as theology and philosophy goes, I’m a layman, so my choice of words may be completely off the mark – but hopefully you know what I mean) belongs to the poor – and not because of charity, but because of justice. And by the way, notice how different it is from socialism and its likes: the reason for the moral obligation to share is neither “human rights”, nor the concept that economic inequality is somehow immoral, nor other left-wing nonsense like those; the real reason seems to be that my money is not really mine – I’ve only been entrusted with the task of managing it, and while I totally should use a portion of it, I should also remember that I administer it on behalf of God, its proper Owner, who does care about the poor and wants my managing to reflect that.

Anyway, I’m even more motivated now to better manage my (or rather “my”) stuff.

CategoryBlog, CategoryEnglish, CategoryFaith

Comments on this page

More...

(Więcej means More in Polish; click it to see older entries.)

CategoryEnglish, CategoryBlog