Functional IT design

Reducing code duplication

895 Views 18 July 2023
Reducing code duplication

Code duplication is the most painful thing for a lot of developers, you think you have solved the problem, but there are several instances of the same issue.

 

In many of the codebases I have seen as a Laravel developer, the console commands always seem to be the forgotten area or the part where people need to pay more attention to the quality.

 

In this tutorial, I will walk through how you can approach writing code with a focus on reducing code duplication. To begin with, let's pose a hypothetical. We have a Laravel application that is an e-commerce store, and once a day, we want to generate a report on all the sales and the status of shipments. Our current approach is to log in to the administration panel and click a button to generate the report.

 

This may be unrealistic, as the first instance here would be to automate it. But stick with me just a little while as we venture through this idea to improve it.

 

Our first step will be to create an artisan command or set of artisan commands that will generate the reports. As we start looking at reporting, it makes sense to have commands explicitly named for what we want to achieve. So let us begin with the sales figures.

 

 

 

We have a simple command here that we will be able to run and get sales figures for yesterday that are marked as complete. The query itself is simple enough. It checks the status and date was yesterday, then orders them, so the latest is first - to allow us to build a chronological report.

 

How can we improve this, though? Are there other aspects of the application where we need to get these orders in a similar order? Let's start the refactoring process.

final class SalesFigures extends Command

{

    public $signature = 'reports:sales';

 

    public $description = 'Run a daily report on sales.';

 

    public function handle(): int

    {

        $date = now()->subDay();

 

        $sales = Order::query()

            ->where('status', Status::COMPLETE)

            ->whereBetween(

                'completed_at',

                $date->startOfDay(),

                $date->endOfDay(),

            )->latest()->get();

 

        // send information through to the report builder

    }

}

Firstly, this specific query is something that we need to run in a few different areas. So we can move this to its own class for us to run.

 

final class ResultsForPeriod implements ResultsForPeriodContract

{

    public function handle(

        Builder $query,

        CarbonInterface $start,

        CarbonInterface $end,

    ): Builder {

        return $builder->whereBetween(

            'completed_at',

            $start,

            $end,

        );

    }

}
Img

Written by

Sandeep Gajera

Sandeep Gajera is a Founder and Technical Director at AstroJal Technology. He is dedicated to making the best use of modern technologies to craft end-to-end solutions. He also has a vast knowledge of Cloud management like AWS / Google Cloud. He loves to do coding so still doing the coding. Also, help employees for quality based solutions to clients. Always eager to learn new technology and implement for best solutions.