Variable declaration creates an instance in the class

Software ResiliencyProgramming Best Practices

Why you should care

It is important to remember that KSH uses variables and parameters to store values.  It also supports data types and arrays.  The four data types KSH supports include string, integer, float and array.  If a data type is not defined then KSH assumes the variable is a string.

Usually KSH variables can be declared as a local variable because these variables will be considered as global in scope otherwise.  Having too many global variables can cause unspecified functions which leads to spaghetti code that ends up being hard to manage and collaborate with.  This in turn leads to a buggy and unproductive code.

Business Impacts

Declaring variables is advised because it is considered to be good programming practice but it also allows the code to be more resilient.  Preventing spaghetti code is absolutely key to progress, otherwise it can lead to confusion and frustration in the development teams.

Production RiskSpaghetti Code

CAST Recommendations

CAST recommends having declared variables as local variables because it allows for code to have specified functionalities and to prevent spaghetti code from occurring.

Here are a few examples below of how to declare variables –

variable= – declares variable and set it to null

$ typeset —i NUM=1 
   $ print $NUM 

NUM is set to an integer=type variable and assigned a value.

Whenever spaghetti code arises, it is important for the development team to take a few steps back, re-evaluate the project’s functions and goals.  Usually the company has a style guide but it may not be followed because it does not have the necessary guidelines that can used for current projects.  In such cases, the guides should be updated to meet the needs of developers and newer projects.


Style Guide

How we detect

This code insight showcases a non-existing variable is implicitly declared when assigned.  It is considered good practice to declare the variable before proceeding any further.

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.

See featuresHow it works