Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Code Review / tips?

1618791197
Ernest P.
Pro
API Scripter
Howdy. I am looking for someone to take a look at a character importer I am writing for just general feedback. The API is functional (with known gaps that I am working on coding for), so what I am really looking for is "gotcha's" from the experience of other coders out there. Where possible I have cribbed code from The Aaron or other examples I could find and generally I try to comment verbosely so it should not be hard to discern what the code is doing. I hope to be submitting this for OneClick sometime by month end, so if there are other things I need to add on that front I would also be happy to hear about them. Code is here:&nbsp;<a href="https://github.com/eepjr24/ImportHS6e" rel="nofollow">https://github.com/eepjr24/ImportHS6e</a> Thanks in advance. - E
1619119645
timmaugh
Forum Champion
API Scripter
You're making progress! It is looking much more concise. A couple of small points... incorporate them or not as you see fit... Line 666 (and continuing) You split() on a single space, and then switch(){} on argument names like "--this". You'd probably gain some forgiveness by splitting on any number of white spaces, and even more forgiveness by splitting on whitespace followed by double-hyphens. I would also do it as a .forEach loop just because that isn't destructive to your args variable, in case you need it later. All of that would look like... &nbsp; let args = msg.content.split(/\s+--/).slice(1); &nbsp; args.forEach(a =&gt; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Loop all attributes &nbsp; switch (a) { &nbsp; &nbsp; &nbsp; case "help":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Show help in Chat &nbsp; &nbsp; &nbsp; &nbsp; // args.splice(0, 1); // &lt;== don't need this line any more (there was a typo in your code, here, too, just FYI) // run help code &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case "debug": &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// run debug code break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// ... } (Other places where you use a for() loop against an array I would probably just use a forEach(), too... but that's just a preference thing to save me typing longer names and brackets everywhere.) Lines 121 - 130 (createOrSetAttr) Your createOrSetAttr function doesn't actually return an object. There's nothing wrong with that, if you never need it, but it wouldn't hurt to return the object which you have located... at least as far as decomposing and reusing this function elsewhere. You could imagine a time when you would want to have the attribute you just created, and it costs you nothing to ignore the return. However, as you currently have it coded, where you aren't actually returning the attribute object, you need never declare it. Just wrap it in a viscous coating of gelatinous protection... also known as an OR case: const createOrSetAttr = (atnm, val, cid) =&gt; {&nbsp; &nbsp; &nbsp;// Set an individual attribute if it exists, otherwise create it. &nbsp; (findObjs({type: 'attribute', characterid: cid, name: atnm})[0] || createObj('attribute', {name: atnm, current: val, characterid: cid})).set('current', val); }; &nbsp;&nbsp;