Ok to use constants in call to size() in immediate mode?

edited August 2015 in Programming Questions

Both the reference page for size() and the new book on Processing by Reas and Fry state that the call to size() must be the first line in code, or the first line in setup(). I am not sure how strict this rule is, because it's actually violated even in the book in an example on p. 300.

My question is: is it safe to specify the parameters to size() as constants, for example as follows:

final int H=100; size (2*H, H);

This technique has worked in my tests, but is it safe?

I know that it would be possible to first define the constants outside setup(), and then use them as parameters to size() in setup(), but as a programming teacher I would like to introduce the use of constants before I introduce setup() and draw() and defining functions in general.

I have two reasons for wanting to specify parameters to size() as constants.

First, assume that the width:height ratio of the sketch should always be 2:1, but the actual size might change. Then good programming practice states that size should be specified as above, not by size(200,100), where you would have to change two values if size of the sketch were changed.

Second, I would prefer that the students specify the width and height of the sketch with their own constants, and use those constants in calculations at other places in the code, rather than the students using the internal variables "width" and "height".

Answers

  • edited August 2015 Answer ✓
    • Up till version 3.0a5 it's both valid & safe to use variables/expressions as size()'s arguments.
    • Although it's indeed less error prone making it 1st line in setup().
    • After that version, dunno whether that even work for the "Immediate Mode".
    • But for "Regular Mode", we need to place size() inside settings() so it can accept variables again:
      https://Processing.org/reference/settings_.html
  • GoToLoop,

    I just installed 3.0 beta 3 and yes, using constants in size() no longer works.

    I would like to make a petition: if at all possible, could the developers please enable the use of constants in call to size(). Note: constants, not variables.

  • edited August 2015 Answer ✓
    • You can tell them by yourself: https://GitHub.com/processing/processing/issues
    • But I believe this is technically not feasible now after all those deep changes in Series 3.
    • And whether it's constant or regular variables don't make any diff.
    • B/c it's PDE's pre-processor which parses all ".pde" files. And it's not that smart! >:)
  • GoToLoop,

    thanks! I opened an issue in github, so at least they know about it now.

    Much appreciated.

  • PS3 has a new method settings () which you can use to set the size using variables and probably constants.

  • Sorry mised the 'immediate mode' part.

Sign In or Register to comment.