Af19cd0c0141bf7d6b42516706eb1d1d

How to make this code looks better?

var message = ""
if (XMLHttpRequest.responseText.length > 0)
  message = jQuery.parseJSON(XMLHttpRequest.responseText).error
if (!message)
  message = t('messages.oh_no_error')

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

August 23, 2010, August 23, 2010 19:26, permalink

No rating. Login to rate!
var message;

if (XMLHttpRequest.responseText.length > 0) {
  message = jQuery.parseJSON(XMLHttpRequest.responseText).error;
} else {
  message = t('messages.oh_no_error');
}
F9a9ba6663645458aa8630157ed5e71e

Ants

August 23, 2010, August 23, 2010 21:14, permalink

No rating. Login to rate!

@Adam: Your refactoring changes the logic. If the result of jQuery.parseJSON(XMLHttpRequest.responseText).error is an empty or null string, the original code sets the message to t('messages.oh_no_error'), but your code now leaves message as empty or null.

A8d3f35baafdaea851914b17dae9e1fc

Adam

August 24, 2010, August 24, 2010 16:01, permalink

No rating. Login to rate!

Good point. Like I have said in the past, all refactorings posted should be required to supply working unit tests. ;)

Ffb522eb0929d410d97214b01a4f3fc0

Robert Dougan

August 26, 2010, August 26, 2010 00:22, permalink

No rating. Login to rate!

I would do something like this

var message      = "",
    responseText = XMLHttpRequest.responseText;

if (responseText.length > 0) message = jQuery.parseJSON(responseText).error;
else if (!message) message = t('messages.oh_no_error');
F9a9ba6663645458aa8630157ed5e71e

Ants

August 26, 2010, August 26, 2010 18:23, permalink

No rating. Login to rate!

@Robert: Good job factoring out the responseText, but your code changes the logic the same way @Adam did. If the result of jQuery.parseJSON(responseText).error is an empty or null string, the original code sets the message to t('messages.oh_no_error'), but your code now leaves message as empty or null.

972fe29d288b40be23449f27fb0bc3bc

Miguel

August 26, 2010, August 26, 2010 20:24, permalink

No rating. Login to rate!

I think it works this way

var message = XMLHttpRequest.responseText;
message = (message.length > 0) ? (jQuery.parseJSON(message).error || t('messages.oh_no_error') : "";
55502f40dc8b7c769880b10874abc9d0

aaronhancock.myopenid.com

April 14, 2011, April 14, 2011 19:49, permalink

No rating. Login to rate!

You don't need to check the length of XMLHttpRequest.responseText for a specific value in this case. You just want to make sure it has something in there.

var message = new String();
if (XMLHttpRequest.responseText.length){
  message = jQuery.parseJSON(XMLHttpRequest.responseText).error;
}
(!message) ? message = t('messages.oh_no_error');
55502f40dc8b7c769880b10874abc9d0

aaronhancock.myopenid.com

April 14, 2011, April 14, 2011 19:56, permalink

No rating. Login to rate!

You don't need to check the length of XMLHttpRequest.responseText for a specific value in this case. You just want to make sure it has something in there.

var message = new String();
if (XMLHttpRequest.responseText.length){
  message = jQuery.parseJSON(XMLHttpRequest.responseText).error;
}
(!message) ? message = t('messages.oh_no_error');
Ca178391701c26b4fcee0efdf112f025

Dan Manastireanu

May 6, 2011, May 06, 2011 12:36, permalink

No rating. Login to rate!
var message = XMLHttpRequest.responseText.length ? jQuery.parseJSON(XMLHttpRequest.responseText).error : '';
message = message || t('messages.oh_no_error');

//As a 1 liner
var message = XMLHttpRequest.responseText.length ? jQuery.parseJSON(XMLHttpRequest.responseText).error || t('messages.oh_no_error'): t('messages.oh_no_error');

Your refactoring





Format Copy from initial code

or Cancel