coding standards


Critical Items
Statement: Xrm.Internal.
Example:  'Xrm.Internal.openDialog'.
Description: 'Xrm.Internal.openDialog' references an undocumented feature of the internal Dynamics CRM client object model. This technique is unsupported in all versions of Dynamics CRM. Remove or replace with a supported API provided in the Xrm.Page client object model.
Guideline: Xrm.Utility.openWebResource("new_webResourcename.htm", null, 300,300);

Statement: Xrm.DialogOptions.
Example:  'new Xrm.DialogOptions()'.
Description: 'new Xrm.DialogOptions()' references an undocumented feature of the internal Dynamics CRM client object model. This technique is unsupported in all versions of Dynamics CRM. Remove or replace with a supported API provided in the Xrm.Page client object model.
Guildeline: according to your scenario use the supported Xrm.Utility.openWebResource

Statement: openStdWin(url, "", features)
Comments: 'openStdWin(url, '', features)' calls an undocumented feature of the internal Dynamics CRM client object model. This technique is unsupported in all versions of Dynamics CRM. Remove or replace with a supported API provided in the Xrm.Page client object model
Guideline:  use  window.open(url, "", features);

Statement: document.body.
Example:  document.body.scrollLeft'.
Description'document.body' calls a document object property of the CRM document object model (DOM) directly. This technique is unsupported in all versions of Dynamics CRM. Remove or replace with a supported API provided in the Xrm.Page client object model.

Statement: openStdDlg(("new_webResourcename.htm", null, 300,300).
Description: openStdDlg calls an undocumented feature of the internal Dynamics CRM client object model. This technique is unsupported in all versions of Dynamics CRM. Remove or replace with a supported API provided in the Xrm.Page client object model.
Guideline : Xrm.Utility.openWebResource("new_webResourcename.htm", null, 300,300);


Statement:  APPLICATION_VERSION
Example:  if (parseInt(APPLICATION_VERSION) >= 7) {
                                XRMSVCToolKit = "/WebResources/mtccp_/CopyRecordSet/Scripts/XrmSvcToolkit.js";
                }
Description:'APPLICATION_VERSION' references an undocumented feature of the internal Dynamics CRM client object model. This technique is unsupported in all versions of Dynamics CRM. Remove or replace with a supported API provided in the Xrm.Page client object model.
Guideline: use Xrm.Page.context.getVersion()

Statement:  xmlHttp.open("GET", XRMSVCToolKit, false)
Example:
 function IncludeExternalScript(targetDoc, filename) {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET", filename, false);
    xmlHttp.send(null);
    if (xmlHttp.status == 200 || xmlHttp.status == 304) {
        IncludeScript(targetDoc, xmlHttp.responseText);
    }
    else {
        alert("Failure loading external script: " + filename);
    }
}
Description: 'xmlHttp.open('GET', XRMSVCToolKit, false)' appears to call a document object function of the CRM document object model (DOM) directly. This technique is unsupported in all versions of Dynamics CRM. Remove or replace with a supported API provided in the Xrm.Page client object model.
Guideline: Do not Load web resource manually in scripts instead load them in form or in button actions.


Statement: crmForm.all.
Example : for (var i = 0; i < crmForm.all.length; i++) {
                            oCtrl = crmForm.all[i];
                             }
Description: 'crmForm.all' accesses a form property in the deprecated Dynamics CRM 4.0 crmForm.all client object model.  The Dynamics CRM 4.0 client object model was deprecated in Dynamics CRM 2011 and is unsupported starting with Dynamics CRM 2013.
Guideline: Replace this call with the following Xrm.Page API: Xrm.Page.data.entity.attributes or Xrm.Page.ui.controls

Statement: eval(xmlHttp.responseText);
Example:  var XRMSVCToolKit = "../WebResources/mtccp_/CopyRecordSet/Scripts/XrmSvcToolkit.js";
                if(XrmSvcToolkit == undefined || XrmSvcToolkit == null) {
                                var xmlHttp = new XMLHttpRequest("Microsoft.XMLHTTP");
                                xmlHttp.open("GET", XRMSVCToolKit, false);
                                xmlHttp.send();
                                eval(xmlHttp.responseText);
                }
Description: Do not use the 'eval' function or its functional equivalents, which obscure code function, complicate tooling, and are a source of non-performant and insecure code patterns. This violation occurred due to use of 'window.eval' detected in source.
Guideline: Do not load web resource in the java script manually . Instead load them in form or in button actions.






Statement :  Avoid Global declaration of variables in c# , if you use them only in one method.

Statement :  Fully Implement the exception handler in the c#.
Example:  throw new InvalidPluginExecutionException("error message");
Description:  An unguarded throw of type Microsoft.Xrm.Sdk.InvalidPluginExecutionException was detected. Refactor this code to either throw an exception of type InvalidPluginExecutionException or guard against thrown exceptions of other types.
Guideline: throw new InvalidPluginExecutionException("error message");
          throw new Exception("error message");

Statement : Do not declare the same variable within same scope(project/file).

High Items
Statement: Xrm.Page.context.getServerUrl()
Example:  Xrm.Page.context.getServerUrl()
Description: Xrm.Page.context.getServerUrl()' calls a deprecated API in the Dynamics CRM 2011 Xrm.Page client object model. This API was deprecated in Dynamics CRM 2011 and is unsupported starting with Dynamics CRM 2013. Replace this call with the following Xrm.Page API: Xrm.Page.context.getClientUrl().

Module: mtcexp_ReimbursementPopup
Statement: window.showModalDialog(Xrm.Page.context.getClientUrl()+"/WebResources/mtcexp_ListReimbursements.html", paramdata, "dialogWidth:700px; dialogHeight:320px;center:yes; addressbar:no")
Source: .../WebResources/mtcexp_ReimbursementPopup.js
Line: 75
Comments: window.showModalDialog(Xrm.Page.context.getClientUrl()+'/WebResources/mtcexp_ListReimbursements.html', paramdata, 'dialogWidth:700px; dialogHeight:320px;center:yes; addressbar:no')' opens a modal dialog. This call references a feature being deprecated in all modern browsers.
Alternate: Xrm.Utility.openWebResource("mtcexp_ListReimbursements.html?id=" + expenseId, null, 900, 420);

 Medium Items


 Medium Items

Statement: new Object()
Example: var a= new Object();
Description: Use short-form initialization for constructing built-in types.
GuideLine: var a={};

Statement: new Array()
Example: var a= new Array();
Description: Use short-form initialization for constructing built-in types.
GuideLine: var a=[];

Statement: An HTML element that references URL targeting a CRM Web Resource file was identified to be invalid. Web Resource URL's should always be relative and not include the '/WebResources/' root in their path.
Example: "/../../ WebResources/mtcalt_/scripts/jquery.js" />
Description:The param element references a relative URL /../../WebResources/mtcalt_/scripts/ jquery.js  that targets a CRM Web Resource file using an invalid structure. Web Resource URL's should always be relative to ensure caching support and not include the \WebResources\ root in their path to avoid potential CRM organization conflict.
Giudline"/../../ ../mtcalt_/scripts/jquery.js

Statement: Always provide braces for conditional statement control blocks
 Example: if(b==undefined) b=null
Description: Always provide braces for conditional statement control blocks.
Guidline: Use if(Condition Statement){
}
Statement: Use the strict equality and inequality operators (=== and !==) exclusively for comparisons.
Example:   If( b==null) or if( b ! =null)
Description:   The == and != perform type coercion before comparison, which can lead to subtle correctness problems. Use the strict comparison operators (=== and !==) exclusively for comparisons.
Guideline: If( b===null) or if( b ! ==null){
}

Statement: Do not explicitly reference 'undefined' .
Example:   if( b==undefined) or if( b ! =undefined)
Description:  Do not explicitly reference 'undefined' as it is subject to redefinition. Use 'null' (which cannot be redefined) instead or eliminate the explicit reference to 'undefined' altogether.
Guideline: if( typeof(b)===”undefined”)
                 if(typeof(b)!==”undefined”)

Statement: When comparing to literals, place the literal on the right-hand side.
Example:  if( null != a) or if(200 == a.status)
Description When comparing to literals, place the literal on the right-hand side.

Statement: Avoid using Javascript functions like window.alert() and window.confirm(). 
Use supported functions from the Xrm.Utility namespace like alertDialog() and confirmDialog() instead.
Example:  alert(“Not a Valid License Key. Contact MTC.")
Description A call to alert is being made within the context of a CRM form. It is preferred to use the provided API Xrm.Utility.alertDialog(message [, okCallback]) on CRM forms to ensure cross-device/platform compatibility.
Guideline: Xrm.Utility.alertDialog(“”); and Xrm.Utility.confirmDialog(“”);

Statement: Do not declare multiple variables with an identical name in the same scope.

Statement : Specify 'new' keyword when calling constructors.
Example:  function FunctionName(){
}
Description: The call to “FunctionName” does not include the 'new' keyword.
If  ’ FunctionName' is a function, Change it to pascal case.
By convention, functions are marked by pascal-cased names.
Guidline:
function functionName(){
}


Statement: Place 'else' keyword on the same line as the closing brace of the previous control block.
Example:  if (a===b) {
//your code goes here.
}
else {
// your code goes here.
}
Description: Place 'else' keyword on the same line as the closing brace of the previous control block.
Guidline: :  if (a===b) {
//your code goes here.
} else {
// your code goes here.
}

Statement : Do not declare functions with identical names.
Example: Function 'successCallbackRec' was previously declared.
Description: Do not declare functions with identical names.

Statement : Performance.
Example:
 Avoid repetitive field or property access.
Optimize code inside the loop
Copy frequently called code into the loop.
Replace recursion with looping
Description: Consider optimizing expensive loops as it can cause performance problems.

Statement : string concatenation.
Example: String  path = “webResources/scripts/alerts” + Xrm.Page.context.getClientUrl();
Description:  call to String.Concat and/or String.Split were found. Avoid such calls since this can potentially lead to poor memory allocation patterns.
Guidline: StringBuilder sb= new StringBuilder()
Sb.append(“webResources/scripts/alerts”);
Sb.append(“Xrm.Page.context.getClientUrl()”);
String path=sb;

Statement  : Bitwise operations in JavaScript.
Example:  if( ((chr1 & 3) << 4) | (chr2 >> 4)){
}
Description: Bitwise operations in JavaScript can convert operands from floats to integers and back. For performance reasons, you should therefore avoid their use for operations against numbers where the count of bits involved may exceed 32. The use of bitwise operators where a logical operator was intended can also be a source of unexpected correctness issues in code.

Statement : Performance.
Description:Consider moving away from using the XmlDocument type by migrating to the XElement or XDocument type, which have been included since the .NET 3.5 release.
You can create an XML Tree using XElement as follows:
Guildline:   XElement contacts = new XElement("Name", "Patrick Hines");

Statement : window.close()
Description: 'window.close()' appears to call a document object function of the CRM document object model (DOM) directly. This technique is unsupported in all versions of Dynamics CRM. Remove or replace with a supported API provided in the Xrm.Page client object model.
Guildline:   Xrm.Page.ui.close();  or Xrm.Page.data.entity.save('saveandclose');
Statement : window.open()
Description: 'window.close()appears to call a document object function of the CRM document object model (DOM) directly. This technique is unsupported in all versions of Dynamics CRM. Remove or replace with a supported API provided in the Xrm.Page client object model.

Guildline:   Xrm.Utility.openEntityForm(entity, id);

Comments

Popular posts from this blog

Basic Plugin Code in D365 using C#

CURD (Create, Update, Retrieve and Delete) Operation in D365 using Power Shell Script

Meta Data Using WebApiRequest