4
\$\begingroup\$

I've stripped most of the code and comments from what follows.

My question is about the style that it is written in, not about the correctness of the code itself.

In particular, I've never seen anyone use this way of defining and initializing variables.

The idea is to keep all the definitions together, but to attach and isolate the initialization code for each variable. Ideally each section does nothing but initialize the named variable, with no side effects on anything else.

It works well for me, but want to know whether other people would find it too confusing.

"use strict"
if (typeof RB === 'undefined') var RB = {}
RB.tab = (function() {
   let tab_start = function(id, color1="#EFE", color2="#CDC", foldername="...") {
      let tabs = document.createElement("div"); {
         tabs.setAttribute("id", "tab_" + id)
      }
      let folder = document.getElementById(id); {
         folder.parentNode.insertBefore(tabs, folder)
         folder.appendChild(document.createElement("div"))
         folder.lastElementChild.innerHTML = "<span></span>" + foldername
      }
      let c1, c2; {
         let get_rgb = function(color) {
            let value; {
               let temp = document.createElement("div")
               document.body.appendChild(temp)
               temp.style.color = color
               value = window.getComputedStyle(temp).getPropertyValue("color")
               document.body.removeChild(temp)
            }
            return value.substring(4, value.length-1).replace(/ /g, '').split(',')
         }
         c1 = get_rgb(color1)
         c2 = get_rgb(color2)
      }
      ...
   }
   ...
   return function(arg) {// RB.tab([ [id, color, color],...,[id, color, color] ])
      let url = new URL(location.href)
      ...
      window.onresize()
   }
}())
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

As you are not using ; consistently I would say that your init style is dangerous as it will block automatic colon insertion.

For example the following lines will throw a syntax error SyntaxError: Unexpected token '{'

 var a = foo{ 
    // init stuff
 }

Or syntax error Missing initializer in destructuring declaration

 var a = 0,{
    // init stuff
 }

In a sea of code this type of typo is easily overlooked. Though it is a syntax error so will not lay in wait (in most normal situations)

When you allow the ; to be inserted by moving the { to a new line it is safer.

 var a = foo
 { 
    // init stuff
 }

however the concern is that you are doing more than simple assignment within the block which could lead to situation such as

 var a = {}
 {
     let a.bar = foo  // was meant to be a.bar = foo
 }

The let is a typo but will not get caught until the code is run.

If you do continue to use this style I would recommend you use semicolons and be very strict in regards to the extent of the setup code within the block.

Personally I think its ugly.

\$\endgroup\$
2
  • \$\begingroup\$ "you are not using ; consistently" ? I used ";" four times, each one in exactly the same way, so how is that not consistent? (And as you said, I could have eliminated that usage entirely by beginning the opening brace on a new line.) \$\endgroup\$ Commented Dec 16, 2019 at 19:07
  • \$\begingroup\$ @RayButterworth Yes you have used them only to prevent the syntax error, Any form of mixed use is inconsistent use. I will not answer "you must use semi colons" but not using them is very poor style. Remember that JS requires ";" (hence auto insert), Do you know every case it can fail? if not use them because missing ; may not generate an error until execution (not during parsing) and the error (if thrown) can manifest in an unrelated location. Not using them increases the odds of silent typo based sleeping bugs. \$\endgroup\$
    – Blindman67
    Commented Dec 16, 2019 at 19:42
2
\$\begingroup\$

I see what you're doing and it does group things nicely. However, I would avoid it simply because it's nonstandard. People will get confused.

You can achieve pretty much the same thing by using empty space to group things;

let tabs = document.createElement("div");
tabs.setAttribute("id", "tab_" + id);

let folder = document.getElementById(id);
folder.parentNode.insertBefore(tabs, folder)
folder.appendChild(document.createElement("div"))
folder.lastElementChild.innerHTML = "<span></span>" + foldername

or just splitting out functions

let folder = addFolder(id, folderName);
\$\endgroup\$

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.