Never mind, not even paying attention to my own co...
# suitescript
m
Never mind, not even paying attention to my own code....... 🤦 OK, I'm having an interesting issue with my code and some calculations. I know that there can be some issues with certain floating point numbers, but has anyone dealt with a scenario where the exact same equation, run twice produces two different results? Example below:
Copy code
const val1 = 172.5 + Math.ceil((445200 - 100000) / 100) * 100 / 1000 * 1.5
const val2 = 172.5 + Math.ceil((445000 - 100000) / 100) * 100 / 1000 * 1.5

console.log(val1)
console.log(val2)

Results:
PS C:\Users\mcarter\Documents\VsCode\netsuite investigate> node .\test2.js
690.3
690
m
They're not the same calc. You have 445200 in the first, and 445000 in the second.
this 2
m
šŸ˜‚ Well, that will do it........
šŸ™‚ 1
s
this is also where I suggest you consider BigNumber (which is already there if you're using NFT)
a
roundByExponent.js
@Matt Carter Without using NFT or big numbers or type script, you can do proper rounding in JavaScript with this small function:
s
If you insist on an arrow function there, you can simplify it even further by dropping the curly braces and the
return
statement as it's implied.
a
I'm very insistent with coding standards too, in NetSuite the odds are that you will have a mix of 2.0 and 2.1 at least, so I don't like the idea of getting rid of `{}`(never like it without this) or
return
for code readability and maintenance.
s
umm, in that case you shouldn't be using arrow functions at all. You should be using
function
statements.
ā˜ļø 1
unless 2.0 supported arrow functions? I thought it didn't.
worth noting that if you DO use TypeScript, you can have the same source code for either SS 2.0 or 2.1 - merely changing the transpilation target. For us, there is no source code changes when switching to 2.1 from 2.0.
So we TypeScript users have enjoyed judicious use of arrow functions for many years now šŸ™‚
a
I'm failing to see why I should do that. In my mind and others like: • https://developer.wordpress.org/coding-standards/wordpress-coding-standards/javascript/ • https://contribute.jquery.org/style-guide/js/ • https://google.github.io/styleguide/jsguide.html And finally: • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions Using arrow functions
=>
with
{}
and
return
is perfectly valid syntax, and you sort of keep a consistent coding standard with your old code base without having to use the old
function
initialization. I use this also as a way to identify and/or isolate 2.1 code from 2.0 code without having to scroll up to check. I take advantage of everything related to ES6 in 2.1, with the only exception of keeping
{}
and
return
again for code readability purposes.
I guess we just have different perspectives about what good code looks like: If I interview two developers and ask them to write a Hello World line and I get: • Developer One:
console.log("Hello, world! " + 123);
• Developer Two: console["\x6C\x6F\x67"]("\x48\x65\x6C\x6C\x6F\x2C\x20\x77\x6F\x72\x6C\x64\x21\x20"+ 123) I'm hiring Developer One without a doubt, and I couldn't care less how smart Developer Two thinks he is.
s
You mentioned wanting consistency between 2.0 and 2.1. It's my understanding that arrow functions aren't supported in 2.0 so your example code using them would utterly fail in 2.0. Considering clarity, I can't see how
const roundByExponent = (pValue, pDecimals) => {
is at all easier to read/understand than
function roundByExponent (pValue, pDecimals) {
at least with the
function
keyword it's clear you're talking about a function definition - with the other syntax you're assigning a variable who's value happens to be a function.
So for clarity and ease of understanding (across 2.0 and 2.1 šŸ™‚ ) we use
function
statements for functions, with the notable exception of predicate/iteratee type functions which are often clearest as one-line arrow functions. e.g.
_.filter(so.item, line => line.quantity > 10)
nice chat though
a
The
{}
and
return
are not the only differences between
function
and
=>
, yes I do want to keep consistency
as much as I can
between 2.0 and 2.1 without sacrificing what I get with
=>
as restrictions of
this
, and other things. Nice chat!...
s
agreed, treatment of
this
is important, but I rarely see SuteScript code in the wild actually referring to
this
or making assumptions about it. I've asked on this forum a couple times and it seems
this
is mostly avoided by SuiteScript devs (probably for the best, most of the time)