Solved

eloquent javascript

Posted on 2013-06-03
3
223 Views
Last Modified: 2013-06-06
What is the best (i.e. most elegant, concise, readable, beautiful, meaningful, expressive, 31337, etc) way of doing error checking on a long string of functions where each one depends on the one before it?

For example, I have a function called findShift(id) which returns an object if the object isn't found, it returns null. I want to access one of the parts of that object.

var worker = findshift(id).worker.lastname;

Open in new window


The problem is that if findshift() returns null, then I get a javascript error about how it can't get the .worker property of null. The same goes if the worker property is null and lastname is null. What I really want is for the whole thing to just assign null if any part of the chain is null. One option is the following:

var worker = finshift(id) && findshift(id).worker && findshift(id).worker.lastname;

Open in new window


but that requires me to run the findshift() function three times, which might be expensive. Another option is this:

var lastname = null;
var worker = null;
var shift = findshift(id);
if (shift) {
    worker = shift.worker;
    if (worker) {
        lastname = worker.lastname;
    }
}

Open in new window


but I find that to verbose and klunky. Is there a better way to do this? Is Try/Catch the solution?
0
Comment
Question by:mankowitz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 39217764
if (shift  = findshift(id) == null) return;
if (worker = shift.worker == null) return;
if (lastname = worker.lastname == null) return;
// All data available here
0
 
LVL 11

Accepted Solution

by:
MajorBigDeal earned 500 total points
ID: 39217769
if (shift = findshift(id)) {
   if (worker = shift.worker) {
      if (lastname = worker.lastname) {
         // All data available here
      }
   }
}
0
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 39226601
I don't know that this is the most elegant way but I think it is readable!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Can't get javascript video player button to work 10 23
Adding 1 the value of input "text" every 3 seconds 5 33
Why is my $_POST not going to results page 10 41
Slush on text 2 11
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question