Imperative versus declarative programming

Imperative programming means describing the computations in terms of statements, as we do in procedural programming. In imperative programming, we instruct the computer on which operation to do and how to do it. It is an explicit sort of coding. For example, we use imperative programming to perform certain operations and catch all the exceptions that may occur, or to install software if it does not exist on a specific machine. PowerShell uses imperative programming style, except in the DSC feature. In the preceding topic, we installed the PowerShell ISE using the ServerManager module, which is an imperative programming practice. Let's revisit it—what we will do here is check whether the PowerShell ISE feature is installed; if not, we will proceed with the installation.

We removed the PowerShell ISE using the Remove-WindowsFeature cmdlet. Now, we will execute the following code to install the PowerShell ISE:

Import-Module ServerManager
If (-not (Get-WindowsFeature -Name "PowerShell-ISE").Installed) {
    try {
        Write-Host "Feature is not available....Installation begins...." -ForegroundColor Green
        Add-WindowsFeature -Name "PowerShell-ISE" -ErrorAction Stop -Verbose
    }
    catch {
        $_.Exception 
    }
}

How does this work? Take a look at the following image:

Imperative versus declarative programming
  • It imports the ServerManager module
  • It checks the installation status; if this is true, no action is taken
  • If the installation status returns false, the installation will begin
  • Adding the PowerShell ISE feature doesn't need a reboot, but removing it requires one.

Let's take a look at how the same operation can be performed using PowerShell DSC.

In PowerShell DSC, we can do this using declarative syntax. For this, we will use the Configuration block, as shown in the following code:

Configuration InstallISE
{
    Node localhost
    {
        WindowsFeature ISE
        {
            Name =  'PowerShell-ISE'
            IncludeAllSubFeature = $true
            LogPath = 'C:LogISE.txt'
            Ensure = 'Present'
        }
    }
}
InstallISE

Take a look at the verbose message in the following image—here, it skipped the installation because the feature is already installed:

Imperative versus declarative programming

By simply changing the Ensure property to Absent, we can uninstall the feature, as shown in the following code:

Configuration InstallISE
{
    Node $ENV:COMPUTERNAME
    {
        WindowsFeature ISE
        {
            Name =  'PowerShell-ISE'
            IncludeAllSubFeature = $true
            LogPath = 'C:LogISE.txt'
            Ensure = 'Absent'
        }
    }
}
InstallISE

Take a look at the verbose message, which appears while applying the configuration, in the following image; it informs us about the reboot, so the action is not completed until we reboot the machine:

Imperative versus declarative programming

Here is a comparison of imperative and declarative programming:

Imperative

Declarative

This describes the task in statements.

This describes the action required.

A step-by-step procedure of execution is required in the code.

In this, we describe the requirement in the code, and the machine executes the task as required.

Let's review a few more declarative samples to study DSC's features and benefits.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset