Servoy Tutorial: Coding Efficiency

Servoy Tutorial Photo Credit: Gwen Vanhee via Compfight

Servoy Tutorial Photo Credit: Gwen Vanhee via Compfight

This is a Servoy tutorial on coding efficiency and frameworks. This time I want to talk about something very basic, but extremely important. It is going to help boast your productivity like nothing else I can share with you.

I have worked in small teams, and big teams, and it always surprises me when I see someone typing the same code over and over again, or searching, copying, pasting, and modifying, a snippet that they located from somewhere in the maze known as your application. Bottom-line is this type of behavior is highly unproductive, and if I am talking about you, then you are not going to get to the next level unless you change your ways now.

This lesson is primarily about the often under-utilized feature in Eclipse (Servoy Developer) called Templates. You may have used some built-in templates that came with Servoy out of the box for loops, try-catch blocks, etc. (shame on you if you haven’t). However, this tutorial is really about creating your own custom templates, so that you can instantly generate big blocks of well designed code, ready for you to tweak for the situation at hand. It’s easy to do, ensures accuracy and consistency, and will crank your productivity through the roof, leaving you lots of time to play COD online with your friends.

To fully appreciate the lesson in this Servoy tutorial, we need to expose you to a simple framework, something I use pretty much everyday. This is going to be a simple framework that takes care of all the work, and error trapping, for running SQL queries to obtain datasets and foundsets using the databaseManager, and running raw SQL using the rawSQL plugin. The purpose is not to convince you that my code, which is now over five years old, is the way to do it. The point is more to show you how you can build out a nice simple framework, and then build some Eclipse Templates so that you can use the framework easily and efficiently whenever needed.

Okay, so let’s get started. Here is the code that I use for obtaining a dataset with a SQL query. It uses the databaseManager to execute the query, ensure “scopes.enums” defaults are used if no data is passed in, and has a separate routine that is called to handle errors. I also have jUnit tests so the function can be validated automatically. This function, and the others to follow, are all in my “scopes.utils” library, and are all built to accept the same object. There are other auxiliary functions that I am not putting here, that take care of things like building IN clauses with multiple arguments, etc., that are really outside of the scope of this Servoy tutorial, but keep in mind that you probably will start with something like this, but then extend it, as unique situations arise.

This next function gets a foundset using the databaseManager. It also accepts the same object, has  jUnit tests to validate it, and any errors are handled by the same separate function.

This function handles the rawSQL plugin, also has jUnit tests for validation, and error reporting is handled by the same routine as in the other functions.

This is the error handling function. It accepts the object from the routine that called it, along with the error message. It then reports the error to the console, or in most cases, displays it in a dialog (pulled out for this example). It also has jUnit tests to validate that it is functioning properly.

Okay, so there are four functions, the beginnings of a simple framework. Just building a simple framework like this will save us a great deal of time, but we can make it even better. You should absolutely be building little frameworks like this for yourself, and your team, so that everyone is using the same functions to do the same thing, from one location, making it easy to maintain and extend, as well as ensuring consistency and reliability.

So let’s talk a minute about how we would use this. The functions have all been created to accept an object. So, if we wanted to use the “scopes.utils.sqlDataset()” function, we could do something like this:

So, this is already pretty easy to use, but we still have code we will be typing each time. Also, what if we forget the “oSQL.args” for example? Well, lets make an Eclipse Template to generate this code for us each time (less the SQL of course).

Go to the Eclipse Preferences, Javascript, Editor, and Templates. Click the NEW button to add a new Template.


Give the Template a name that you will remember. You will be able to start typing the name, then press CTR + SPACE to get Eclipse to suggest an available auto-completion template. I created myself three templates, one for each of the main functions, sqlDataset, sqlFoundset and sqlRaw. Notice that I use the “Insert Variable” button to place the “${cursor}” in the location I would like my cursor to be after the code is generated by the auto-completion.


Returning to Servoy, I begin to type the beginning of my code templates, “sql”, and press CTR + SPACE. Eclipse shows me the following selections.


If I type “sql_d” and press CTR + SPACE, Eclipse shows me the following:


And if I hit RETURN while it is displaying this selection, the code shown below is automatically inserted for me, and my cursor is positioned where I wanted it.

You can probably see how easy that was. Now I am ready to type my SQL and add my arguments. I can even see what the other options are, already set to defaults for my application from “scopes.enums”. Why would I want to type this block of code over and over again, every time I want to get a dataset using SQL?

Okay, so that was the point of this Servoy Tutorial. I wanted to make you understand how important it is for everyone in your team to use frameworks for the routine functions you will be using in your application, and how vital it is that you all start creating custom Eclipse Templates for increased productivity. Stop wasting time; life is too short! Get on it, start building out your templates today, and send your productivity through the roof.

That concludes this Servoy tutorial. I hope you enjoyed it!

Gary Dotzlaw
Gary Dotzlaw has 20+ years of experience as a professional software developer and has worked with over 100 companies throughout the USA and Canada. Gary has extensive qualifications in all facets of the project life cycle, including initial feasibility analysis, conceptual design, technical design, development, implementation and deployment. Gary is an expert Servoy Developer, with extensive large-commercial project experience, and has written numerous advanced tutorials on Servoy development.