Cloud Code Functions
Complexity Report
10 min
how to create a report showing the complexity of your cloud code introduction this section will teach you to generate a code complexity report of your cloud code using plato cloud code must be efficient from design as it is called many many times, a slightly worse performance can become a huge problem and affect your production environment badly if you take your time to design your cloud code efficiently, you will be able to serve more requests using smaller servers, which can lead to huge savings over time on the other hand, badly designed cloud code can only scale up in bigger, more expensive machines, which also has limitations this situation can and probably will lead to the necessity of rewriting code and more spendings over time please take your time to test, load test and constantly check reports on code complexity prerequisites to complete this tutorial, you will need a local environment with node js installed to apply unit tests you can follow the official nodejs tutorial to successfully install node js at your terminal an app created at back4app follow the create new app tutorial to learn how to create an app at back4app back4app command line configured with the project follow the setting up cloud code tutorial to learn how to set up cloud code for a project first off, we need to talk about plato we usually start developing by creating a smaller set of functions that break a big problem into smaller, easier to address ones this approach is usually fine and these initial smaller functions grow over time, making more complex operations and dealing with more data as data grows in your application, computing intensive tasks such as loops and recursive calls get called more and more, which tends to slow the application in severe cases it might even freeze the application completely this is where plato https //www npmjs com/package/plato comes in plato https //www npmjs com/package/plato is a javascript source code visualization, static analysis, and complexity tool that generates reports showing how complex your application is getting and where to address fixes to potentially speed up processes 1 installing plato if you have nodejs https //nodejs org/en/download/package manager/ and npm https //www npmjs com/ installed in your system, installing plato https //www npmjs com/package/plato is as easy as typing if you don’t, please install those before proceeding 2 running plato running plato https //www npmjs com/package/plato after installation consists of typing the following command from the directory where your cloud code is the options mean r recursive, meaning it will go into directories and subdirectories looking for files d myreportfolder (output) directory plato will create a directory named myreportfolder myreportfolder where it will store its results t “my report for this app” title plato will name this report my report for this app my report for this app this is useful to create multiple reports over time and keep track x json exclude json files you can tell plato to ignore file types so it runs faster js look for anything with the extension js to be evaluated 3 getting results in the myreportfolder myreportfolder created by the command above, you will find an index html index html containing the report open that file in a browser and you will find something like this in my case, i only had a file named main js main js , but depending on your code, you can have more files scroll down to the files files section and click the file name you want to open (main js in my case) this will open the report for that file maintainability is a value between 0 and 100 that represents the relative ease of maintaining the code a high value means better maintainability difficulty measure is related to the difficulty of the program to write or understand estimated errors is halstead’s delivered bugs is an estimate for the number of errors in the implementation the function weight has two metrics by complexity this metric counts the number of distinct paths through a block of code lower values are better by sloc source lines of code / logical lines of code now you can scroll down and watch the alerts and possible fixes that are suggested in my case, it is telling that arrow function syntax (=>)' is only available in es6 (use 'esversion 6') arrow function syntax (=>)' is only available in es6 (use 'esversion 6') , which is not a problem but let’s add some very inefficient code to that function and re evaluate 1function getsquarerootof(numberone, numbertwo, numberthree){ 2 var finalresult; 3 4 var i = 0; 5 var j = 0; 6 var k = 0; 7 8 for (i = 0; i < 100; i ++){ 9 for (j = 0; j < 100; i ++){ 10 for (k = 0; k < 100; k++){ 11 var resultone = getsquarerootof(numberone); 12 var resulttwo = getsquarerootof(numbertwo); 13 var resultthree = getsquarerootof(numberthree); 14 finalresult = resultone + resulttwo + resultthree; 15 } 16 } 17 } 18} and evaluate the result as we can see, the complexity of this function is 4, which is ok the higher the number you get, the more complex the function is and the more you should ensure it is efficient plato https //www npmjs com/package/plato will also warn you of missing semicolons and other potential javascript errors conclusion having a tool such as plato https //www npmjs com/package/plato checking the complexity of your code and continuously reworking cloud code to be as fast, and efficient as possible can lead to huge savings over time you and all developers should include this step or something similar in your development process to ensure you get the most bang for your buck serving requests