Select to view content in your preferred language

ArcGIS JavaScript programming, single responsibility, singletons and best practices?

2523
13
Jump to solution
06-18-2013 09:02 AM
YgorThomaz
Emerging Contributor
Hello guys,

I started my programming in two days, my "main.js" file starts getting gigantic and hard to understand.

Do you know any documentation that can help me with the Javascript programming with Dojo and ArcGIS API?
I believe that to minimize the problem will need to singleton for the object map. Questions:

- How do you guys organize your files (.JS)? Separate files for subsequent "compilation"?
- Do you use MVVM?
- Do you use the single responsibility principle for the scope of require([]) (Dojo)?

Thanks, any opnion is welcome!
13 Replies
JasonScharf
Deactivated User
Dynamic typing is one of the core concepts of JavaScript, which is powerful. Which one is better is arguable. But since JavaScript has dynamic typing nature, I'm not sure if adding a typescript upon it makes a lot of sense. I haven't compared the performance of typescript. But JavaScript normally has the reputation of "SLOW", you may not want to sacrifice more just for type safe. Once you get used to dynamic typing, you will find it's a very flexible and interesting feature.


TypeScript compiles into pure JavaScript and there's no perfomance hit that I know of. If anything, it's skewed towards better performance and encapsulation. TypeScript produces JavaScript that is essentially what an excellent JavaScript developer would produce when working with namespaces, modules, and "classes".

The dynamic versus static typing argument is an old one. I can't imagine a reason not to want compile-time checking, code completion, etc.

One of the beautiful things about TypeScript is that has strong type inference. Example:

// JavaScript:
var thing = "a b c";

thing = 5; // Totally fine!

var pieces = thing.split(" "); // Run time error: thing.split is not function


// TypeScript:
var thing = "a b c";

thing = 5; // Compile time error: Type of "thing" is string, not number!

var pieces = thing.split(" ");


Strong type inference with TS makes porting existing codebases easy. You get compile-time checking, code completion, static analysis, and all the good things that come with these. The only thing that really changes in TS are namespace and class declarations, as well as method signatures. They gain type annotations.

TypeScript also supports what are known as "definition files". These are a lot like C headers. They define objects and types and method signatures, often belonging to libraries outside of your control. We use them when coding against dojo, the ArcGIS JavaScript API, and jQuery. They are simply code contracts. They tell us what's available, and how to create objects and call methods upon them without making mistakes and wasting time.
0 Kudos
JianHuang
Deactivated User
Yes, dynamic typing versus static typing is an old argument starting since LISP, and it's still an argument. Here is a good discussion brought up by Douglas Crockford:
https://plus.google.com/118095276221607585885/posts/MgzNUSTwjRt
In my opinion, I think it's really a personal preference, and I want to write and maintain real javascript code directly so that I have control over everything. That doesn't mean I refuse the help of tools such as JQUERY or DOJO. Translating from another "language" is not my favorite option even though the result seems not unreadable.
Intellisense might be a big plus for many people. Again, it's a personal reference. It's a nice to have feature, especially for programmers whose primary language is not JavaScript.
0 Kudos
MatthewWeyland
New Contributor

TypeScript also supports what are known as "definition files". These are a lot like C headers. They define objects and types and method signatures, often belonging to libraries outside of your control. We use them when coding against dojo, the ArcGIS JavaScript API, and jQuery. They are simply code contracts. They tell us what's available, and how to create objects and call methods upon them without making mistakes and wasting time.


Care to share your d.ts file?

Is this something that ArcGIS would contribute to the DefinitelyTyped project on GitHub

We're looking at this and want to see how this works with Knockout and how well it plays with Tyepscript.  

We've been working quite extensively with the Google Maps API and currently have about a 4k LOC rich UI tool for mapping, demographic data, geographies etc.  I've encapsulated my interface with the google maps API to only a few objects so I'm hoping making the swap out won't be too bad if I can make them all play together well.

Thanks.
0 Kudos