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
hogi
Level XII

JSL: if - else -> find the commas?

Hi,

 

I often face the problem that a code doesn't work because 1 out of 10 semicolons is a comma or one comma which belongs to the if block is actually missing.

Is there an easy trick to make the blocks in an if-else clause more visible?

 

16 REPLIES 16
jthi
Super User

Re: JSL: if - else -> find the commas?

Not to my knowledge. Currently the few changes you can are found from preferences Script Editor page.

 

Also writing customizable auto-formatter isn't the most simple task (at least I would think so) and I'm not even sure if it is a good idea. I think opinionated auto-formatters are more common and most likely better than fully customizable ones. But JSL has no style guides to follow, so there is no common opinion to base the auto-formatter on (JSL is very small language).

 

Here is a video regarding black auto-formatter for Python Łukasz Langa - Life Is Better Painted Black, or: How to Stop Worrying and Embrace Auto-Formatting (... 

-Jarmo
"Speaker: Łukasz Langa What good is a code style if it's not internally consistent? What good is a linter when it slows you down? What if you could out-source your worries about code formatting, adopt a consistent style, and make your team faster all at the same time? Come hear about Black: a new
David_Burnham
Super User (Alumni)

Re: JSL: if - else -> find the commas?

I agree with @jthi , I avoid using re-format.  It's my job as a programmer to have a clear style guide and enforce it as I write the code.

Annotations such as 

, // else

can help give a visual guide when there are a large number of lines of code.  

I too, think aligning the comma with the main if statement is the preferable style.

 

-Dave
ron_horne
Super User (Alumni)

Re: JSL: if - else -> find the commas?

Hi @hogi ,


This is not an exact answer to your specific question. But i think this jsl extension for visual studio provides a better scripting experience in general.

 

https://marketplace.visualstudio.com/items?itemName=VinceFaller.jmp-scripting

 

 

hogi
Level XII

Re: JSL: if - else -> find the commas?

Oh no - once again, after some copy & paste and re-arrangement of my code it behaves as if there was a wrong comma somewhere in an if statement

 - and I wished there was a highlight function implemented already in Jmp17

Hm, not yet ...

 

So I went back to the suggestions in the posts and searched for some help.

While reading the suggestions the first time I thought: sounds good, many of them will help me when I have the next problem.

 

Now, when searching for the erroneously placed comma, actually it was @txnelson's  suggestion which helped me to find it:
Click on reformat script and check where the indentation doesn't fit to the expected one.

Quite simple - and as expected.


On the other hand, while searching again and again for the wrong comma, the other suggestions read a bit like

"there is a comma where it should not be? you have to find it and correct it".

 

To combine both approaches ,to write your code

1) in a readable way
2)  such that reformat script doesn't change the formatting.

One could put an empty line in front of the commas in the if statements. This will prevent reformat script from moving the commas to the end of the previous line. Unfortunately, it will also shift the code after the commas to the next line, just leaving the commas there - which makes the code a bit "wide-meshed" and difficult to read. Hm.

Names Default To Here( 1 );
dt = Current Data Table();
If(
	Is Empty( dt ),
		Print( "no table found" );
		
,
	N Rows( dt ) == 0,
		Print( "table has no data" );
		
, // else
	Print( "ok" )
);

 

Try on your own: Wherer is the erroneously placed comma
code as it was provided by ErraticAttack, but with a comma where there should be a semicolon:

Names Default to Here( 1 );
dt = Current Data Table();

Try(
	If( Contains( dt << Get Column Names, As Name( "height" ) ),
		mean = Col Mean( dt:height );
		max = Col Max( dt:height );
		min = Col Min( dt:height );
		std dev = Col Std Dev( dt:height ),
		Write( "\!NSome stats on :HEIGHT:",
			"\!NMin: ", min,
			"\!NMax: ", max,
			"\!NMean: ", mean,
			"\!NStd Dev: ", std dev
		)
	,
		Contains( dt << Get Column Names, As Name( "weight" ) ),
		mean = Col Mean( dt:weight );
		max = Col Max( dt:weight );
		min = Col Min( dt:weight );
		std dev = Col Std Dev( dt:weight );
		Write( "\!NSome stats on :WEIGHT:",
			"\!NMin: ", min,
			"\!NMax: ", max,
			"\!NMean: ", mean,
			"\!NStd Dev: ", std dev
		)
	,
		Contains( dt << Get Column Names, As Name( "age" ) ),
		mean = Col Mean( dt:age );
		max = Col Max( dt:age );
		min = Col Min( dt:age );
		std dev = Col Std Dev( dt:age );
		Write( "\!NSome stats on :AGE:",
			"\!NMin: ", min,
			"\!NMax: ", max,
			"\!NMean: ", mean,
			"\!NStd Dev: ", std dev
		)
	,
		Print( "Cannot find columns 'height', 'weight', or 'age'" )
	)
,
	Print( "mistakes were made" )
)

 

View more...
after reformat script:
-  the wrong comma is easily visible because indentation changed completely.
-  (even without a wrong comma) all commas are moved to the end of the previous line -  which makes it more difficult to analyze the structure

Names Default To Here( 1 );
dt = Current Data Table();

Try(
	If(
		Contains( dt << Get Column Names, As Name( "height" ) ),
			mean = Col Mean( dt:height );
			max = Col Max( dt:height );
			min = Col Min( dt:height );
			std dev = Col Std Dev( dt:height );, // <- wrong comma
		Write( //<- "wrong" indentation gives the hint
			"\!NSome stats on :HEIGHT:",
			"\!NMin: ",
			min,
			"\!NMax: ",
			max,
			"\!NMean: ",
			mean,
			"\!NStd Dev: ",
			std dev
		), Contains( dt << Get Column Names, As Name( "weight" ) ),
		mean = Col Mean( dt:weight );
		max = Col Max( dt:weight );
		min = Col Min( dt:weight );
		std dev = Col Std Dev( dt:weight );
		Write(
			"\!NSome stats on :WEIGHT:",
			"\!NMin: ",
			min,
			"\!NMax: ",
			max,
			"\!NMean: ",
			mean,
			"\!NStd Dev: ",
			std dev
		);, Contains( dt << Get Column Names, As Name( "age" ) ),
		mean = Col Mean( dt:age );
		max = Col Max( dt:age );
		min = Col Min( dt:age );
		std dev = Col Std Dev( dt:age );
		Write(
			"\!NSome stats on :AGE:",
			"\!NMin: ",
			min,
			"\!NMax: ",
			max,
			"\!NMean: ",
			mean,
			"\!NStd Dev: ",
			std dev
		);, Print( "Cannot find columns 'height', 'weight', or 'age'" )
	)
,
	Print( "mistakes were made" )
);
Jeff_Perkinson
Community Manager Community Manager

Re: JSL: if - else -> find the commas?

 Pasting the If() statement into the Formula editor helps spot it pretty quickly. 

2022-12-04_15-54-03.773.png

-Jeff
hogi
Level XII

Re: JSL: if - else -> find the commas?

cool, very convenient

hogi
Level XII

Re: JSL: if - else -> find the commas?

a nice example what can go wrong:

x= if(
// 1st test
1==3, 1 , // 2nd test 1==5, 2, , // others 3 )

after executing the code, x= ...

View more...
hint:
hogi_0-1711130931064.png


... no error message?