- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Functions — this is something not used when coding
• Catalog client scripts
• Catalog UI Policies
• UI Macros
• UI Pages
• UI Scripts
In the world of coding functions are your Super Friends. Functions are useful for a multitude of reasons, and if you are not using them now you definitely should be. Functions will make your coding life easier.
Functions are modular units of execution. What this means is you can stick some code into a function and reuse it in different places. You can collect a set of functions into an object and create a library so you can separate your logic from Service Now Logic and still get results.
Beyond that when using the Chrome Debugger, the debugger will not jump into the center of some page, but instead go into a familiar function you wrote. The function should determine if you have all the elements needed to work and if not it should return an error.
Functions are written as declarations
function NameOfFunction ( argument1, argument2 ) {
//Code in here
}
or as an expression
var funcVar= function(){
//Code In Here
};
For the purpose of this page we will look at how to use functions to check if elements really exist: (defensive coding).
Functions take values or objects passed in as arguments.
- The function parameters should be verified before using them. They should not only be checked for existence, but also ensure they are in the correct range of elements we want.
- One should never assume the correct value is in the variable. Assumptions like this lead to mistakes, wasted time, and lost money.
- If the checks do not work out, return a handling error so we can see the problem.
Here is an example of how to check for empty variables with an isEmpty function that should be used everywhere.
function isEmpty(elem){
if(elem !== null && typeof elem !=="undefined" && elem!== ""){
return 1;
}
else{ return 0; }
}
function checkInternalVariables(elements) {
var error = "GOOD";
console.log(elements);
for (var i=0; i< elements.length; i++){
if(1 == isEmpty(elements[i])){
continue;
}
else { error ="ERROR"; break; }
}
return error;
}
function hideVariables(a,b,c,d,e,f,g){
if (checkInternalVariables(arguments) == "GOOD" ){
console.log("WRITE SOME CODE");
}
else {
console.log("MESSED UP");
return (function(){console.log("HANDLE YOUR ERROR")})();
}
}
hideVariables(1,2,3,4,5);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.