Help me Which is best practice to follow

Supriya25
Tera Guru

Hi Team,

 

here is my code, please let me know which is best practice to follow

Please check BOLDED lines in code.

 

 

Business Rule :

var result = getDetails(current.getValue('sys_id');

Or

 var result = JSON.parse(getDetails(current.getValue('sys_id')));
if(result.firstassignee){
current.u_assignee_level='Level 1 User';
} else if(result.group || result.user){
current.u_assignee_level='Level 2 User';
current.u_notify=gr.notify;
current.u_critical_level = gr.critical;
}

 

function getDetails(SYSID){
var gr ={ };

gr.firstassignee =false;
gr.group=false;
gr.user =false;

var gr1 = new GlideRecord('metric_instance');
gr.addQuery('definition=33j2krj329032rjwrjew98932jlsdwe^ORdefinition=33j2krj7863nsdf987232jlsljd^id='+SYSI);
gr.setLimit(1);
gr.query();
if(gr.next()){
  if(gr.definition=='33j2krj329032rjwrjew98932jlsdwe')
  gr.group=true;
else if(gr.definition=='33j2krj7863nsdf987232jlsljd')
        gr.user =true;

gr.critical = 'P2';
gr.notify ='Yes';
}else {
gr.firstassignee =true;
}
return gr; OR return JSON.stringify(gr);
}

 

 

I normally use

 return JSON.stringify(gr); format For Script include and Client script combination .
return gr ; format For Script include , Business rules combinations OR server-side inside function for return values like above.

Please let me know which is best practice for Server -side in the above example.

 

1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@Supriya25 

In the context of your provided code and considering best practices, returning a JavaScript object (gr) without converting it to JSON (JSON.stringify(gr)) is the recommended approach.

Because of:

1. Simplicity and Readability: Returning the JavaScript object directly (gr) keeps the code simpler and easier to read.

2. Performance: Converting an object to JSON (JSON.stringify()) incurs additional processing overhead compared to returning the object directly. If there's no need to convert the object to JSON, it's better to avoid this unnecessary step, especially in server-side operations where performance might be critical.

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

 

View solution in original post

3 REPLIES 3

Maddysunil
Kilo Sage

@Supriya25 

In the context of your provided code and considering best practices, returning a JavaScript object (gr) without converting it to JSON (JSON.stringify(gr)) is the recommended approach.

Because of:

1. Simplicity and Readability: Returning the JavaScript object directly (gr) keeps the code simpler and easier to read.

2. Performance: Converting an object to JSON (JSON.stringify()) incurs additional processing overhead compared to returning the object directly. If there's no need to convert the object to JSON, it's better to avoid this unnecessary step, especially in server-side operations where performance might be critical.

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

 

Hi @Maddysunil Thanks for your reply on that.
Sure I will follow  JavaScript object directly (gr)  :  return gr; as a Best practice and Best approach to keep simple and read-only faster. 

@Supriya25 

Happy to help you...

Please Mark Correct if this solves your query..

Thanks