HISE Logo Forum
    • Categories
    • Register
    • Login

    Does the Order of Declaring Identifiers (Variables, Functions, etc.) Matter?

    Scheduled Pinned Locked Moved Solved Scripting
    7 Posts 2 Posters 214 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • clevername27C
      clevername27
      last edited by

      Does it matter where in HISE Script I declare identifiers?

      Stuff like:

      const var panel_TM_REMIC_ToggleSystem_GUI = Content.getComponent("panel_TM_REMIC_ToggleSystem_GUI");
      
      combo_TM_REMIC_MicSelectionNew_GUI.setControlCallback(on_TM_NewMicSelector_Control);
      

      I've seen people write that variables should be declared in the onInit callback, but this seems ungainly and decreases code portability, especially with large projects.

      Then again, I've noticed different behaviour in HISE Script depending on the order I declare things.

      Also - does it matter what order I include external script files?

      include "names_of_ferrets.js";
      include "list_of_ferrets.js";
      

      I've seem to have noticed a difference here, as well - as far as I know, there's no header mechanism, though.

      Thanks!

      d.healeyD 1 Reply Last reply Reply Quote 0
      • d.healeyD
        d.healey @clevername27
        last edited by

        @clevername27 said in Does the Order of Declaring Identifiers (Variables, Functions, etc.) Matter?:

        I've seen people write that variables should be declared in the onInit callback, but this seems ungainly and decreases code portability

        Use namespaces

        @clevername27 said in Does the Order of Declaring Identifiers (Variables, Functions, etc.) Matter?:

        Also - does it matter what order I include external script files

        Yes, at least if those files contain namespaces

        Libre Wave - Freedom respecting instruments and effects
        My Patreon - HISE tutorials
        YouTube Channel - Public HISE tutorials

        clevername27C 1 Reply Last reply Reply Quote 1
        • clevername27C clevername27 has marked this topic as solved on
        • clevername27C
          clevername27 @d.healey
          last edited by clevername27

          @d-healey You are a gentleman and scholar. I"m just not quite there yet - I understand namespaces and HISE's implementation - forgive me for being a little dense, but I'm unclear as to the answer to my question, and how namespaces is related? 🧐

          d.healeyD 1 Reply Last reply Reply Quote 0
          • clevername27C clevername27 has marked this topic as unsolved on
          • d.healeyD
            d.healey @clevername27
            last edited by d.healey

            @clevername27

            Ok so when you have a namespace, for example

            namespace MyNamespace
            {
            }
            

            Everything inside of it is essentially within on init but its encapsulated and as long as you don't declare vars in it the scope of its variables is contained within. This keeps things neat and tidy.

            Unfortunately we can't nest namespaces, so for situations where you want to declare some enums within a namespace I use a const object. e.g.

            namespace MyNamespace
            {
                const MY_ENUMS = {
                    MY_FIRST_ENUM: 1,
                    MY_SECOND_ENUM: 2,
                    MY_THIRD_ENUM: 3
                }
            }
            

            So then I can access this from anywhere in the script using MyNamespace.MY_ENUMS.MY_SECOND_ENUM keeping everything nice and neat and making it easy to identify where a particular variable has been declared.

            For other namespace variables that I want to be accessible from outside the namespace (although I try to avoid that situation) I use getter and setter functions. Of course you could just access a variable directly but a getter and setter is better because if the variable name changes or the way to access them changes you only need to update that in the namespace and the other places that access them don't need to know anything about it.

            Regarding the order of includes. If you have an include with a namespace, and a second include with a namespace that references something in the first namespace, then they must be included in order.

            Libre Wave - Freedom respecting instruments and effects
            My Patreon - HISE tutorials
            YouTube Channel - Public HISE tutorials

            clevername27C 1 Reply Last reply Reply Quote 0
            • clevername27C
              clevername27 @d.healey
              last edited by

              @d-healey Thank you for your extended explanation. Do I have this correct, then—

              If I declare a constant after it's referenced, I'll have a problem. But I declare it in exactly the same place within a namespace, it will be fine—because before HISE executes anything, it goes through all the scripts and looks for things defined in namespaces (and builds the symbol table)?

              d.healeyD 1 Reply Last reply Reply Quote 0
              • d.healeyD
                d.healey @clevername27
                last edited by

                @clevername27 said in Does the Order of Declaring Identifiers (Variables, Functions, etc.) Matter?:

                If I declare a constant after it's referenced, I'll have a problem.

                07c6bf49-3e0e-4aa7-a5fc-7685835b3dee-image.png

                Yes

                @clevername27 said in Does the Order of Declaring Identifiers (Variables, Functions, etc.) Matter?:

                But I declare it in exactly the same place within a namespace, it will be fine

                9c8f28a5-22c9-42f7-a982-1ffa4261b9b9-image.png

                No

                I was saying if you have two namespaces in two separate files, and one namespace makes reference to the other, then you must put them in the correct order so that the reference is after the declaration.

                For example this works:

                b3b54e5d-845b-42aa-9d5c-bac8a3a5676d-image.png

                But this doesn't

                4e07513d-fe9d-48bc-94b4-207faf34910d-image.png

                Libre Wave - Freedom respecting instruments and effects
                My Patreon - HISE tutorials
                YouTube Channel - Public HISE tutorials

                clevername27C 1 Reply Last reply Reply Quote 1
                • clevername27C
                  clevername27 @d.healey
                  last edited by

                  @d-healey Thank you for taking the time to clarify all this - cheers.

                  1 Reply Last reply Reply Quote 0
                  • clevername27C clevername27 has marked this topic as solved on
                  • First post
                    Last post

                  14

                  Online

                  1.8k

                  Users

                  12.1k

                  Topics

                  104.9k

                  Posts