tag:blogger.com,1999:blog-20053782298664432192024-03-05T23:55:32.557-08:00The puzzled programmerThis is mostly about programming but other subject might be discussed if I think they can be of interest to other programmers.
When you visit this blog Google will use Google cookies, Adsense cookie, Chocolate cookies and what have you. If you don't like this you can use a Web Anonymizer or similar technique to avoid this information gathering. jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.comBlogger34125tag:blogger.com,1999:blog-2005378229866443219.post-42925772230719682912018-01-24T21:16:00.000-08:002018-01-26T15:28:07.810-08:00Important programming languages to learn (a break from C++17 and friends)A break from C++17 and friends. If I had to name the programming languages that are important to learn I would include LISP somewhere near the top of the list (no pun intended).
One thing that speaks in favor of LISP is the fact that learning it will develop your programmer mind in ways that other programming languages will not. The other factor in favor of learning this language is the doors it will open for you. Armed with your knowledge of LISP you will be able to use a great number of good tools and platform. In Linux for example there is a large number of implementations available (Steel Bank Common Lisp, GNU Common Lisp and possibly others). LISP is also available on the Java VM in the form of Clojure, ABCL, KAWA (a Scheme) and possibly other implementations. For a few years now Lisp is also an option when programming for the Erlang VM as LFE (Lisp flavor Erlang). Erlang has a Virtual machine (BEAM) that has interesting characteristics and is certainly worth taking a look at. However, Erlang (the primary language for this platform) is not a language that has the same availability as Lisp so learning Erlang might be considered too large an investment. Fortunatly with a dialect of Lisp available you now have an interesting alternative. Here is me fooling around in the Erlang LFE REPL:
<pre>
Erlang R14B04 (erts-5.8.5) [source] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]
lfe: (set mytuple #(1 "Winston Churchill"))
#(1 "Winston Churchill")
lfe: (element 1 mytuple)
1
lfe> (element 2 mytuple)
"Winston Churchill"
lfe: (defun nth (n atuple) (element n atuple))
nth
lfe: (nth 1 mytuple)
1
lfe: (nth 2 mytuple)
"Winston Churchill"
lfe: (defun doubleit (x) (* 2 x))
doubleit
lfe: (doubleit 2342342342)
4684684684
</pre>
I create a tuple and peek at the values inside using the 'element' function. Since 'element' feels a little long I define a function named 'nth' to mimic a function available for List in other Common Lisp implementation, I also create a simple function 'doubleit'.
Here is me fooling around in the GNU GCL REPL:
<pre>
GCL (GNU Common Lisp) 2.6.7 CLtL1 Feb 1 2012 09:07:26
Source License: LGPL(gcl,gmp), GPL(unexec,bfd,xgcl)
Binary License: GPL due to GPL'ed components: (XGCL READLINE UNEXEC)
Modifications of this banner must retain notice of a compatible license
Dedicated to the memory of W. Schelter
Use (help) to get some basic information on how to use GCL.
Temporary directory for compiler files set to /tmp/
::(defvar *mylist*)
*MYLIST*
::(setf *mylist* '(1 "WInston"))
(1 "WInston")
::(nth 1 *mylist*)
"WInston"
>>(nth 0 *mylist*)
1
::(defun doubleit (x) (* 2 x))
DOUBLEIT
::(doubleit 4598349)
9196698
</pre>
Clojure alone is enough of a reason to learn Lisp. Even more so now that Clojure Script is available.
</pre<jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-29167096465640991052018-01-05T21:22:00.001-08:002018-01-10T16:37:09.235-08:00Fooling around with C++17 (part 1)<h2>New years resolution</h2>
In 2018 I will be back writing post here and I will try to be more consistent.
I will eventually go back to the subject of 'dynamic like' Java programming but just for fun I will start the year by looking at C++17 (including x14 and x11)
C++ is starting too look like a 21 century programming language so I think it is well worth looking at a few features that are starting to make programming really more productive.
WARNING: this is not a C++ style guide.
<h4>auto this, auto that</h4>
C++14 and 17 extend the power of auto and really make it more productive.
In C++11 they added the posibility of using auto for the return type of functions but you still had to provide the type using the new arrow syntax. In C++14 they completely removed the need to explicitly provide the type. Here is an example with both syntax:
<pre>
class LazyCoder
{
public:
auto get_it_Cv11 () -> int
{
return 11;
}
auto get_it_Cv17 ()
{
return 17;
}
};
</pre>
I don't think this is the best use of auto since the type in function declaration is also good documentation (better than comments).
In C++11 you could use auto for local variable declaration, for loop variables, function parameters.
With C++14 and C++17 you can now also use auto when declaring a variable in the scope of an 'if' and also when doing structured bindings (similar to other languages 'destructuring')
<pre>
// Create a tuple
auto tuple = std::make_tuple(3, 7);
// C++14
int i, j;
std::tie(i,j) = tuple;
assert (i == 3 && j == 7);
// C++17 (look ma, no explicit variable declaration)
auto [x, y] = tuple;
assert (x == 3 && y == 7);
// and then ...
if (auto t = (x != y))
{
// I can use t here
cout << "This is getting ridiculous like t=" << t << endl;
}
</pre>
C++z17 has one more trick up its sleeve. The 'structured binding' works with user data types.
<pre>
// I have this struct defined somwhere
struct Zmas
{
int z, w;
};
// Now I can do this:
Zmas zmas {7, 11}; // Uniform intialization with curly brackets (next post)
// The number of items between square bracket here must match the number of public
// members of Zmas
auto [z, w] = zmas;
assert (z == 7 && w == 11);
</pre>
Of course C++11 addition of auto was already a major improvement when working with complicated types but the latest extensions are even more icing on the cake.
For more details about auto check out <a href="https://skebanga.github.io/structured-bindings/">C++17 Structured Bindings</a> jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-68625374981788739332016-01-17T15:02:00.001-08:002016-04-11T16:59:09.162-07:00Dynamic like programming with statically typed Java.It's been a while since I posted anything here. These days I'm working on a Java project where I have made a lot of good design choices.
A lot of those ideas turn around the concept of using a "dynamic like" programming style with a statically typed language. When you think about it, a lot of the flexibility and power that comes from using dynamic languages comes from using APIs where Maps and Tuples are often used in some places where a statically typed language would use classes. Of course, dynamic languages often have syntactic support for Maps and Tuples that greatly simplify using them. Still, in the comming series of post I will show how Java programming can benefit from a more widespread use of Maps and Tuples. I'll show how using some well known pattern can help compensate for the lack of syntactic sugar. jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-2985030199465213092014-11-08T20:15:00.002-08:002014-11-09T08:46:21.730-08:00Nothing beats Bash scripting for file manipulationOne 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. <br/>
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:
<br/><br/>
1) Put the script in a variable using <i>cat</i> and <i>heredoc</i>
<pre><b>
vtest=$(cat - <<-'_EOT_'
/^[ \t]*package[ \t]+[a-zA-Z_]+/ {
print $0
}
/^[ \t]*import[ \t]+/ {
print $0
}
{}
_EOT_
)</b>
</pre>
Simply feed the heredoc to cat and then put this in a variable using <i>command substitution</i>. Don't forget the single quotes around the _EOT_ at the top. <br/>
<br/>
2) Feed the script to AWK using a redirected <i>echo</i> expression as if it were a file (-f).<br/>
<br/>
awk -f <(echo "$vtest") file_to_process<br/>
<br/>
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 <i>heredoc</i> 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).
<br/>
What do you think about this trick ? jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-7514593775178075432014-10-15T21:14:00.000-07:002014-10-20T21:12:53.960-07:00Next step selecting an alternate programming languageOk 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 ?jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-86627102813639104522014-08-19T20:11:00.002-07:002014-08-21T18:13:53.834-07:00Separating the user interface form the rest of the codeBefore 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:<br/>
- Ask the programmer to write the functions in a separate module (module A) <br/>
- Ask him to provide a command line utility (module B) to call the functions in module A<br/>
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:<br/>
- 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.<br/>
- 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.<br/>
= The end product will often be a useful deliverable (this is the best application of this trick)<br/>
Of course writing a command line utility should not remove the requirements for automated unit tests. <br/>
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 ?
jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-40208985911429769722014-08-17T20:31:00.000-07:002014-08-17T20:31:09.383-07:00Escape 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. jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-35245275861115272952014-07-29T20:29:00.000-07:002014-08-01T14:22:48.952-07:00Is Python a possible alternative for JavaOne 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) ?jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-7117057337144818822014-07-21T10:30:00.001-07:002014-07-29T20:29:51.090-07:00Finding an alternative for JavaSome 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.jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-41085577313885760562013-12-25T10:51:00.000-08:002013-12-25T10:53:31.637-08:00Another reason to use Netbeans instead of EclipseA couple of weeks ago I stumbled on a post by another Eclipse fanatic. The insect ... oups, blogger, was making fun of people using Netbeans. Well, fortunatly for him there was no comments or reply button on the page because I would have replied and my comment would have been difficult for him to handle: <blockquote>You might like the chaotic and always in beta look and feel of Eclipse however the biggest problem with Eclipse is its buggy Java compiler.</blockquote>
Some people don't even know this but Eclipse has his own "internal" Java compiler. Netbeans on the other hand uses the JDK supplied compiler. Eclipse's compiler has one advantage over the one in the JDK and that is the granularity of the compiled elements. Eclipse will compile and "link" smaller pieces of code. In a few cases where you have to stop debugging and restart in Netbeans, Eclipse will simply recompile the small bit of modified code and keep the debugging session alive. The problem however is that
the last time I looked there was still a bug in the compiler forcing us to add useless class cast in generic code. To that you might respond that it is not such a big issue. My response to this would be: well one of the benefit of generics is to remove the need for a lot of type casting in the code so for me it is important.
My other response to this would be that it is not the only problem with the Eclipse Java compiler. The reason I switch to Netbeans despite Eclipse being the standard where I work is that for a while in the transition to Java 1.7 the Eclipse compiler would simply not compile my project. This was eventually fixed (a week later) but I simply could not wait a whole week without a working IDE so I switched to Netbeans. I have no problems with that because I have always preferred Netbeans to Eclipse. Aside from what I would call the objective argument (an IDE should compile the code correctly) there is of course a subjective side to my preference for Netbeans. I prefer the more static placement of the Netbeans windows and views. It is flexible enough for me and without feeling chaotic like Eclipse. Also, Netbeans was in front of Eclipse supporting the preview version of Java 1.8 and I would not want to be the guinea pig for the 1.8 internal Eclipse compiler.jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-59733833439786849392013-06-02T08:23:00.001-07:002013-12-25T10:55:15.953-08:00The answer to the question at the end of the previous postWhat is the answer to the question at the end of the previous post:
<blockquote>Is using an enum for analyzer family a good way to customize the processing in the client application (CT) </blockquote>
Well the answer in the case of our projet was no.
The stable part of the application ecosystem was the CT and the analysis cycle step in it. Adding an enum that we would then use to skip step in the processing would have made it impossible for us to add new Analyzer class without opening the code of the CT to support the new class.(remember the OCP or Open Close Principle)<br/>
What is an API designer to do in such a case ? Well, what we did was define an interface for the steps in the analysis cycle of the CT and implement a kind of <i>Decorator</i> for that in all SM(I) instances. Of course, concrete instances of this interface are provided by the <i>IInstrumentDefinition</i> factory. The main interface for this is something like:<br/><br/>
<i>interface IAnalysisSteps<br/>
{<br/>
void doStep (String stepName);<br/>
}<br/>
</i><br/>
The CT supplies a default implementation of this that is in fact a supertype that looks like this:<br/><br/>
<i>interface IAnalysisStepHandler extends IAnalysisStep<br/>
{<br/>
void skipStep (String stepName);<br/>
}<br/>
</i><br/>
The IInstrumentDefinition interface has a factory method that looks like:<br/><br/>
IAnalysisStep getAnalysisStep (IAnalysisStepHandler analysisStepHandler);<br/>
<br/>
The SM(I) implements a version of <i>IAnalysisSteps</i> that will do one of two thing when it gets called:<br/><br/>
- Simply call the default implementation of the <i>IAnalysisStepHandler</i><br/>
- Call the <i>skipStep(String)</i> version of the method in the <i>IAnalysisStepHandler</i><br/>
<br/>
Now SM(I) instances can customize the steps in the CT without actually having to open the code of the CT. What if new analysis steps are added to the CT ? Well, in that case you don't have any choice but to open the CT and to support that you simply have to implement the SM(I) <i>IAnalysisSteps</i> decorator such that if an analysis step is unknown it is skipped. Since this step was originally not present in the CT skipping it should be a good default behavior for old SM(I) instances. I left out some details above like having different parameters for different analysis steps. That however is easily handled using a single additional parameter of type <i>Map<INamedParameter></i>. If you don't remember what a <i>INamedParameter</i> is simply look at the previous blog entry. jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-38274965785369415182013-05-22T21:02:00.001-07:002013-12-25T10:55:00.946-08:00Good software design principles (part 5)AS you have seen the two main interfaces of the SMF are:<br/>
- IInstrumentDefinition: an <i>Abstract Factory</i> that defines methods to get different classes to work with analyzers.<br/>
- IInstrument: a <i>Strategy</i> that implements all the steps needed to acquire data from an analyzer. Concrete instances of this are returned by concrete implementation of <i>IInstrumentDefinition</i> dynamically loaded from a .jar file.<br/>
The only class that needs to be public (exported) in the SM(I) .jar is the implementation of the <i>IInstrumentDefinition</i><br/>
Now, how do you customize the behaviour of the client application (CA) to work properly with a given SM(I) ? After all, the steps in a analysis sequence of the CA might not all be appropriate for the data returned by a given SM(I).<br/>
Would having a method that defines an analyser family be a good idea ? Something like:<br/>
Family getFamily()<br/>
Where Family would be an enum. Something like:<br/><br/>
public enum Family<br/>
{<br/>
FTIR,<br/>
PARTICLE_COUNTER,<br/>
MASS_SPECTROMETER<br/>
}<br/><br/>
Is this a good idea ? Think about this in light of the OCP.jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-77305109062330113472013-05-13T20:49:00.004-07:002013-12-25T10:54:46.155-08:00Good software design principle part 4Another thing that was a big success in the SMF was the use of List and Map(s) (Dictionary in .net). If a class looks like a List, smells like a List and sounds like a list then it probably is a List. This might sound trivial but I have seen project where a class with getters and setters was defined to handle a concept that obviously was a Map. People had to constantly update the interface when new properties where added and the whole thing really was a nightmare. Using data structures such as Lists and Maps makes programming in Java feel much more like dynamic programming. So the <i>IInstrumentDefinition</i> includes a number of methods that actually return unmodifiable Maps. The Maps are generic Map<String, <i>INamedParameter</i>> where the <i>INamedParameter</i> interface is a little one element heterogeneous container. The <i>INamedParameter</i> defines the name, class and value of a parameter or variable. A Map of those is a nice little package that can easily give access to the List<String> of <i>INamedParameter</i> names. Once you have the name you can get the actual <i>INamedParameter</i> from the Map. jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-1844858702164019822013-05-11T15:55:00.003-07:002013-12-25T10:54:35.606-08:00Good software design principle part 3In the previous post I gave a summary of the problem to be solved using the Successful Module. I explained that in fact the problem is solved using a Successful Module Framework (SMF) and Successful Module Instances SM(i). The SMF is a library used by the client application (CA) and the SM(i) to interact with each other without building cumbersome circular references and dependencies. In Java all modules are .jar files. At compile time both the CA and the SM(i) know the SMF but they don't know each other. Of course at runtime the CA needs to know how to load the SM(i) but that information is limited to the file location. Once it is loaded the CA interacts with the SM(i) as if it consisted only of classes (more precisely interfaces) defined in the SMF.
The CA loads the SM(i) .jar and looks for a class that implements an interface called <i>IInstrumentDefinition</i> that marks it as the point of entry into the module. In fact the CA knows only interfaces (pure abstract classes in C++) defined in the SMF. This is one of the key to the success of the whole project. No concrete classes in the API. To understand what the <i>IInstrumentDefinition</i> is we need to enumerate some of the interaction between the CA and an analyzer. The CA needs to:<br/>
1- Define the value of parameters used by the analyzer (resolution, number of scan, etc...)<br/>
2- Find the list of values that a particular analyzer can supply (the data it can return, etc...)<br/>
3- Start an acquisition <br/>
4- Read the status of the analyzer<br/>
5- Read the data at the end of an acquisition cycle<br/>
So. Does the IInstrumentDefinition need to supply operations (methods) related to all these interactions ? No, not at all.
In fact it turns out that many of the operations listed above are defined in a separate class called an <i>IInstrument</i>. Things like operation 3, 4 and 5 are defined in the <i>IInstrument</i>. We found that in practice it was very useful to have a specific interface - the IInstrumentDefinition - to define the functions that would provide the more "static" information about an analyzer and that it made sense that this interface be the entry point into the module. We also identified another very important function of the <i>IInstrumentDefinition</i>: <i>Abstract Factory</i>.<br/>
The instrument definition is a factory for a host of classes used when interacting with a specific analyzer. Of course the most important of the factory method is the <i>getInstrument()</i> method that returns a <i>IInstrument</i>. jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-88819961627082972112013-04-27T09:35:00.001-07:002013-12-25T10:54:09.884-08:00Good software design principles part 2For the benefit of this blog we will call the module mentioned in the previous post the SM for successful module. Remember that the objective for this module was to make our software capable of acquiring and analysing data from any third party instrument. When thinking about a problem like this many of you might immediately think about the Open Close Principle. If you did not think about this and/or if you do not know what the OCP is, here is a definition:<br/>
<blockquote>software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification</blockquote>
Here is a link to the Wikipedia article: <a href="http://en.wikipedia.org/wiki/Open/closed_principle">OCP</a><br/>
Trying to satisfy the OCP has a number of implication. One of them is that you should not have to modify the application code each time you want to support a new instrument. So of course the first thing that was decided was that the module should be loaded dynamically at runtime based on information in the application configuration. This is not earth shattering or revolutionary but it is a very useful concept. What we will be doing with the SM is to build a plugin infrastructure for our application instrument interface. Plugin are used in Eclipse, Netbeans and a whole menagerie of applications. In Java this is easy to do you just pack your SM in a .jar. Now the tricky par is to define the API that this jar file should implement. Another more immediate task is to define how the whole thing will be structured in terms of module. Now from the discussion we know that the starting point is this:<br/>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgixkMpAh_8nkqHGchvxAxr8fHWwOpczE8XE5DqNw4KmNXXtMcoSXLdzsuSp3zxc3Y562X5IUfoP5xsPi6IhrYrWwSIxf71MjLVIBWC2ldhem4dMQkNdf91zf8LGxy4bsLTfcEY1UkMgm4/s1600/FirstView.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgixkMpAh_8nkqHGchvxAxr8fHWwOpczE8XE5DqNw4KmNXXtMcoSXLdzsuSp3zxc3Y562X5IUfoP5xsPi6IhrYrWwSIxf71MjLVIBWC2ldhem4dMQkNdf91zf8LGxy4bsLTfcEY1UkMgm4/s320/FirstView.png" /></a><br/>
Now with a project like this you want to get your dependencies right from the beginning. In the case of this project it is easy to understand that has illustrated on the diagram communication between the two module goes both way. The Application supplies the SM with parameters and possibly other information and the SM returns statuses and results to the Application. We will probably need to allocate objects and possibly implement interfaces on the application side as well as in the SM. The question then is: where do we define those classes and interfaces ? The answer is: in a third module. Now the high level view of the project looks like this:<br/>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNDDYrq7TBLb4uqsGdjyXQKRwfi6aHWb1_ZgSy0bi_xYoCNMDJxP5Q4DWBlgZ1CPuMaPlfSp2dLZUaa3WE-WQ5TimBwoYN7DArOuyYJJyinmgu95IFjzN8onc_XBODDAEEdAvJjioPH4M/s1600/Packages.png" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNDDYrq7TBLb4uqsGdjyXQKRwfi6aHWb1_ZgSy0bi_xYoCNMDJxP5Q4DWBlgZ1CPuMaPlfSp2dLZUaa3WE-WQ5TimBwoYN7DArOuyYJJyinmgu95IFjzN8onc_XBODDAEEdAvJjioPH4M/s320/Packages.png" /></a><br/>
Now except for the weird looking arrow the elements on that diagram are packages with their dependencies. You have:<br/><br/>
1) Application.<br/>
2) SM(I). This is a specific SM Instance (I added the I between parentheses to highlight that).<br/>
3) SMF the Successful Module Framework<br/><br/>
As you can see there is no circular dependency in the standard UML elements. At compile time the APplication depends only on the SMF and the SM(I) also knows only the SMF. Now I added a non UML element (the weird broken arrow not quite connected) to represent the runtime dependencies in the system. I think having this extra arrow makes everything obvious and clean.
In my next blog entry we will continue on our analysis of the SM and SMF. In fact for a while the emphasis will turn on the SMF and the key patterns used in that module the most important being:<br/><br/>
- Abstract Factory<br/>
- Strategy<br/>
<br/>
Now a closing comment. It goes without saying that before you start on a project like this a good analysis and requirements definition phase is in order. This is beyond the scope of the current thread but we might come back to this or insert a few blog entries about this phase later.
jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-79454621511209155402013-04-20T21:20:00.001-07:002013-06-02T08:31:48.069-07:00Good software design principles and weekends at the beachI work on software that is used to read and analyse data from instruments made by the company I work for: Fourier Transform Infrared Spectrometer. The software I work on is a client/server type continuous acquisition and analysis software. I work on the server component of that software.<br/>
One key component used by the server is the data acquisition component - sometimes called the data acquisition driver. These days data acquisition module is a more appropriate name for it since this component is written in Java and does not really corresponds to what we would call a driver. In the old days, when our software was written in C/C++ the thing really was a driver. However, in recent years the link between the PC and our instrument was changed from a proprietary protocol to good old Ethernet TCP/IP link. Also, in the meantime, software development switched to Java. Of course, all of this is very nice since Java has very good Network communication libraries.<br/>
Now, our software is very flexible and has very good data processing capabilities so why not use it to analyse data from other instruments ? Well that's exactly what I was asked to do a few years ago: make our software capable of acquiring and analysing data from any third party instrument. Now, while this may sound like a simple task, doing it right really is not that simple.<br/>
In my next few blog entries I'm going to talk about how this problem was solved. Now of course, I'm not going to discuss this at a level of details that could get me in trouble with my employer but this is not a problem since the key to the success of the project are not in the kind of implementation details that could be considered trade secret. No actually the key to the success of this project was using well known and documented good software design principles. I think you will enjoy this.jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-64090965308010984252013-04-18T19:54:00.000-07:002013-04-18T19:54:10.807-07:00Eclipse vs Netbeans ... no contestEveryone is crazy about Eclipse. Me, I just can't stand Eclipse any more. Version 4.x was supposed to bring working multiple editor window but it just is not working. If you use a second editor window (drag an editor tab on a separate screen) the shortcut keys do not work and operations selected in the menu are simply not applied to the correct screen. I don't need billions of plugin. I need a working editor and just like I can't stand single pane file browsers I simply can't stand an editor that does not properly support multiple separate editor window. I spend most of my time editing code so I'm going back to good old clean and stable Netbeans. Which Java IDE should you use ? Well Netbeans of course.jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-48623810400002047912012-01-22T14:16:00.000-08:002013-04-14T07:26:41.701-07:00Writing good static argument validation methodsBefore we get into the NullObjectProxy class there is one more prerequisite to get out of the way.<br />
How many times did you have to write the following kind of code:<br />
<pre>// Example (1)
if (parameter > limit)
{
throw new IllegalArgumentException (
"'parameter' is too large: " + parameter + " > " + limit);
}
</pre>I decided that I had enough of this and that I wanted to be able to write something like:<br />
<pre>// Example (2)
validate (parameter > limit, "'parameter' is too large: " + parameter + ">" + limit);
</pre>However, a little thinking made me realize that this form is not really appropriate and would have annoying side effects. In this specific case a bunch of Strings would be allocated and concatenated each time the method is called and that is not good since it would make calling the method very different from the initial code. I did a little more thinking and I came up with:<br />
<pre>// Example 3
validate (parameter > limit, "'parameter' is too large: &1 > &2", parameter, limit);
</pre>The implementation for this is shown below:<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT3LkA4nQL-zdUvC5ILSaisYCAA7YofYrbTsxaqRT_sNGy4kKstzE34cXJFG6pvQxI4slBJimsREi51C8iH00tLgak8-1SXchvXv_asDmOFfxghF4cp1wsepVF2iOxYa4MOsYQ6KhcKIk/s1600/validate.jpeg" imageanchor="1" style=""><img border="0" height="392" width="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT3LkA4nQL-zdUvC5ILSaisYCAA7YofYrbTsxaqRT_sNGy4kKstzE34cXJFG6pvQxI4slBJimsREi51C8iH00tLgak8-1SXchvXv_asDmOFfxghF4cp1wsepVF2iOxYa4MOsYQ6KhcKIk/s400/validate.jpeg" /></a></div>Here is the getFormattedMessage(..) method:<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIk3isC9cchCi9zzUK8TUI3Y17puNUs1CqXTqL5Ys7a55ZwnwJVPffYVAry-Ry3jXKizgWVUrp5sGIItnQoLOXrMi0DgCDFEm0weYDNIoAtYTchzG5VjctbQEUmWnSBbKSeco5MuyktYk/s1600/getformattedmessage.jpeg" imageanchor="1" style=""><img border="0" height="376" width="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIk3isC9cchCi9zzUK8TUI3Y17puNUs1CqXTqL5Ys7a55ZwnwJVPffYVAry-Ry3jXKizgWVUrp5sGIItnQoLOXrMi0DgCDFEm0weYDNIoAtYTchzG5VjctbQEUmWnSBbKSeco5MuyktYk/s400/getformattedmessage.jpeg" /></a></div>Now with this version what you have on each call is that a few arguments are pushed on the stack. You still have annoying side effects though. You still get an Object[] allocated and filled on each call. Also, if you have primitive arguments you get autoboxing of those into primitive wrapper classes (Integer, FLoat, ...). Of course you can further optimize this by having different versions of the method with different primitive arguments. Fortunately it is easy to define such methods for the most common cases. Here is an example with two <i>int</i> arguments:<br />
<pre>public static final void validate (boolean condition, String formatString,
int firstValue, int secondValue)
</pre>Those methods are defined in a static utility class called <i>Verification</i> and this class contains one and two arguments versions for all primitive number types. Now, the <i>validationFailure(String, Object ...)</i> is called only if the condition fails and so objects are allocated only on failures. We at last have something close enough to the original construct that we can use on a routine basis.<br />
I was so pleased with this that I added a validateNotNull(boolean, String, Object ...) method:<br />
<pre>// Example (4a)
public static <T> T validateNotNull (T value, String formatString, Object ... arguments)
</pre>This is a generic method that will handle any type of reference and return the correct type (no cast needed). I also have a short form for this:<br />
<pre>// Example (4b)
public static <T> T validateNotNull (T value, String parameterName)
</pre>This short form will print a predefined message:<br />
<pre>parameterName should not be null
</pre>You can use those methods like this (short form):<br />
<pre>// Example (5)
SomeType localVariable = validateNotNull (parameterOfSomeType, "parameterOfSomeType");
</pre>The verification class also contains a number of <i>expect(boolean, String)</i>, <i>expect(boolean, String, Object ...)</i> and <i>expectNotNull(Object, String)</i> methods. Those are used to test other conditions that would normally throw IllegalStateExceptions.<br />
Ok, now your thinking "that's a lot of trouble for an assert mechanism". The reason I don't use the Java assert mechanism for this is that:<br />
1) I don't want it to be possible to disable those verifications and I want them enabled by default.<br />
2) My Verification class methods have a very usefull trace mechanism build-in. When activated this trace will always get printed even if exceptions are thrown away or lost (handled improperly in thread, ...).<br />
I really like those little methods and I use them a lot. They make it really easy to build meaningful error information reporting into the code.<br />
Of course I will use them in my next blog about my new NullObjectProxy class.jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-15251442147981886772011-12-28T03:04:00.000-08:002012-01-21T13:29:28.103-08:00When is it ok to use method overloading in Java ?In his excellent book <a href="http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683">Efficient Java Programming</a>, Joshua Bloch has an item titled: <blockquote>Item 41: Use overloading judiciously</blockquote>Because I'm going to use method overloading in my NullObjectProxy implementation I will give some additional information about when it is ok to use method overloading. I highly recommend that you read <i>Item 41</i> but here is a little extra graphic help and also a relaxation of Joshua Bloch's criteria. To simplify the presentation I will assume that the return type of all methods here is <i>void</i> and I will not include the return type in the examples. <b>For a method <i>m(t1 x1, t2 x2, ..., tn xn)</i> where <i>t1</i> to <i>tn</i> are the n parameter types it is safe to create an overloaded version <i>m(t'1 x1, t'2 x2, ..., t'n xn)</i> if for each type ti (i=1 to n), t'i is not a subtype or supertype of t.</b><br />
Lets look at a few examples. We will base those examples on the set of classes illustrated here <div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZqV60QaHcxNxt_1Mnpcbow9XeDkMdBBCV-m6JkCtXjSLAsVIDZTrIXOBORRMLlnVAe0i2pJ_bYi17u3ain-nwM_mEQrpiRs6ceLS01Tv_RrX41xvkCeumPi4XyU6-wxqWTtFibVYWdUQ/s1600/hierarchy.jpeg" imageanchor="1" style=""><img border="0" height="230" width="302" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZqV60QaHcxNxt_1Mnpcbow9XeDkMdBBCV-m6JkCtXjSLAsVIDZTrIXOBORRMLlnVAe0i2pJ_bYi17u3ain-nwM_mEQrpiRs6ceLS01Tv_RrX41xvkCeumPi4XyU6-wxqWTtFibVYWdUQ/s400/hierarchy.jpeg" /></a></div>We will use a one parameter method as our existing method to overload: <i>m(Class-X x1)</i><br />
The following overloaded versions would be ok:<br />
<i>m(Class-Y x1)</i><br />
<i>m(Class-Z x1)</i><br />
The following would not be ok:<br />
<i>m(Class-A x1)</i><br />
<i>m(Class-d x1)</i><br />
Had the original method been <i>m(Class-A x1)</i> there would have been no safe way to overload using the types in the diagram. <br />
Had the original method been <i>m(Class-d x1)</i> then the following overloaded versions would have been ok:<br />
<i>m(Class-Y x1)</i><br />
<i>m(Class-Z x1)</i><br />
But <i>m(Class-A x1)</i> or <i>m(Class-X x1)</i> would not be safe.<br />
You get the idea.jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-41266322913604141622011-12-24T14:34:00.000-08:002011-12-24T14:34:36.971-08:00Learning from experienceSince my last post I have had several occasions of using my NullObjectProxy class in practice. I learned a lot from that. The first thing I realized was that I needed to allow defining behavior for the Object class methods (hashCode, equals, toString). Used in the confined environment of unit tests this was not obvious but used in real code it really became evident that this was needed. In fact I decided that I needed to provide a default coherent behavior for those methods.<br />
I also realized that I needed to provide a more sophisticated method of defining return value even for a Null object.<br />
The result of all this was that I modified my initial code quite a bit. So in my next post I am going to start over on the NullObjectProxy and used the updated more sophisticated version of this class.jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-5137761848096366522011-08-22T07:36:00.000-07:002011-08-22T07:42:26.023-07:00The Builder and Null Object pattern meet the Java Proxy classI love the Null Object pattern. It is a simple and useful pattern in many situations. The archetypal example is in the context of JUnit tests (or whatever is your favourite testing framework). In unit tests, Mock objects are often little more than Null Objects. Obviously, there are very good libraries for mocking objects in unit tests and I guess I could use this for Null objects in regular code but that would bring a lot of dead code into my application since those libraries provide some pretty fancy features. I need something simpler.<br />
I have used the Java dynamic java.lang.reflect.Proxy class for things like debugging proxy and other applications so I though using this class for Null Object would be great (if you need a refresher on Java Dynamic Proxy check <a href="http://www.javaworld.com/javaworld/jw-11-2000/jw-1110-proxy.html">Explore the Dynamic Proxy API</a>). In fact since a Null object has only one immutable state you only have to worry about the return value of its method. I want to keep this simple so what I will do is I will use two Map(s) in my proxy handler: one with return types for keys (Class<?>) and one with method names for keys (String). This will allow me to define return value for different return types and return values for different method names. This implementation does not support having different return value for methods with the same name and different parameters (overloaded methods). This should not be a big limitation since a Null object is likely to return the same value in this case. Here are the declarations for my Map(s):<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBukUL9oc6di3mKDp5UftdJPL9Pl7CwcryD2RrYBGwP87a4Yt0VTRe6ZqFVplgk04cxD5gtb_4dp2CPqtBT2Sl7FvVXTbxsmCYBW78_pCidNZJ8pmWWz7lOnlU-UuD0giIpWJu6UAM_gU/s1600/handler_return_type_map.png" imageanchor="1" style=""><img border="0" height="224" width="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBukUL9oc6di3mKDp5UftdJPL9Pl7CwcryD2RrYBGwP87a4Yt0VTRe6ZqFVplgk04cxD5gtb_4dp2CPqtBT2Sl7FvVXTbxsmCYBW78_pCidNZJ8pmWWz7lOnlU-UuD0giIpWJu6UAM_gU/s400/handler_return_type_map.png" /></a></div>The returnTypeLookup must include values for primitive types since methods that return those cannot default to null. As a bonus the Map includes a consistent set of values for primitive wrapper classes. <br />
The code below shows how the Map(s) are used in the invoke method of my handler:<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiowFoFpDkWpzsNBZv4UsHCkSAkSszXfnoH-qdMnnVGeItUCZ_ch1WiIhofm85MR-wvUEX9f_TSw6pwKEXjZY-ZVq3H6UsGKmuMXvQ5ZtBFujhqD0OpXVB5OxtGsEaXcPZ-JvxmAUFD-F8/s1600/handler_invoke_method.png" imageanchor="1" style=""><img border="0" height="223" width="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiowFoFpDkWpzsNBZv4UsHCkSAkSszXfnoH-qdMnnVGeItUCZ_ch1WiIhofm85MR-wvUEX9f_TSw6pwKEXjZY-ZVq3H6UsGKmuMXvQ5ZtBFujhqD0OpXVB5OxtGsEaXcPZ-JvxmAUFD-F8/s400/handler_invoke_method.png" /></a></div>As you can see we first check if there is a custom return value for the invoked method name and then we check if there is a custom value for the return type. This makes it possible to handle most common cases where you want to override the default.<br />
Now how can we customize our Proxy ? We could use different constructors for our invocation handler or some extra methods to override values. I have considered several options and finally I think I have found a very elegant solution. Here is what creating a Null object using my API looks like: <br />
<div class="separator" style="clear: both; text-align: left;"><pre><i>
ISomeInterface r =
NullObjectProxy.newInstance (ISomeInterface.class)
.whenReturnType (boolean.class, true)
.whenMethodName ("someMethodName", new SomeUserDefinedType())
.build ();</i></pre></div>To achieve this I use a generic <i>Factory</i> method and a <i>Builder</i> class.In my next post I will show you how to define a class that can be used with such a flexible and programmer friendly syntax.<br />
<br />
<br />
jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com1tag:blogger.com,1999:blog-2005378229866443219.post-12780228350321878052011-07-06T19:45:00.000-07:002011-07-07T05:53:45.500-07:00Microsoft and Oracle killing the cash cowI don't know if Microsoft and Oracle realize that they have to be careful in their battle with Google and Android. Personally, I think this whole patent business could end up killing the cash cow or at least weakening it quite a bit. The Android technology has created a very dynamic ecosystem that has generated what I think is a non negligible amount of business activity. Both Microsoft and Oracle are probably already benefiting indirectly from this economic activity. I wonder how many developers are running Eclipse using Oracle JRE and using the Android SDK on Windows based PCs.<br />Instead of being always destructive maybe Oracle and Microsoft should start to think about how they could benefit from the Android platform (I know, that sounds weird). I'm sure they would find some positive ways to get money out of the platform.jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-45444640661409637992009-12-25T09:53:00.000-08:002009-12-25T10:21:48.642-08:00My first JavaFX scriptI just wrote my first JavaFX script. This script is actually a little benchmark I run when first looking at a new language. Is it rigorous and scientific ? Not at all. It is a very modest benchmark that gives me a feel for the language speed. It includes a bunch of operations typical of the kind of code that I write (string manipulations, collection manipulations, etc...). The code is not optimized to any degree and might actually run faster (or slower) if I was to use more idiomatic style. I also use a number of Java class. I think this is needed because this is also representative of what actual code will look like.<br />So what is the result ? Turns out that in this benchmark JavaFX is only 2 times slower than Java (511 milliseconds in one tests compared to 272 using Java). If you compare that to other JVM languages that is actually quite good. Groovy for example runs this particular benchmark in about 2600 millis (about 5 times slower than JavaFX). Of course JavaFX is a statically typed language so I was hoping I would get that kind of performance. I suspect that this will improve with subsequent versions. Scala for example runs the benchmark in about the same time as Java. <br />Of course speed is not the only criteria for language selection. Groovy for example being a dynamic language has a lot of characteristics that make it really appealing. However, in my case for the kind of applications I work on I cannot run 10 times slower than Java. If I can write only a small portion of an application using a language then this language becomes much less interesting. With the kind of speed that JavaFX is giving me I know I can write most of my code using it. That makes it really appealing. Now if JavaFX graphic rendering can get fast enough and with its already compeling UI capability it becomes a clear winner in my book.jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-54233911033032746652009-11-01T22:40:00.001-08:002009-11-01T22:42:42.810-08:00In a previous blog I mentioned using DbC as a technique on a project. I forgot to mention one important element that is very important in DbC and that's invariants. Since I did not use any fancy tools to work with DbC I had to find a way to define invariants. I choose to simply define a private method called invariants and added a call to this method in a conditional compile block at the beginning and end of every public methods. Like I said in my previous discussion this simple way of using DbC adds a bit of clutter to the code but this is a small cost to pay to get the benefit of DbC.<br />Invariants are assertions about a class that are always true. For a linked list for example:<br /><pre><br /> first != null || size == 0<br /></pre><br />Combined with preconditions and postconditions the impact on code correctness is just amazing. Thinking about the assertions just get you there faster. <br />On my next project I just might use Microsoft's Contract tool. The only annoying thing is that I probably won't be able to use this with MONO.jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0tag:blogger.com,1999:blog-2005378229866443219.post-46631145870346093712009-10-18T22:01:00.000-07:002009-10-18T22:40:27.232-07:00FInally a decent Groovy and Scala IDEYesterday I installed InterlliJ Community edition on my Linux system at home. I have not completed my evaluation but I must say that what I have seen so far looks good.<br />Groovy support is available "out of the box" while Scala support can be added by downloading a plug-in. I have been reading about both languages and I even started writing little scripts at work in Groovy (utilities to extract data from text files and such). However I must say that the lack of a good IDE was quite a hindrance. As a Java programmer I expect a lot from my IDE being used to programs like Eclipse and Netbeans. Some of the productivity gains from those new languages are not so impressive when one is used to working with a good IDE. Take for example the Groovy (or Scala) "def" keyword. This is often presented as one of the advantages of Groovy over Java. You can replace:<br /><pre><br /> StringBuilder myString = new StringBuilder();<br /></pre><br />with:<br /><pre><br /> def myString = new StringBuilder()<br /></pre><br />The problem is that in Eclipse for example when I enter the first expression I type the following:<br /><pre><br /> myString = new StringBuilder();<br /></pre><br />Then I just press <ctrl>+F1 and select "Create local variable" and the IDE adds the missing type at the beginning of the line.<br />Another example is the Groovy @Delegate annotation that generates delegate methods for a given member. Again in Eclipse I just right click the member and select "Generate delegate methods" from the Source menu.<br />Of course, in this category both Groovy and Scala offer much more gains then what an IDE like Eclipse can offer. However, those gains have to be weighted against the loss of other IDE functionality. As a Java programmer and Eclipse user I expect a good browser for my language. A browser is an essential part of a good OOP environment. This is so true that Smalltalk development kits have always included a browser. It is somewhat painful to apply good OOP principles if you don't have a browser (good OO programming tends to result in more numerous small classes). <br />The other must of course is code completion. The large number of core classes and API makes this absolutely essential. <br />I think a good open source environment like IntelliJ will contribute to the adoption of both Groovy and Scala.jslarochellehttp://www.blogger.com/profile/18074208326694485418noreply@blogger.com0