HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Random generator extension for VSCode

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
randomextensiongeneratorvscodefor

Problem

Background

A VSCode extension that uses Chance.JS to generate random data types.

Concerns

  • My main concern is that there are 4 levels of function references that feels like a hack. From registerCommand → insertFunc → randomFunc → processSelection.



  • If the code is sensible TypeScript/JavaScript



The Code

```
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('randomeverything.int', insertRandomInt));
context.subscriptions.push(vscode.commands.registerCommand('randomeverything.letters', insertRandomLetters));
context.subscriptions.push(vscode.commands.registerCommand('randomeverything.lettersAndNumbers', insertRandomLettersAndNumbers));
context.subscriptions.push(vscode.commands.registerCommand('randomeverything.iPv4Address', insertRandomIPv4Address));
context.subscriptions.push(vscode.commands.registerCommand('randomeverything.iPV6Address', insertRandomIPV6Address));
/ More commands... /
}

function insertRandomInt(): void {

var max:number;
var min:number;

Window.showInputBox({prompt: "Please enter [MIN-MAX]", value:"1-100"}).then(
function(txt){
if(txt){
var args = txt.split("-");

min = Number.parseInt(args[0]);
max = Number.parseInt(args[1]);

if(args.length != 2 || isNaN(min) || isNaN(max))
{
Window.showErrorMessage("Invalid format.");
return;
}
processSelection(randomIntString, [min, max]);
}
});

}

function insertRandomLetters(): void {
processSelection(randomLetters, []);
}

function insertRandomLettersAndNumbers(): void {
processSelection(randomLettersAndNumbers, []);
}

function insertRandomIPv4Address(): void {
processSelection(randomIP, ['ipv4']);
}

function insertRandomIPV6Address(): void {
processSelection(randomIP, ['ipv6']);
}
/* More

Solution

As for the architecture it seems good to me.

As for the TypeScript I have a few minor suggestions:

Avoid using var, use const or let instead.

function randomIntString(min: number, max: number): string {
    const chance = require('chance').Chance();
    const randomVar: number = chance.integer({
      min: min,
      max: max
    });

    return randomVar.toString();
}


It is also preferable to use let for for-cycle control variables.

for (let x = 0; x < sel.length; x++) {
   // body
}


It would help readability and maintainability if you specified the types of all the arguments of all your functions.

function randomIntString(min: number, max: number): string {
    // body
}


Also prefer using lower case type names for built-in primitive types (e. g. number over Number) when specifying the type of something.

You could specify the IP option type more precisely (asuming you use TypeScript >= 2.0). This will help you prevent typos and also improves readability (the code is more self-documenting):

type IpAddressType = 'ipv4' | 'ipv6';
function randomIP(option?: IpAddressType): string{
    // body stays the same
}


When the property of the object being initialized has the same name as the variable assigned to it you can shorten it like so (see the chance.integer call):

var randomVar: number = chance.integer({
      min,
      max
    });

Code Snippets

function randomIntString(min: number, max: number): string {
    const chance = require('chance').Chance();
    const randomVar: number = chance.integer({
      min: min,
      max: max
    });

    return randomVar.toString();
}
for (let x = 0; x < sel.length; x++) {
   // body
}
function randomIntString(min: number, max: number): string {
    // body
}
type IpAddressType = 'ipv4' | 'ipv6';
function randomIP(option?: IpAddressType): string{
    // body stays the same
}
var randomVar: number = chance.integer({
      min,
      max
    });

Context

StackExchange Code Review Q#151239, answer score: 4

Revisions (0)

No revisions yet.