Safely using external libraries and plugins

Safely using external libraries and plugins

APRIL 26, 2016     Developer Tips    
  7,067views    


4 minutes to read

Nowadays there are many frameworks, libraries and plugins available to developers on internet and sometimes these products due to their large size or the beta stage, they have potential bugs and flaws which makes the products invulnerable to these bugs if the developers are not careful to foreseen these possibilities and prepare their systems with fallback strategies. These available software would make it easier for people to code and develop systems and reduce their required development time and budget, but we should prepare for the secret key, not all of them are secure and perfect.

 

Using Latest Version

To avoid the dilemmas and potential issues we always should use the latest production version of the software which has the less potential security risk and more compatible with the existing technologies and other software. But keep in mind, using the latest version is not always the good practice as it may not be compatible with other components in your project and may cause conflict in operation of parts and whole project. Before using the latest version or updating your libraries or plugins, make sure all the dependencies are compatible with the new version and they don’t have any known issue.

 

Check Variables and Values

One of the best practices in programming is to check the variables and their values before using them in functions and external classes and libraries. Sometimes these external classes and libraries does not check the variable to see whether is accepted format or not, which as the end result cause your software to crash or stop working without any feedback. Before pass your variables to functions check if they are undefined, null or empty. This practice makes it safer to use external libraries, classes and plugins that you have not wrote yourself and it would guarantee that you will get the expected result as you should.

In JavaScript, sometimes you declare the variable but never assign a value nor use a default value for its which makes it useless to check if it’s value is null or not. Always to make sure that the variable has value, you are advised to check for the following conditions and even the datatype of the value.

var num;
if (num != undefined && num != null && typeof num == “number”) {
// the num variable is not undefined nor null and it’s datatype is number
}

To find more examples of JavaScript datatypes and how to differentiate between undefined and null values and datatypes, visit the W3Schools JavaScript DataTypes.

 

Fallback Strategy

In some cases, due to the browser, system version or the limitation in the external libraries, some functions and features may not run as it’s expected. For example, due to incorrect structure of an array, the $.each function of jQuery may cause an internal break and as end result, your system stop working. The best practice is to check if the plugin or library is loaded successfully before using any of its functions. Secondly, make sure the data is in correct format and the function is able to process the data and return the result without any issue. Lastly, prepare the fallback strategy in case the original logic failed to proceed and give you the expected results.

$(document).ready(function() {
// now the jQuery is loaded in this document and it’s ready to use
});

To achieve the best fallback strategy, you always can use the try-and-catch to catch the inner exception of the libraries and process your data with secondary logic and avoid crashing of your system.

try {
$.each(data, function(i, v) {
// iterate through the data array
});
} catch (ex) {
for (var i = 0; i < data.length; i++) {
// using for loop in case jQuery each function failed
}
}

You always can find different methods to ensure your program keep working no matter what happen to your data or which component of your software fail to work. You can search internet to find examples of fallback strategies in different platforms and programming languages.

 

Make Full Backup

As always, before updating your libraries, plugins and modifying your source code make a full backup of your files and database. So in case your project stop working, you can always recover your previous version by restoring your backup files. Also, you can start using version control services like Git and Microsoft Team Foundation Server (TFS) which is now called (Visual Studio Online) Visual Studio Team Services, to keep a versions of your project files and keep them safe on the web or private cloud storage for future use.



About the Author
Masoud Haghi

is an enthusiast designer, developer and system architect and he love to write about the creative design, integration, development and technology. Building startup companies and social branding is one of his hobbies and work routine. His latest startup company JaaMaa is comprehensive real estate system.



Share...

window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-Q027PK392X');