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.

Tuesday, September 9, 2014

Break out of a sentinel-controlled loop in Java/C#

The issue is the same in Java and C# . To exit a loop with a sentinel-controlled value, you need a break statement.

Example:
while(someValue != "Q"){
      //code for loop to execute while sentinel value not entered
      if(someValue.equals("Q"){
             break;//MUST USE THIS OR YOU WILL LOOP FOREVER
}

I think MSDN does a much better and simpler job explaining break. But I did manage to find some Oracle documentation that included code examples here.

In general, I am finding any documentation on Java to be spotty and not well written in the sense of not providing a substantive explanation with code examples. (Sorry to all the Java die-hards I offend.) In this case, you can see that MSDN uses one sentence to explain the break statement while the title of the Oracle documentation will make it difficult to even find this in a search using keyword "break".

The moral of the story: Java is a competitor to C# so they are very similar. If you can't find info on your Java issue, you might find it in MSDN and be able to apply it to Java.