Friday, September 26, 2014

What I love about Xojo - Part I - Caching and optimization

Caching - Have you even done a programming exercise, in any language, and done something like this?
      int x = array.length
      int average = 10/x

Have you wondered why you didn't just do this?
     int average = 10/array.length

Because they both work, but one requires more lines of code. Aren't we supposed to use as few lines of code as possible (within reason)?

The answer is explained in Xojo's book, Introduction to Programming, by Brad Rhine. On pages 89-90, Mr. Rhine explains that if you use a built-in function and you're using a loop, for example, the entire function has to run every time the loop iterates. If you assign the function to a variable, in other words, if you cache it, your app will speed up by accessing the value of the built-in function instead of running the function each iteration.

Seriously. To date, I've taken several programming classes and done some self-teaching and never has this been explained. See also this article that goes into a lot more depth.

Xojo gets the first thumbs up from me for explaining a common concept that should be explained in every intro class for any language.

No comments:

Post a Comment