Extracting the logic for finding minimum an maximum into a new block Pro Preview

One of the smartest things you could do in any software program is to extract logic in small reusable, simple, understandable units. In EV3-G these are called Blocks and we are going to extract the logic for finding a minimum and maximum for each of the sensors in a new block.

  • #645
  • 04 Oct 2017
  • 2:37

Why blocks

First rule of EV3-G - if the program is taking more than one screen it is difficult to use and understand it. 

By extracting into a new block you turn this:

Into this:

Much better, easier and understandable. 


Now that we've seen that there are no problems on the display and that we detect the correct minimum and maximum values the next thing that we must do on our program is to extract part of the logic into a new block. And the logic that we are extracting is the one that finds the minimum and maximum values of the all 4 sensors.

We select with the mouse all the blocks that are in the loop

like this then we go to Tools>MyBlock Builder

and in the MyBlock Builder first we must set a name and it's FindMinMax so that's a nice way to name it. So, we find the minimum and maximum and we should select some kind of an icon. Now, there are a couple of ways to think about it. First, probably we can select the icon with the array because we are doing some operations with an array or we can have some icon with a sensor. So, we prefer the one with an array and then we finish.

And now I can finally zoom in

and we have a nice little program that's easy to understand and it's a program where we init the array, we move, we find the minimum and maximum,

we do this for about 1 second, we stop and then we display all the values on the display. So, in this way we've extracted our complex logic for finding the minimum and maximum into a block and now we can use this block in other programs that we are also developing.