Contemporary Code Development Techniques

Exactly 16 years ago, NASA sent Opportunity Rover to Mars to carry out the Mars Rover mission, to collect models from the ground and rock fragments, to investigate whether there is water, in short, to explore the planet. In short, Oppy's planned mission period was 3 months, but it successfully served on the surface of Mars for exactly 8 Mars - 15 Earth years. Finally, in February 2019, he was dismissed with the shutdown signal sent from the world, and he was appreciated by the whole world for his services.

And another new mission from NASA: MARS 2020...

Just like in the example of Oppy, we, as the nations of the world, may still be benefiting from very old software products, even after many years have passed. Software designed with short-term plans may have served us much longer than expected. However, you need to be aware that they are starting to expire, hear their cries from the mistakes they make, and derive software that is suitable for the algorithmic structure of the contemporary world. In summary, it's time to send shutdown signals to the codes you've produced over the years.


At the beginning of the article, we talked about NASA, Mars, Oppy, but what you will do is not as difficult as sending a rover to Mars.

With a few simple techniques, you can make the codes you produce more understandable, more useful, and more efficient.

How come?

1) Define the variables with their data types as much as possible, write the variables with the same initial values side by side on a single line, separating them with = and equalize the result to a value, group the variables that are children of the same workgroup.

The software starts with variables! The values of the variables sometimes change within the algorithm, and sometimes they continue on their way without any change. Whether they change or not, describe them by understandable names and, if possible, by specifying their data type. Make sure that each variable or group of variables consists of (preferably) English names that can express the actions it will take. Make a struct group the variables that you will use in the same workgroup. Although the world calls it structure, I call it variable packaging :) Variable packaging processes are one of the important tools that will help you to minimize code complexity.

Example:

2) For conditions that will have one-line results, use the ternary statement colloquially, the line if, to reduce confusion.

With the triple statement method, which we also call line if, write multiple line conditions for a single line operation and also send closing signals. Instead, follow the method below.

In the above code, we have set the rover.shortName value to the roverName variable if there is a shortName index in the rover struct, and the rover.fullName value if it does not exist.

3) Take advantage of Array and Struct flavors ( ArrayFilter, ArrayMap, ArrayReduce, StructFilter, StructMap, StructReduce )

Arrays and objects are indispensable in software.

They allow us to create data models, group data, process data as a whole or individually.

Of course, various functions are needed to process the data they contain. Thanks to these functions, we can solve operations that can be much longer and more complex in a simpler way.

For example, you have a series of planets including the properties of the planets. You want to list the data in this array according to some filters or to collect some numerical values it contains. Let's see if the following examples are what you want?


Example 1: Outputting planets with an area greater than 300,000,000.

ArrayFilter

ArrayFilter takes 2 parameters, the first element being the array and the second element being the return function. It performs the operation that you defined as a function in the 2nd parameter for each line of the array you have given in the 1st parameter.

ArrayFilter is very successful in extracting by filter, after applying the filter to all array elements in order, it returns the result as an array again.

For the curious: https://cfdocs.org/arrayfilter  

Now we have the array of planets with an area greater than 300,000,000. Let's say we are wondering about 2 times the current area of each planet in this series? Let's calculate that too.

Example 2: Adding 2 times the current area of planets with a surface area larger than 300,000,000 as a new index.

ArrayMap

ArrayMap takes 2 parameters, the first element being the array and the second element being the return function. It performs the operation that you defined as a function in the 2nd parameter for each line of the array you have given in the 1st parameter.

ArrayMap produces a new result from the struct elements in the array and assigns a new element to each struct. Returns the result as an array.

For the curious: https://cfdocs.org/arraymap

Finally, we have a series that includes 2 times the area of planets with an area greater than 300,000,000, and we want 2 times the area of all planets to be summed up and returned to us as a result. We can code this example as follows.

Example 3: The output of planets with an area greater than 300,000,000 by adding 2 times their current surface measurements.

ArrayReduce


ArrayReduce takes 2 parameters, the first element being the array and the second element being the return function. It performs the operation you defined as a function in the 2nd parameter for each row of the array you have given in the 1st parameter and produces a single fixed result.

For the curious: https://cfdocs.org/arrayreduce

You can use these functions in a method called a nested pipe. Let's show it this way.

 


4) Divide your operations into parts, pay attention to functionality, create the whole using the parts.

Let's say you are developing from scratch. In this development you have made, your priority is; It should be to check the already written functions that you think can work, and to check their usefulness in your own development, if possible, to use them.

When you reach some functions that can meet your needs, construct an algorithm that can work integrated with them. If there are deficiencies, make up for the deficiencies with parametric improvements. Think of your external improvements as functional. Keep in mind that there may be the same needs in different developments later on. Functionality is an indispensable concept to get rid of code clutter, facilitate error management and improve performance.

The most appropriate method to divide the work into particles is to create a component and divide it into functions within this component.

Pay attention to the following situations when creating components!

Make sure that the components you create contain construct ( constructor - init - initialize ) functions.

Don't neglect encapsulation ( getter and setter )

Define function and variable types, work with properties.

Be careful with scope management, don't stop yourself from using this and super scopes.

Define the types of values and accessibility properties that functions return, if any.

I created a simple component below and illustrated its use.

Example: Grouping all planets with their sub-properties and listing the related planet with all its sub-properties according to the planet's sent name.

In the above example, we first created a file named planet.cfc and created a component in it.Inside the component, we have defined a property named planetModel and a constructor function named init. 

We made the Property populate with the init function. With the setPlanet function, we made it possible to assign a new element to the planetModel property. (Setter)

With the getPlanet function, we provided the return of the relevant planet's information according to the index sent from the planetModel property. (Getter)

With the getPlanetByName function, we have returned the information of the planet according to the planet name sent from the planetModel property. (Getter) 

Apart from the modernization studies that I tried to describe above, there are different techniques, but you will witness how clear your code and mind become when even these methods are applied at the beginning!

Thanks for reading.

Feedback

Did you find this content helpful?