0
\$\begingroup\$

This is what I am doing and it's working:

var isEmpty = true;
for(let i = 0; i< members.length; i++)
{   
    var member = members[i];
    if(member && member[3]){
        isEmpty = false;
        break;
    }   
}

if(isEmpty) {
    var somePrefix = "123 -"
    for(let i = 0; i< members.length; i++)
    {   
        var member = members[i];
        if(member && member[2]){
            member[3] = somePrefix + i;
        }   
    }
}

Can this be improved? I'm simply checking if a specific property is empty for all of the collection of multi dimensional array.

Here is what's happening:

  1. Checking X property is empty for all members
  2. If it is, then auto populate it
    • If any of the property is filled in then leave it as is
\$\endgroup\$
  • \$\begingroup\$ Can I just point out that your "style" is very hard to read/uncommon. \$\endgroup\$ – Vera Perrone Mar 20 at 13:09
  • 1
    \$\begingroup\$ @VeraPerrone show me how you would improve it, without that your comment isn't of much help \$\endgroup\$ – Mathematics Mar 20 at 15:07
1
\$\begingroup\$

Style

Some points on your style

  • Don't forget to close the line with semicolons where appropriate.
  • Use const for variables that do not change.
  • Use let for variables that are scoped to the block.
  • Use for of loops when you do not need the index.
  • In JS the opening block delimiter { is on the same line as the associated statement. eg for(...) { // << { on same line
  • Even if its an example, always write code as functions, not as flat bits of code as in your example. The function name adds additional contextual semantic meaning and forces you to write in a style more akin to real world needs.

  • Use a space between

    • tokens and (, eg for (, if (foo) {.
    • operators and operands, eg 1 + 2, i ++
    • commas and expressions, eg a, b (not that it applies in your example)

Typescript?

You have the question tagged typescript, yet the code is pure JS. If you wanted a typescript version you have not indicated, and personally apart from better IDE integration the additional complexity of typescript does not offer any benefit over well written JS.

Rewrites

With all things equal the best quality source code has the shortest line count.

Cleaning up of your code

Note that because the code is written as a function I have not needed to use an intermediate to hold the abstract "empty" state of members, removing 4 lines of code.

function populateIfMembersEmpty(members, prefix = "123 -") {
    for (const member of members) {   
        if (member && member[3]) { return }   
    }
    for (let i = 0; i< members.length; i++) {   
        if (members[i] && members[i][2]) { members[i][3] = prefix + i }   
    }
}

Alternatives

function populateIfMembersEmpty(members, prefix = "123 -") {
    if (! members.some(member => member && member[3])) {
        members.forEach((member, i) => {
            if (member && member[2]) { member[3] = prefix + i }   
        });
    }
}

or

Does a member of members on the same line need to be named, or is a symbolic representation any less meaningful?

function populateIfMembersEmpty(members, prefix = "123 -") {
    if (! members.some(m => m && m[3])) {
        members.forEach((m, i) => m && m[2] && (m[3] = prefix + i));   
    }
}

or

function populateIfMembersEmpty(members, prefix = "123 -") {
    if (members.some(m => m && m[3])) { return }
    members.forEach((m, i) => m && m[2] && (m[3] = prefix + i));   
}

19 lines down to 4 may sound pedantic, but apply the same to a large code base and a monster source file of 10,000 lines can be a far more manageable 2,000 reducing the odds of a bug by 80%


As the only response you have so far got that has unnecessarily emphasized a confusing point, I will say your style is neither "very" hard (or hard) to read, nor is it uncommon. (Let alone "very" uncommon, meaning... unique? no! 🙄).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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