Asserts in Servoy – Joachim Hilgers
Joachin Hilgers explains how to use Asserts in Servoy
So, hello everybody. So this session is about documentation, now just kidding, sorry. As you might remember, I’m coming from Foxport background and one of the commands I’m missing in the boy is a sword, familiar with a sword. Okay, the other ones do not know about it, so it’s a keyword or a command in Foxpro, Java, C sharp, and might be another link as well. So I implemented my sword for Java or for Savoy. And on this side, on the left side, you can see some code, which is handling the clicker off the table, on the red side. I’m pretty sure all of you have written something similar or at least the NG guys, they’ve written something like this. So you get in the found set, call the index record, and then you have just an if block, which says, oh, if it’s a custom ID, I want to do this and otherwise I want to do something else. Most probably all of you have done something, something for a G grid. So I’m a lazy programmer and I’m trying to write a safe code, defensive coding. So the else of this one always has some coding for me. If I miss one column, I just added another column which should be added to something like that, and I don’t handle it, I want to see it. I want to get a warning, hey, something is missing there. Typically you would put something like this in there or a log warning or whatever, I put in the sword. The sword in this case pulls and the text. Another thing here is on the top. Many languages have something where you can make sure that you pass in the parameters in a way that you can check the parameters. So you can do that with an if, whatever, and say, okay, here’s something coming in, which I’m not expecting. That’s one side of the coin. The other side is how do you make sure that your parameters are what you’re expecting? In there where you have no way to check this. There are some languages where you can make sure that the parameters are checked when they come in and you get an exception. Hey, that’s not what I’m expecting. You can do that pretty easily with an assert. Here, you have column index if this column index is less three. Something is wrong. So let’s deseet see this in practice. They are click on the first column. Everything is fine. Okay, the first it runs. I’m pretty sure the Fox guys now know what might happen. If I press on the second column, I get an assert failed dialogue. So I’m now able to either continue this able to assert. I don’t want this to see this dialogue. What is able all this assert? Here is my assert error. It’s going to the log as well. Okay, this message only pops up if I’m in developer. So I’m developing and I find it pretty nice to have a warning and see how something is going wrong. Something is not as I’m expecting it. And if you have the possibility to use this message here and search in your code, where is that coming from? In this case, I know where it is. Put a great point here. Say continue and then you are in your code at that point. So it’s an easy debugging feature which might make it much easier to just find arrows in your code. Okay. Let’s continue. So we had this arrow which says, okay, we haven’t unhandled on click. Let’s try to click on column number four. Three. That’s the other assert. That’s this one here. Firey. Huh. So I’m sending in a parameter which is out of the range I am expecting. Easy, easy. So how complicated might the code be? Not complicated. It’s just a single scope, a single method. And it’s looking like this. Now that’s the assert. Let’s go. Everyone could understand or can understand. You can use it. Throw in this single scope file into your application and you will be happy. This one saved my life because on the productions we saw that we did not handle a data change on the grid where we changed the edited, the added flag of the other column. And in the else we had this assert and this just wrote to our log. Oh, there’s a lot of problems in there. We did not handle that correctly. So that’s all. Thank you. Thank you. Thank you. Thank you.
