9 Replies Latest reply on Dec 10, 2012 9:25 PM by Brett Elliott

    Standardising my JavaScript

    Brett Elliott

      Hello everyone it's been a couple of years since my last post here but I have a question (I hope someone may have some advice they would be willing to share with me).

       

      The question is rather odd and isn't specific to a snippet of code. My boss has recently given me the task of standardising all my XML and JavaScripts which I have written over the past 8 years. The reason for this is my code does not follow a standard format is because I have been evolving my style as I have accumulated knowledge over time. Of course I can read my script without an issue myself and everything I have done works (well over 30,000 lines of finished code). However, as part of my position's redundancy measures, the staff we have tried to train to back me up (3 years now) cannot seem to grasp my work or create their own, hence the call for me to make all my scripts follow 1 easy to understand standard (my backup are graphic artists not programmers, but so am I).

       

      The one commonality with my scripts is they all use a InDesign Templates, XML Data Imports, and JavaScript to Format and Finish.

       

      My question is, how would you go about standardising your script if you were asked? Is there a process? Or, is this simply not worth the effort for very little gain? The programmers in our IT department tell me standardising script can be the hardest thing a programmer can do, is this correct?

       

      The daunting thing is once I figure out what the new standard is (this is where I'm a bit stuck), I'd possibly need a couple of years to re-write it all!

       

      I have been on Google for a week now and most answers seem to revolve around ‘Don’t do it!’

       

      Any advice on this problem would be greatly appreciated as I'd rather be as informed as possible before I make a descision one way or another.

       

      Regards,

      Brett

        • 1. Re: Standardising my JavaScript
          absqua Level 4

          My two cents:

           

          To standardize style, I use JSHint in the SublimeLinter plugin for Sublime Text 2. I found myself unable to settle on whitespace conventions, brace style, etc. without outside help. A lot of people have problems with the JSLint style, but I'm grateful to have some sort of standard to stick to.

           

          As far as more extensive refactoring of old, working code: this strikes me as worse than useless. I do this only as I need to make extensive modifications to the functionality or when I port it to a new version—in other words, when I would need to test it exhaustively anyway.

           

          Jeff

          1 person found this helpful
          • 2. Re: Standardising my JavaScript
            Vamitul Level 4

            sorry for the off-topic, but.. absqua.. THANK YOU!!

            i'we been looking for a good js editor, and just downloaded Sublime Text... it is awesome!!

            • 3. Re: Standardising my JavaScript
              absqua Level 4

              I love it but tend to only use it to clean up or refactor code. I religiously lint my files in it as my last step before committing, which helps prevent  a lot of meaningless whitespace noise in diffs, amongst other things. I tried to use it as my main editor but got tired of bouncing over to the ESTK for debugging.

              • 4. Re: Standardising my JavaScript
                Bob Stucky Adobe Employee

                FWIW, I keep a script file open in BOTH Sublime and ESTK.

                 

                Any time I save in Sublime, the ESTK religiously notifies me the file was changed and would I like to reload it. Switching back and forth for just for debugging doesn't feel onerous to me.

                • 5. Re: Standardising my JavaScript
                  Vamitul Level 4

                  hm.. since ESTK is scriptable (and i'm sure so is Sublime) there must be a easy/nice way of linking them. even nicer would be a sublime plugin for the Indesign DOM/debugging, but... i'll settle for what i can get

                  • 6. Re: Standardising my JavaScript
                    absqua Level 4

                    I did successfully use  the build system in Sublime to run an Applescript (or a shell command running an Applescript?) to  run a JavaScript in InDesign, but no stepping or breakpoints or console...

                     

                    I spent some time putting together a prefs file for JSHint in SublimeLinter that includes all the InDesign globals (or did in CS5). Let me know if you want it.

                    • 7. Re: Standardising my JavaScript
                      Vamitul Level 4

                      Please do!

                      yeeyy.. christmas!!

                      • 8. Re: Standardising my JavaScript
                        Brett Elliott Level 1

                        Thanks guys,

                         

                        I have no problem with the look and feel of the code as I am a neat freak and extensively comment as I go along. The refactoring is where I need to focus because when the XML and Templates change to a 'standard style of work' so does the functionality of the code. For example, if the descision was to use 1 table instead of a dozen floating textFrames (this would suit most of my products but not all) I would need to code for tables, columns and cells instead of mapping out co-ordinates and sizes of several textFrames in relation to each other etc. I have solutions for both tables and textFrames (coded very differently to each other) but I'm not sure what the 'best practice' is for stepping myself through the descision making process.

                         

                        As you said, I may be usesless and I would have to test exhaustively as it's basically starting from scratch...

                         

                         

                        Again, thanks for the help, it gives me more information to base my descision on. I just hope I can sell it!!!

                         

                        Brett

                        • 9. Re: Standardising my JavaScript
                          Brett Elliott Level 1

                          I finally had a breakthrough in this problem. I've made a spreadsheet of all my XML tags and cross referenced, rationalised, removed obsolete tags etc. This really helped me get the whole project into some sort of meaningful format or datamap.

                           

                          I then came up with a universal XML format with a set tag naming convention, tag order etc (it doesn't matter if a tag is populated or not if I'm not using it for the product I'm making because it will be ignored by InDesign).

                           

                          What I am in the process of doing now is a complete re-writing of 1 product to use this new XML, but I'm doing something a little different. I won't be using a pre-made template to build my product as I have in the past, I'll write everything in JavaScript including the creation of the document and all it's content, formatting, layout and resizing. This way I'll be able to rearrange the XML from within the document to suit the product I'm creating as it's being created. My hope is it will give me more flexibility at my end to make last minute changes without having to annoy our database developers and I also hope it will be easier for a JavaScript developer to understand without having to use InDesign's GUI should I need the backup. The other benefit is my current work collegues will be here during the whole process so they should be able to pick up how the entire process works as well.

                           

                          Anyway thanks for the help, it's been a interesting and very challenging process to map out what I need to do, but now the ball is rolling (sometimes I just need to talk things through even on a forum) I should be OK.

                           

                          Brett