I often inherit code that has been written by other people and sometimes it isn’t always the clearest or most readable code although that usually depends on how old it was. It seems that older Dynamics customisation code especially the JavaScript was often thrown together quickly.
Don’t get me wrong I have nothing against quick and dirty code, I have written a lot of it myself but sometimes figuring out the logic can be a bit tortuous.
There is one change now that I often make especially when there is an If statement that is checking to make sure that a field does not contain a number of values that is usually written something like.
if(myValue!="something" && myValue!="somethingelse" && myValue!="somethingother" && myValue!="somethingtoo"){}
Sometimes these can have a lot of clauses and its easy to make a mistake if modifying it, and perhaps falling foul of the JavaScript interpreter missing bracket that stops everything working.
My favourite way of rewriting this to make it simpler and easier to read is
var myNotList=["something","somethingelse","somethingother","somethingtoo"];
if(!myNotList.includes(myValue)){}
A simple change where I declare the array at the top of the function and populate it with the values I am looking for, and then use the prototype.includes(value) function to see if the value I am looking for is in the list.
Works for
if(myValue=="something" || myValue=="somethingelse" || myValue=="somethingother" || myValue=="somethingtoo"){}
Just as easily by changing the new code to
var myList=["something","somethingelse","somethingother","somethingtoo"];
if(myList.includes(myValue)){}
Only a small change but I personally think it makes the code much more readable.
Note: This may not work in older browsers but fortunately Dynamics almost forces users to make sure that their chosen browser is up to date, but this might be an issue for other non Dynamics projects.