Recently I have been asked about my methods for creating dummy data, coding styles, coding habits, how to …. well, just about every aspect of how I like to code in vRO. In fact, how I like to code in vRO is how I like to code in multiple languages and for projects that don’t reside in the VMware or virtualisation ecosystem that I work on. Rather than blab on about all the things you’ve heard before (Agile working, LEAN etc), I thought I would write a really short, simple, consumable blog post that hopefully gives some insight into my [editor: twisted?] mind and ways of working.

When developing, K.I.S.S (or refactor, refactor, refactor..)

Yes, when developing code, keep it simple. Something fantastic about coding is that you are continually learning. Don’t get emotionally attached to the code you’ve just written. Go back over it and simplify it, make it easier to read, easier to consume. The biggest benefit you can give to yourself and your peers (assuming you share your code) is to make it easy to consume and reusable. Don’t think about writing code to solve the exact issue you have; try and think about writing code that solves your problem of course, but try and think about it in such a way that it can be used for similar problems – take a generic approach.

Comment your code

I have worked on projects where we have had GBs of code and hundreds of commits a month. Imagine working in that environment, with no code comments. Why has this been done? Why is this backwards? Why even do this? *sigh* so many times I have asked myself these things. But if we code clearly and work in a pragmatic structure, we don’t need documentation or code comments. The code just explains itself, right? Wrong! Coding is a personal thing, we all have our ways and means. Please, comment your code people!

Don’t be afraid to output

Please do fill my debug console [editor: and just the debug console, please..] with row after row of output, showing me what is going on with the various objects and properties being managed by your code. If you write the code, show its output path and lifecycle through outputting to a debug window or log. When something goes bang and you need to debug, it is nice to have somewhere to start after all.

Commit and comment

Don’t commit once a day, 400 lines of code changed and then wonder what’s broken. Please, use source control to store your code. Batch up your work into small pieces and commit often. It simplifies bug fixing later on, makes it easier to review code changes and track where you are and what you working on. Source control is your friend. Treat it with respect and use it properly and you will be well rewarded.

Readability

Let’s have a look at the following code example. We want twenty example vm objects to test a method on. A lot of people would construct like this.

var vmObject1 = {};
vmObject1.name: "small vm 1";
vmObject1.memoryGB: 4096;
vmObject1.cpuCount: 2;
vmObject1.cpuCoreCount: 4;
vmObject1.osType: "Windows";

var vmObject2 = {};
vmObject2.name: "small vm 1";
vmObject2.memoryGB: 4096;
vmObject2.cpuCount: 2;
vmObject2.cpuCoreCount: 4;
vmObject2.osType: "Windows";

var vmObject3 = {};
vmObject3.name: "small vm 1";
vmObject3.memoryGB: 4096;
vmObject3.cpuCount: 2;
vmObject3.cpuCoreCount: 4;
vmObject3.osType: "Windows";

var vmObject4 = {};
vmObject4.name: "small vm 1";
vmObject4.memoryGB: 4096;
vmObject4.cpuCount: 2;
vmObject4.cpuCoreCount: 4;
vmObject4.osType: "Windows";

var vmObject5 = {};
vmObject5.name: "small vm 1";
vmObject5.memoryGB: 4096;
vmObject5.cpuCount: 2;
vmObject5.cpuCoreCount: 4;
vmObject5.osType: "Windows";

That is just 5 entries, imagine 20! And then imagine you want some varying values. Here is an alternative way of doing this.

var vmObjects = [];
var count = 1;
while (count < 20) {
    vmObject = {
    name: "small vm " + count,
    memoryGB: randomMemoryMB(),
    cpuCount: randomEvenNumber(1,16),
    cpuCoreCount: randomEvenNumber(1,8),
    osType: pickOsType(count)
    }

    vmObjects.push(vmObject);
    count++;
}

Ok, so, obviously there are some function calls in there, but, this is going to create 20 vmObjects and store them in an array. Look how readable that code is compared to the previous example.

Obviously it will look completely crazy when we add the functions in though right?

var vmObjects = [];
var count = 1;

while (count < 20) {
    vmObject = {
    name: "small vm " + count,
    memoryGB: randomMemoryMB(),
    cpuCount: randomEvenNumber(1,16),
    cpuCoreCount: randomEvenNumber(1,8),
    osType: pickOsType(count)
    }

    vmObjects.push(vmObject);
    count++;
}

// ****** Helper functions to create dummy data ******
function pickOsType(num) {
    if (num % 2 == 0) {
        return "Windows Server 2019";
    }
    return "Linux RHEL 7";
}

function randomEvenNumber(min,max) { 
    var complete = false;
    var num = 0;
    while (complete == false) {
        var num = Math.floor(Math.random() * (max - min) + min);
        if (num % 2 == 0) {
            complete = true;
        }
    }
    return num;
} 

function randomMemoryMB() { 
    var num = Math.floor(Math.random() * (4 - 1) + 1);
    switch (num) {
        case 1:
          return 1024;
        case 2:
          return 2048
        case 3:
          return 8192
        case 4:
          return 16384;
    }
}

Nope! It really doesn’t. In fact, you can easily follow through what is there, how the objects are being constructed and you can see how easy it is to adapt so you can have more varied fake data in your objects. What’s more, the functions can be re-used across multiple use cases. This approach to modular coding and making use of functions and reusing code is all considered good practice in the word of software development and programming.

Don’t be the dinosaur

There is nothing wrong with doing what you have always done, if it still works. But, have you considered reading the release notes? Seeing what changes have been introduced that may streamline some of your code?

In our example below, we have created an array of 20 vmObjects, all with different data. How could we get all of the values out for each object? Iterate over using a for or for each loop?

 

Yes, you could indeed! However, what about this?

for each (var entry in vmObjects) {
    var values = Object.keys(entry).map(function(k){return entry[k]}).join(",");
    System.debug(values);
}

This will output all of the values from each entry in our array of vmObjects. You see, new syntax and abilities introduced in later product versions, really can make your life easier! All you need to do is stay up to date. Check out the new functionality, review it, try it and rate it against what you have already been doing. If it is an improvement and streamlines your workflow, embrace it. If it doesn’t work for you? Don’t sweat it, just move on.

Peer review

Lastly, don’t be afraid to ask a peer or someone in the relevant community for help. No one has seen it all! If you ask for help you may even end up helping someone else learn something as you may problem solve together, teach them a thing or to about your coding habits and style or just learn something yourself. There is no negative in asking for help!

 

 

A simple example of runnable code, referenced throughout this post can be seen below.

var vmObjects = [];
var count = 1;
while (count < 20) {
      vmObject = {
        name: "small vm " + count,
        memoryGB: randomMemoryMB(),
        cpuCount: randomEvenNumber(1,16),
        cpuCoreCount: randomEvenNumber(1,8),
        osType: pickOsType(count)
      }
    vmObjects.push(vmObject);
    count++;
}

System.debug("Show me a csv layout");
var keys = Object.keys(vmObject).join(",");
System.debug(keys);

for each (var entry in vmObjects) {
    var values = Object.keys(entry).map(function(k){return entry[k]}).join(",");
    System.debug(values);
}

System.debug("");
System.debug("Show me a json layout");
System.debug(JSON.stringify(vmObjects, null, 2));


// ****** Helper functions to create dummy data ******
function pickOsType(num) {
    if (num % 2 == 0) {
        return "Windows Server 2019";
    }
    return "Linux RHEL 7";
}

function randomEvenNumber(min,max) { 
    var complete = false;
    var num = 0;
    while (complete == false) {
        var num = Math.floor(Math.random() * (max - min) + min);
        if (num % 2 == 0) {
            complete = true;
        }
    }
    return num;
} 

function randomMemoryMB() { 
    var num = Math.floor(Math.random() * (4 - 1) + 1);
    switch (num) {
        case 1:
          return 1024;
        case 2:
          return 2048
        case 3:
          return 8192
        case 4:
          return 16384;
    }
}

paul_davey

CIO at Sonar, Automation Practice Lead at Xtravirt and guitarist in The Waders. Loves IT, automation, programming, music

%d bloggers like this: