Saturday, November 8, 2014

Nothing beats Bash scripting for file manipulation

One of its strength of course is that it comes standard with most Linux distro. You can also have the power of bash scripts under Windows using Cygwin. A powerful element of bash scripting is its capacity to use external utilities like AWK or SED.
I use AWK a lot so let me share with you a way to integrate AWK script in bash scripts without having to use separate files. Here is the simplest way I found:

1) Put the script in a variable using cat and heredoc

vtest=$(cat - <<-'_EOT_'
    /^[ \t]*package[ \t]+[a-zA-Z_]+/ {
        print $0
    /^[ \t]*import[ \t]+/ {
        print $0
Simply feed the heredoc to cat and then put this in a variable using command substitution. Don't forget the single quotes around the _EOT_ at the top.

2) Feed the script to AWK using a redirected echo expression as if it were a file (-f).

awk -f <(echo "$vtest") file_to_process

In this case here, notice the use of double quotes around the variable to preserve the end of line and other formatting info. I use this if the AWK scripts gets complicated enough and readability becomes a factor. Of course you don't want to abuse this by putting really large scripts inside variables. In my example the script was made meaningless by removing a few lines for simplification but because of the nice formatting with heredoc you can still clearly see that both the java package and import statements received special treatment (the script was used to process Java source files).
What do you think about this trick ?

Wednesday, October 15, 2014

Next step selecting an alternate programming language

Ok it's down to ADA or Object Pascal (the Free Pascal compiler). I still have to check the license for FP in more details but it looks good. Two languages that have compilers on several platform and libraries to go along. I have worked with Turbo Pascal and Delphi years ago and the Free Pascal version of the language is a very close clone of Delphi so the learning curve should not be too bad. I have played a little with ADA and so far I think the language has very interesting features. I might actually start a more detailed thread about my ADA experimentations. What do you think ? Does anyone out there have any comments about either language ?

Tuesday, August 19, 2014

Separating the user interface form the rest of the code

Before I go back to my exploration of alternate programming languages a short one on design (or is it management ?). Every programmer knows that when you write code that might eventually get used in a GUI application a basic good practice is to always clearly separate the GUI code from the code implementing the actual functions. No big news here. There are a lot of tricks and design principles to help towards the goal of keeping things separate and of course any decent programmer should know about design patterns like MVC. Unfortunately, good design principles can be ignored or neglected if someone else is writing the actual code and additional control mechanism are needed (even you might benefit from additional safeguards). Code reviews can help. Automated unit test will also often help by encouraging developers to encapsulate and write more focused classes. Today's design or management trick is:
- Ask the programmer to write the functions in a separate module (module A)
- Ask him to provide a command line utility (module B) to call the functions in module A
Of course this does not provide an absolute protection against bad programming but it adds another level of control. This can be similar to what automated unit tests provide but not exactly the same since:
- It is fairly easy to check that module B uses A but not the reverse (no circular dependencies). You might not have the same level of isolation for the unit test code and the tested code.
- It is more focused on separating the two parts of the code. The part that implements the user interface (command line for this part of the project) from the part that implements the functions.
= The end product will often be a useful deliverable (this is the best application of this trick)
Of course writing a command line utility should not remove the requirements for automated unit tests.
If the result is not perfect, you or the other programmer doing the work will get another shot at cleaning up the API of module A when the time comes to use the module with the GUI. What do you think about this. Do you have any tricks to help you use good design principle ?

Sunday, August 17, 2014

Escape from the land of C (prologue)

Ok. I'm having a look at ADA. One of rare languages that actually was compared to the aberrant C language to compare productivity. More in my next blog.

Tuesday, July 29, 2014

Is Python a possible alternative for Java

One of the language I evaluated as an alternative to Java of course was Python despite my hatred for languages that have significant white spaces. I did not get very far. In general, the syntax of the language is ok (except for whitespace) but the problem is speed. CPython is terribly slow as in unacceptably slow so I looked at PyPy since this uses a JIT compiler. No luck. It is better that CPython but it is not a substitute for it. You simply cannot assume that the program you ran in CPython will run with PyPy. Some libraries do not run with the JIT and there are a few other incompatibilities. So, Python will not be the successor for Java in my book. What would be your choice of multi-platform open source programming language (really open source - no license for embedded systems) ?

Monday, July 21, 2014

Finding an alternative for Java

Some people at work question the use of Java as our main programming language. Since I am not a Java language fanatic I welcome the discussion however, several of the arguments mentioned against Java simply do not resist close scrutiny. Speed when compared to C/C++ ? JIT compiled Java speed is very close to C/C++ and the problems we have encountered in the past where speed was a problem were mostly related to I/O or ... bad design. Verbosity when compared to other languages ? Not such a big problem since the IDE handles most of the scaffolding (the infamous curly brackets, etc ...) and Java 8 removes some of the verbosity. Certainly not worth switching language specially when you consider the advantage of Java. Advantages that include: an incredible ecosystem of tools an libraries, a very good specification, several of the advantages of modern languages such as Garbage Collection and good support for multi-core processors. In fact, since I love programming languages I did try to find a good alternative because of the license issue. Having to pay for a license on embedded systems is a problem when trying to cut cost. Finding a viable lower cost alternative is not obvious. More on this in the next blog.