Results 1 to 3 of 3

Thread: Java Concepts(?) + SRL

  1. #1
    Join Date
    Dec 2016
    Posts
    49
    Mentioned
    0 Post(s)
    Quoted
    22 Post(s)

    Default Java Concepts(?) + SRL

    *Note* This is a work in progress as I'm still learning and would love any discussion on the topics below!
    Hi all! To explain I began taking a course on Java via Pluralsight and had some concepts that were mentioned during the training that brought up some questions in regards to if these can be used within SRL. Really just looking for discussion in helping to better understand so any input is welcome.

    1. Chaining if-else (AKA scan-react method per our guides)
    Per the instructor "while this is a bad practice - it is not "illegal""...OK. Great.
    So in SRL terms given the nature of our scripts is this still considered bad practice? If so please explain your reasoning.
    I personally use this on majority of my scripts as it's "simple" and easy for me to understand - especially the flow.

    2. Multi-Variable Declarations
    See example;
    Simba Code:
    Int v1=10, v2=4, diff;

    Now - the only way I could get something "close" to this was the following:
    Simba Code:
    var
      exec: integer =1;
    I don't know how often it would get used - but in my opinion this doesn't seem like a bad idea and would be great for keeping your script "clean". Anyone know if something of this nature is possible or if there's a "best practice" to this? (see my next note on Variable Scope*)

    3. Variable Scope
    I feel like I've seen this posted multiple times "keep your variables local". Maybe I'm just making stuff up though. Anywho...
    Why use local variable's opposed to global variables?
    I understand a variable is holding a block of space but wouldn't this "improve" performance?
    What is the best practice and why is that? Is this a potential memory issue?

    4. Conditional Assignment/Logic
    Alright as for this I haven't even bothered testing on my own yet. For example:
    Simba Code:
    int rooms = 0;
    int students = 30;

    if (rooms > 0 && students/rooms > 30) {...}
    // What this actually does...
    // if rooms > 0 {
    //   if students/rooms > 30
    //     System.out.println('blah');
       }

    So you see we just took 3 lines of code and simplified it drastically down to only 1. I thought this was absolutely bad ass. As stated I haven't tested anything like this within SRL but are the capabilities there?

    Edit: Further thought I know I've done similar to do 2 and sometimes even 3 checks by doing "and" within a scan/react method. I believe there was one in Java however where basically you use ? so if the first one doesn't turn out good the second one will then run. I think this was "conditional assignment" ex.
    Simba Code:
    int v1=7
    int v2=5

    int vMax = v1 > v2 ? v1: v2

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,588
    Mentioned
    395 Post(s)
    Quoted
    1594 Post(s)

    Default

    Quote Originally Posted by BlitzKrieger View Post
    ...
    1. I assume you mean multiple nested if-statements. No it's not bad practice and sometimes it's necessary. However, if a statement has too much logic and can be packaged into a function call, then do that.

    If you mean ternary operator chaining, then Simba has no such thing.. IE:
    Java Code:
    int x = y > 0 ? z < 1 ? 9 : 8 : 10;

    //same as:

    int x = 0;
    if (y > 0)
    {
        if (z < 1)
        {
            x = 9;
        }
        else
        {
            x = 8;
        }
    }
    else
    {
        x = 10;
    }

    So use if-statements instead (you'll still get short-circuit evaluation).


    2. You cannot initialize multiple variables at the same time in a single statement. You can do something like:

    Simba Code:
    var
      x: Integer = 0; y: Integer = 1; z: Integer = 2;

    which is the same as:

    Simba Code:
    var
      x: Integer = 0;
      y: Integer = 1;
      z: Integer = 2;

    or

    Simba Code:
    var
      x, y, z: Integer;
    begin
      x := 0;
      y := 1;
      z := 2;

      x := y := z := 0; //initialize all to 0.
    end;


    3. Who said to keep all of them local? Keep them where they're supposed to be. If the variable is used by the entire script, then it is a global variable. IE: Constants (although constants aren't "variables". They are constant.

    For example, Bitmaps.. If they are used throughout the entire script, why not load the bitmap once (stored in a global variable).. and some time before the script terminates, free the bitmap.. This way, you aren't loading a bitmap every function call (Local scope)..

    3b. No there is no performance difference. Simba is interpreted. Not compiled. I doubt you'd see any difference in a compiled language as well.. Besides.. that's MICRO-OPTIMIZATION which is frowned upon in any programming language unless you absolutely 100% need every "bit" of performance you can get (IE: Micro-controller programming and Game-Programming, etc)..


    4.
    Simba Code:
    var
      rooms: Integer = 0;
      students: Integer = 30;
    begin
      if ((rooms > 0) and (students / rooms > 30)) then
      begin
        //...
      end;

      //What this actually does..
      if rooms > 0 then
        if students / rooms > 30 then
          writeln('blah');
    end;
    Last edited by Brandon; 08-16-2017 at 12:43 AM.
    I am Ggzz..

  3. #3
    Join Date
    Dec 2016
    Posts
    49
    Mentioned
    0 Post(s)
    Quoted
    22 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    1. I assume you mean multiple nested if-statements. No it's not bad practice and sometimes it's necessary. However, if a statement has too much logic and can be packaged into a function call, then do that.

    If you mean ternary operator chaining, then Simba has no such thing.. IE:
    Java Code:
    int x = y > 0 ? z < 1 ? 9 : 8 : 10;

    //same as:

    int x = 0;
    if (y > 0)
    {
        if (z < 1)
        {
            x = 9;
        }
        else
        {
            x = 8;
        }
    }
    else
    {
        x = 10;
    }

    2. You cannot initialize multiple variables at the same time in a single statement. You can do something like:

    Simba Code:
    var
      x: Integer = 0; y: Integer = 1; z: Integer = 2;

    which is the same as:

    Simba Code:
    var
      x: Integer = 0;
      y: Integer = 1;
      z: Integer = 2;

    or

    Simba Code:
    var
      x, y, z: Integer;
    begin
      x := 0;
      y := 1;
      z := 2;

      x := y := z := 0; //initialize all to 0.
    end;


    3. Who said to keep all of them local? Keep them where they're supposed to be. If the variable is used by the entire script, then it is a global variable. IE: Constants (although constants aren't "variables". They are constant.

    For example, Bitmaps.. If they are used throughout the entire script, why not load the bitmap once (stored in a global variable).. and some time before the script terminates, free the bitmap.. This way, you aren't loading a bitmap every function call (Local scope)..

    3b. No there is no performance difference. Simba is interpreted. Not compiled. I doubt you'd see any difference in a compiled language as well.. Besides.. that's MICRO-OPTIMIZATION which is frowned upon in any programming language unless you absolutely 100% need every "bit" of performance you can get (IE: Micro-controller programming and Game-Programming, etc)..


    4.
    Simba Code:
    var
      rooms: Integer = 0;
      students: Integer = 30;
    begin
      if ((rooms > 0) and (students / rooms > 30)) then
      begin
        //...
      end;

      //What this actually does..
      if rooms > 0 then
        if students / rooms > 30 then
          writeln('blah');
    end;
    I edit section 4 prior to seeing your response. I was aware of using and - I apparently got a little excited over it and spaced out.
    Update: Edit: Further thought I know I've done similar to do 2 and sometimes even 3 checks by doing "and" within a scan/react method. I believe there was one in Java however where basically you use ? so if the first one doesn't turn out good the second one will then run. I think this was "conditional assignment" ex.

    As for 1. Chaining if else - I don't think it would be considered "nesting" but you tell me.
    Simba Code:
    if (...) then
    else if (...) then
    else if (...) then
    else if (...) then
    //and so on and so forth...

    Now obviously dependent on what's above it could be as simple as using a case - although that's not where I'm going with this. More of a "MainLoop" type of deal where it's doing individual checks to determine what it needs to run next. This likely won't change your original answer but just want to make sure we're on the same page.

    Appreciate the fast response!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •