Exceptions to implement Program Logic

Sometimes it is conveniant to use exceptions for implementing the regular program logic. Assuming we want to find some data and then process them. When no data is found, this step can be skipped, because there is nothing to do. So we could program something like this: public Data readData(Param param) {     Data data = db.read(param);     if (data.isEmpty()) {         throw new NotFoundException(„nothing found“);     }     return data; } public ProcessedData doWork(Param param) { …

Share Button

Meaningless Whitespace in Textfiles

We use different file formats that are more or less tolerant to certain changes. Most well known is white space in text files. In some programming languages white space (space, newline, carriage return, form feed, tabulator, vertical tab) has no meaning, as long as any whitespace is present. Examples for this are Java, Perl, Lisp …

Share Button

Loops with unknown nesting depth

We often encounter nested loops, like for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { doSomething(i, j); } } This can be nested to a few more levels without too much pain, as long as we observe that the number of iterations for each level need …

Share Button

How to recover the Carry Bit

As frequent readers might have observed, I like the concept of the Carry Bit as it allows for efficient implementations of long integer arithmetic, which I would like to use as default integer type for most application development. And unfortunately such facilities are not available in high level languages like C and Java. But it …

Share Button

Getter und Setter

English In der objektorientierten Programmierung gilt es als fortschrittlich, getter und setter zu verwenden, statt auf Attribute direkt zuzugreifen, weil das einem die Flexibilität gibt, später auf berechnete Attribute umzuschwenken. Etwas hässlich ist das, weil die getter und setter, etwas willkürlich den Attributnamen mit so einem vorangestellten „get“ oder „is“ oder „set“ und eventueller Umwandlung …

Share Button