Softstuff Consulting
My occasional musings of a technical nature
  • Send us a message
  • About Softstuff Consulting
  • Privacy Policy
  • Main Website
Dynamics365 , JavaScript , Thoughts

Make JavaScript a little more readable

by Ian Blair March 5, 2020 No Comments

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.

  • Previous Make fields read-only in a Dynamics editable grid5 years ago
  • Next Call a Dynamics365 workflow from JavaScript5 years ago

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to make your Powershell scripts more annoying
  • What motherboard is in my PC
  • A Dynamics365 plugin thought experiment
  • Registering a Dynamics365 plugin and I get an error
  • Going back in time with Dynamics365

Categories

  • Bootstrap
  • C#
  • CSS
  • Dot Net Core
  • Dynamics365
  • JavaScript
  • Powershell
  • T-SQL
  • Thoughts
  • VBScript
  • Visual Studio
  • Windows
  • Xamarin

Recent Comments

  • S0ftStuffB055 on Call a Dynamics365 workflow from JavaScript
  • Siva on Call a Dynamics365 workflow from JavaScript
  • TC Sharpe on Throw exceptions in Dynamics365 workflow plugins, or don’t
  • BigOwl on Throw exceptions in Dynamics365 workflow plugins, or don’t
  • CRMGod on Access a Dynamics365 embedded webresource

Archives

  • January 2021
  • May 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • June 2019
  • May 2019
  • February 2019
2025 Softstuff Consulting. Donna Theme powered by WordPress
  • Twitter