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) { …
Kategorie-Archive: C
Unicode and C
It is a common practice in C to use arrays of char as strings. The 0 is used as end marker. The whole thing was created like that in the 1970s and at that time it was kind of cool to get away with one less language feature and to express it in terms of …
Flashsort in Ruby
Deutsch There is a simple implementation of Flashsort in Ruby, after having already provided an implementation in C. The C-implementation is typically faster than the libc-function qsort, but this depends always on the data and on how well the metric-function has been written, that is needed on top of the comparison function for Flashsort. You …
How to draw lines, circles and other curves
These ideas were developed more than 30 years without knowing that they were already known at that time… Today the graphics cards can easily do things like this in very little time. And today’s CPUs are of course really good at multiplying. So this has lost a lot of its immediate relevance, but it is …
Logging
Deutsch Software often contains a logging functionality. Usually entries one or sometimes multiple lines are appended to a file, written to syslog or to stdout, from where they are redirected into a file. They are telling us something about what the software is doing. Usually we can ignore all of it, but as soon as …
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 …
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 …
Carry Bit, Overflow Bit and Signed Integers
It has already been explained how the Carry Bit works for addition. Now there was interest in a comment about how it would work for negative numbers. The point is, that the calculation of the carry bit does not have any dependency on the sign. The nature of the carry bit is that it is …
Will Java, C, C++ and C# be the new Cobols?
A few decades ago most programming was performed in Cobol (I do not want to shout it), Fortran, Rexx and some typical main frame languages, that hardly made it to the Linux-, Unix- or MS-Windows-world. They are still present, but mostly used for maintenance and extension of existing software, but less often for writing new …
Frameworks for Unit Testing and Mocking
Unit testing has fortunately become an important issue in many software projects. The idea of automatic software based unit and integration tests is actually quite old. The typical Linux software that is downloaded as source code and then built with steps like tar xfzvv «software-name-with-version».tar.gz cd «software-name-with-version» ./configure make sudo make install often allows a …