cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar

Object-Oriented JSL - Examples?

Following on from the great talk Drew Foglia gave at discovery conference Europe (Object-Oriented JSL – Techniques for Writing Maintainable/Extendable JSL Code), I am trying to write my first object oriented JSL.


I am able to use the ObjectClass.jsl (from the core example files provided by Drew) to get and set properties to an object I have created.


My question is does anyone have some simple examples they could share to see ways this idea is implemented. For example how might the following example look as objects (considering in the real world I am likely to want to store details like table name, no. cols, column names, no. rows, label columns etc. for each table)?

NamesDefaultToHere(1);

dt1 = Open("$SAMPLE_DATA/Cars.jmp");

dt2 = Open("$SAMPLE_DATA/Cars 1993.JMP");

//join tables (assume some function was used to find the matching column names)

dt3 = Data Table( dt1 ) << Join(

  With( Data Table( dt2 ) ),

  Merge Same Name Columns,

  By Matching Columns( Column(dt1,1) = Column(dt2,1), Column(dt1,2) = Column(dt2,2) ),

  Drop multiples( 0, 0 ),

  Include Nonmatches( 0, 0 ),

  Preserve main table order( 1 )

);

//display result

win1 = dt3 << Graph Builder(

  Size( 534, 447 ),

  Show Control Panel( 0 ),

  Variables( X( :Size ), Y( :Name( "Wheel Base (inches)" ) ) ),

  Elements( Points( X, Y, Legend( 38 ) ), Box Plot( X, Y, Legend( 39 ) ) )

);

//tidy up

win1 << On Close(dt1 <<Close Window;dt2 <<Close Window; dt3 << Close Window(No Save) );

When would I decide to create a new class? The generic ObjectClass would work just fine for tables provided I can remember which property I have assigned to which key!

Thanks,

Stephen

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Object-Oriented JSL - Examples?

stephen.pearson​​,

I apologize for taking so long to respond. I'll give an example of a "class" definition, and its use, for the example you provide. It is important to consider if you would want/need this. As you pointed out, "when would you decide to make a new class?" As you may recall from my presentation, I suggested that project size, collaborative development, longevity, need for reuse, etc. should be considered before determining if OO constructs are the best approach. I will respond to msharp​'s comments with more on that topic.

Back to your example. Using the ObjectClass as a parent class, although not required, we could create a DataTableClass:

12599_Screen Shot 2016-08-24 at 11.26.50 AM.png

Usage of the fields for DataTableClass instances would look like:

12600_Screen Shot 2016-08-24 at 11.31.09 AM.png

For such a simple example, there would no reason to use the OO constructs. However, if you were to consider a case where you wanted to manage collections of tables, enable users and external systems to add comments and other attributes, pass these collections around, process and respond to given events, plan for future expansion and extension, establish a "contract" by which others can easily consume these tables, etc. Then, using OO constructs such as these are very beneficial.

Drew Foglia

View solution in original post

9 REPLIES 9
msharp
Super User (Alumni)

Re: Object-Oriented JSL - Examples?

Just my 2 cents:  Forget everything you learned in that talk.

JSL is NOT an object oriented language.  OOP is great for languages that don't have built in data structures, but it's far from the only way to program.  However, most programmers coming from a Java, C++, C# background can't get their heads around programming in non-OOP way.  In JMP data structures are best left to data tables.  This comes with benefits and draw backs.  In my opinion, you CAN hash together a Frankenstein object out of JSL data table, but then you're left with all the draw backs and none of the benefits.

What JSL IS good at is functional programming.  JSL supports functions AND expressions.  It can do everything you need cleanly using functional programming.  Check out this article for some pro's and cons comparing the two programming methods:  CodeNewbie

Phil_Brown
Super User (Alumni)

Re: Object-Oriented JSL - Examples?

stephen.pearson,

I'm so glad to see someone else interested in this topic. msharp is correct in saying that JSL is NOT an OOP language. But, in the recent past I've been able to realize "pseudo-OOP" constructs in JSL. I'm happy to share my work with you. I'll see if I can craft an appropriate response to the question you posed... Stand by

Phil

PDB
Phil_Brown
Super User (Alumni)

Re: Object-Oriented JSL - Examples?

stephen.pearson

I know drewfoglia​​ already answered your question, however, I was in the midst of crafting a response so I'll just put it forth for what its worth.

First, in my opinion the datatable itself can already be thought of as an object.

dt1 = NewTable() -    Creates new "instance" of Datatable

ShowProperties(dt1) -  Show all the "properties" and "methods" for the Datatable object dt1.

One can think about "extending" the existing Datatable Object with additional properties and methods, as in Drew's reply. I thought about having additional features like:

1. Rowstate Handler.

2. Column and Row, Add/Delete detection.

3. Table cell change history.

4. Databox (Displaybox)

So, a possible "constructor" for the DatatableObject could look like, where member variables and functions are declared. (note the Expr( New Namespace() ).

The idea behind declaring the object this way, is not only establish a replicable entity, but also allows one to clearly see the functions and expression names that will become usable once they are fully defined.

12610_Screen Shot 2016-08-25 at 12.32.23 PM.png

My version of the NewDatatableObject (partial listing). Here is where the member functions are actually defined:

12611_Screen Shot 2016-08-25 at 12.31.20 PM.png

   12621_Screen Shot 2016-08-25 at 1.17.43 PM.png

So for example, we create 2 separate instances of DatatableObject:

12612_Screen Shot 2016-08-25 at 12.47.23 PM.png

These tables now have "extended" properties built-in. In this example, some table actions are tracked, such as detecting row selection and Column/Row deletions.

12620_Screen Shot 2016-08-25 at 1.07.03 PM.png12613_Screen Shot 2016-08-25 at 1.06.06 PM.png

One can imagine a host of other useful functions as well, that could be implemented using this method.

PDB
msharp
Super User (Alumni)

Re: Object-Oriented JSL - Examples?

Don't get me wrong.  OOP is a great methodology that produces superb code, however, FP is the same.  If you're an experienced programmer, you'll be able to produce the quality of code needed in a FP way and writing pseudo-OOP is more an exercise in can it be done than should it be done.  If you're an intermediate programmer who only knows OOP so you want to code in a familiar way, I'd recommend learning FP as it will help you grow, think differently, and program better in many languages.  If you're an intermediate programmer who only knows FP and wants to learn OOP to broaden your horizons I would recommend starting by learning another language that actually has classes and objects implemented within the language.  If you're a novice programmer who really doesn't know anything, and you just heard OOP is cool, then take my advice, and forget about it for now.  Especially if you are going to be scripting mostly in JSL.

Personally, I would wait till JMP implements classes and objects, so true OOP can be realized.  I'm not sure if it's even on their radar, but I'm sure it will happen, I mean, we have classes and objects in JavaScript now.  You never know what the future will bring.

Re: Object-Oriented JSL - Examples?

stephen.pearson​​,

I apologize for taking so long to respond. I'll give an example of a "class" definition, and its use, for the example you provide. It is important to consider if you would want/need this. As you pointed out, "when would you decide to make a new class?" As you may recall from my presentation, I suggested that project size, collaborative development, longevity, need for reuse, etc. should be considered before determining if OO constructs are the best approach. I will respond to msharp​'s comments with more on that topic.

Back to your example. Using the ObjectClass as a parent class, although not required, we could create a DataTableClass:

12599_Screen Shot 2016-08-24 at 11.26.50 AM.png

Usage of the fields for DataTableClass instances would look like:

12600_Screen Shot 2016-08-24 at 11.31.09 AM.png

For such a simple example, there would no reason to use the OO constructs. However, if you were to consider a case where you wanted to manage collections of tables, enable users and external systems to add comments and other attributes, pass these collections around, process and respond to given events, plan for future expansion and extension, establish a "contract" by which others can easily consume these tables, etc. Then, using OO constructs such as these are very beneficial.

Drew Foglia
Phil_Brown
Super User (Alumni)

Re: Object-Oriented JSL - Examples?

drewfoglia

Forgive me if I missed this but where is "super()?" defined?

PDB

Re: Object-Oriented JSL - Examples?

Hello, Philip

It's very subtle. If you look at line 30 of the DataTableClass definition, you'll see this:

12606_Screen Shot 2016-08-25 at 8.58.17 AM.png

super is being defined as a local variable (scoped to the lifecycle of the function) and is being assigned the DEFINITION of the already existing deleteChildReferences function.

Read that again.

Let me say it another way. There is already a function, deleteChildReferences, on the namespace (defined in the ObjectClass.jsl that is included and gets assigned when the ObjectClass:newObject() call on line 6 executes). In the DataTableClass, line 29 is replacing that assignment. However, in the process of reassigning, we are creating a local variable "super" that is holding a reference to the original function definition - the one declared in ObjectClass. In this way, we are overriding the behavior of that function, adding code to close the table reference in this case, and then still able to call the original function declaration using the super() call. 

Drew Foglia

Re: Object-Oriented JSL - Examples?

drewfoglia​,

That was really helpful, thank you. My current project has non-linear interactions so I think this approach will more easily allow me to juggle the meta data I am collecting from the users.

vince_faller
Super User (Alumni)

Re: Object-Oriented JSL - Examples?

drewfoglia

First off, I love the detail you put into this.  If you don't mind me playing devil's advocate, why do you create the top namespace when the function returns a namespace?  Namespaces don't stack and in all but one of your examples, the only function in that namespace is an instancing function.  I just think the class should be instance-able  by name and everything should be inside of it so instead of ObjectClass:newObject().  I'm just curious your thought process. 

I've been mocking Object Oriented stuff for a while but your "this=namespace(expr(ns<<getname)" was just a brilliant way to make it self referential and something my stuff seriously lacked.  Just wanted to pick your brain a bit. 

Thanks,

Vince

Vince Faller - Predictum