Through the lens of the SOLID principles, which one of the following makes the most sense? I want to ensure the approach I select keeps everything simple and maintainable as we add properties, modify or replace functions, and scale. These objects may contain up to thirty properties or more and each one may require a value which changes.

Let's say I have a JSON object and I want to dynamically add values to it.

The scenarios below all technically work but I'm not sure of what the "best practice" would be when dynamically adding items in a JSON object, especially as it grows in the number of properties.

Scenario A) - Operations in a variable.

var added = 3 + 4;
var subtracted = 1 + 2;

var testObj = {
    key1: added,
    key2: subtracted

Scenario B) - Operations in a function.

var b = 1, c = 2, e = 3, f = 4;    

function Added(x, y) ( return x + y }
function Subtracted(a, z) ( return a - z }

var testObj = {
    key1: Added(b, c)
    key2: Subtracted(e, f)

Scenario C) - Operations in the JSON properties.

var a = 1, b = 2, c = 3, d = 4;
var testObj = {
    key1: a + b
    key2: c - d

Is there another approach which I haven't yet considered?

  • 1
    \$\begingroup\$ With the info you've given, I couldn't say... each of solution you've written could be valid in different situation \$\endgroup\$ Commented Jan 12, 2018 at 17:44

1 Answer 1


Stupid nitpick..

There's no such thing as a JSON object in Javascript, they're just objects in Javascript. JSON stands for JavaScript Object Notation - Talking about JSON objects in the context of Javascript is like saying "strawberry flavored strawberry ice-cream which is strawberry flavored." Sorry. </rant>

Best option

If this is a realistic representation of your objective then the third one is the best, fastest and most concise. Creating a function to add two numbers is a waste of space and memory. Creating a variable is also a waste of memory as it will just have to get garbage collected.

Approaches you aven't considered

class add2Numbers{
        this.addend = 0;
        this.augend = 0;
        this.result = 0;

        return parseFloat(n); 
        this.addend = this.parseNum(n);
        this.augend = this.parseNum(n);
        this.result = this.addend + this.augend;
        return this.result;

function insertProperty(obj, key, value){
    obj[key] = value;

var o = {};
var adder = new add2Numbers();
var result = adder.getResult();
insertProperty(o, 'sum', result);

// o now contains a property called 'sum' which contains the
// sum of the numbers 2 and 5


The point that I'm trying to make is that new programmers and even old ones for some reason spend way too much time worrying about stupid buzzwords that they completely forget about common sense. The only buzzword you need right now is K.I.S.S.

Good luck and godspeed.

  • \$\begingroup\$ Speed isn't something I care about here. Thank you for your answer though. \$\endgroup\$ Commented Jan 11, 2018 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.