Created:
Nov 24, 2022 03:34 PM
| Last Modified: Sep 13, 2023 1:08 PM(3816 views)
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?
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).
"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
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.
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 suggestionwhich 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" )
)
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" )
);