Avoid using functions before their declaration

Avoid using functions before their declaration

[nz_btn text="Software Agility" link="http://casthighlight.wpengine.com/category/product/indicators-methodology/code-insights/software-agility/" target="_self" animate="false" animation_type="ghost" color="pink" size="small" shape="rounded" type="normal" hover_normal="fill" hover_ghost="fill"][nz_btn text="Code Readability" link="http://casthighlight.wpengine.com/category/product/indicators-methodology/code-insights/software-agility/code-readability/" target="_self" animate="false" animation_type="ghost" color="black" size="small" shape="rounded" type="ghost" hover_normal="fill" hover_ghost="fill"]This code insight counts the number of cases when functions are called prior to their definition in the source code. Based on the density of this bad practice and on specific thresholds CAST has defined, Highlight counts penalty points for the scanned file. Pattern example (here, in Javascript): var foo = myFunction(10); // eventually some stuff... function myFunction(param) {    // your function definition }

Why you should care

Have you ever tried to watch a movie by starting in the middle of the story? It is hard to really understand who's who since you may have missed some important details that were explained in the first scenes. Most (approx. 99.9%) developers read their source code by scrolling down, hence function definitions should be placed before (above) they're used in the application, in order to know the different parameters of a function they're about to modify. References: http://eslint.org/docs/rules/no-use-before-define http://javascript.crockford.com/code.html

CAST recommendations

For readability purpose, it is better to define a function before using it, as follows: function myFunction(param) {    // your function definition } // eventually some stuff… var foo = myFunction(10);

About CAST and Highlight's Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.[nz_btn text="How it works" link="http://casthighlight.wpengine.com/how-it-works/" target="_self" animate="true" animation_type="ghost" color="black" size="small" shape="rounded" type="normal" hover_normal="fill" hover_ghost="fill" icon="icon-cog"][nz_btn text="Features & Analytics" link="http://casthighlight.wpengine.com/outputs-analytics/" target="_self" animate="true" animation_type="ghost" color="pink" size="small" shape="rounded" type="ghost" hover_normal="fill" hover_ghost="fill" icon="icon-stats"]
For reference only. For the complete details please refer the original article
https://doc.casthighlight.com/alt_baddeclarationorder-avoid-using-functions-declaration/
Have more questions? Submit a request

Comments

Powered by Zendesk