Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. I hate JavaScript

I hate JavaScript

Scheduled Pinned Locked Moved The Lounge
javascriptlearningpythoncomsysadmin
22 Posts 15 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Marc Clifton

    You know you've posted this too many times when Chrome auto-completes the subject line for you! Anyways, I have this function: function initializePage(raceList, hlsList) ... (for demographics, race and Hispanic/Latino/Spanish, of course, a "I don't want to answer" is accepted) but anyways, Randomly, the list would not load. Really hard to reproduce locally, would happen once every 10 or 15 pages loads on the real site. Those lists, and others, are acquired from an Ajax call that only calls this function after all the lists have been loaded from the server. Of course it's a stupid way of doing it, but at the time... Plus this particular page used to only have those two lists, but then it grew to more things...you know how that story goes.. and of course what's worse is that this is the only page where I seem to have forgotten to refactor the code and do it "the right way." Anyways, in one place, I accidentally forgot to pass in the lists: initializePage() which resulted in the random "why isn't the list showing up" error. I blamed my SPA code, I blamed jqWidgets, I blamed my mother. And after staring at the code for hours over several weeks, I finally actually saw the offending line. God, I want parameter type checking. I know, use TypeScript or something. Marc

    Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

    R Offline
    R Offline
    rhyous
    wrote on last edited by
    #21

    JavaScript is a terrible language and you have plenty of justification to hate it. It is only used because it is the only option on browsers. If another existing language was suddenly supported by all browsers today, it would probably take over JavaScript quickly. JavaScript is so terrible, many developers prefer not to code in it. Many are creating new languages such as CoffeeScript, TypeScript, etc., that compile to JavaScript. Many are writing compilers for known languages (python, C#, etc.) that compile to JavaScript. Even the most popular JavaScript libraries (Angular) are written in TypeScript, not JavaScript, and those libraries recommend that we do the same.

    1 Reply Last reply
    0
    • M Marc Clifton

      You know you've posted this too many times when Chrome auto-completes the subject line for you! Anyways, I have this function: function initializePage(raceList, hlsList) ... (for demographics, race and Hispanic/Latino/Spanish, of course, a "I don't want to answer" is accepted) but anyways, Randomly, the list would not load. Really hard to reproduce locally, would happen once every 10 or 15 pages loads on the real site. Those lists, and others, are acquired from an Ajax call that only calls this function after all the lists have been loaded from the server. Of course it's a stupid way of doing it, but at the time... Plus this particular page used to only have those two lists, but then it grew to more things...you know how that story goes.. and of course what's worse is that this is the only page where I seem to have forgotten to refactor the code and do it "the right way." Anyways, in one place, I accidentally forgot to pass in the lists: initializePage() which resulted in the random "why isn't the list showing up" error. I blamed my SPA code, I blamed jqWidgets, I blamed my mother. And after staring at the code for hours over several weeks, I finally actually saw the offending line. God, I want parameter type checking. I know, use TypeScript or something. Marc

      Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

      R Offline
      R Offline
      Ryan Peden
      wrote on last edited by
      #22

      Although it won't make you hate JavaScript any less, you could use a countify factory function to make your existing functions pedantic about the number of arguments they receive:

      // adapted from args length code found on SO
      function argsLength(func) {
      return (func + '')
      .replace(/[/][/].*$/mg,'') // strip single-line comments
      .replace(/\s+/g, '') // strip white space
      .replace(/[/][*][^/(*)]*[*][/]/g, '') // strip multi-line comments
      .split('){', 1)[0].replace(/^[^(]*[(]/, '') // extract the parameters
      .replace(/=[^,]+/g, '') // strip any ES6 defaults
      .replace(/[\(\)]/g, '') //strip any remaining brackets
      .split(',').filter(Boolean) // split & filter [""]
      .length; //get length
      }

      // made up all by myself
      function countify(fn) {
      var expectedArgsCount = argsLength(fn);

      return function() {
      	var receivedArgsCount = arguments.length;
      	var expectedDescription = expectedArgsCount === 1 ? "argument" : "arguments";
      	var receivedDescription = receivedArgsCount === 1 ? "argument" : "arguments";
      	
      	if(arguments.length !== expectedArgsCount) {	
      		var errorMessage = \["Expected", 
      							expectedArgsCount.toString(),
      							expectedDescription + ",",
      							"received", 
      							receivedArgsCount.toString(), 
      							receivedDescription + "."\];
      							
      		throw new Error(errorMessage.join(" "));
      	} else {
      		fn.apply(this, arguments);
      	}
      }
      

      }

      Then, with initializePage, you could change its definition to

      var initializePage = countify(function(raceList, hlsList) {
      //your code here
      });

      but, since changing the function definitions of functions you want to countify is a pain, you could just do

      initializePage = countify(initializePage)

      anywhere after initializePage is defined. As a bonus, you can use this approach to countify library code as well, as long as you don't countify a function that does different things depending on the number of arguments it receives. Heck, with a few small modifications, you could make countify check argument types as well, by making it take two arguments: an array containing the types of the function arguments, and then the function itself. Adding the checks will of course add a small amount of overhead at run time, but it won't be noticeable at all unless your function is being called tens of thousands of times in a t

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups