Creating a new project | #05 - DRY code

in #dev5 years ago

If you haven't read the last post you can find it here


**TLDR** I am building a web application from scratch using PHP Laravel and learning in the process, the application will be a Recipe sharing app that I will build on overtime.
So in the last post, I had made great progress. I now had the ability to create/delete & edit recipes(posts).
I wanted to add the ability for the user to add steps while creating a recipe, how I initially coded this was a bit of a mess. Spaghetti code at its finest. So I added in some simple for loops to cut the number of lines of code. DRY coding (Don't Repeat Yourself).

You can see the top 4 lines of code replace the 15 commented outlines.
I used a similar approach over a few different areas, on the edit page I used a similar loop to display the step fields and then followed by a Javascript for loop to hide any empty fields:

While looking through my code I realized how I had created the database migration was messy and I hadn't DRY coded it. The small issue is with changing this I would have to do a fresh migration to the database, which will remove all posts/users from my DB but at this stage, it's best to start with DRY code as it's easier to improve now. (this is how it currently looks like):
public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->mediumText('ingredients');
            $table->mediumText('body'); 
            $table->mediumText('step1'); 
            $table->mediumText('step2')->nullable(); 
            $table->mediumText('step3')->nullable(); 
            $table->mediumText('step4')->nullable(); 
            $table->mediumText('step5')->nullable(); 
            $table->mediumText('step6')->nullable(); 
            $table->mediumText('step7')->nullable(); 
            $table->mediumText('step8')->nullable(); 
            $table->mediumText('step9')->nullable(); 
            $table->mediumText('step10')->nullable(); 
            $table->mediumText('step11')->nullable(); 
            $table->mediumText('step12')->nullable(); 
            $table->mediumText('step13')->nullable(); 
            $table->mediumText('step14')->nullable(); 
            $table->mediumText('step15')->nullable(); 
            $table->timestamps();
            //$table->enum('level', ['easy', 'hard']);
        });
    }



This is my approach:

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->mediumText('ingredients');
            $table->mediumText('body'); 
            $table->mediumText('step1'); 
            for ($x = 2; $x < 16; $x++) {
                $guff = 'step'.$x;
                $table->mediumText($guff)->nullable(); 
            }
            $table->timestamps();
            //$table->enum('level', ['easy', 'hard']);
        });
    }



Now I need to run a terminal command to do a fresh DB migration

    php artisan migrate:fresh



And there we have some cleaned up DRY code. It may not seem like big progress but cleaning up the code will firstly make it run a little better but also allows for future changes to be made easier.


For example, I have set a limit to 15 steps for a recipe, I originally chose this as I didn't want to code in any more than 15 as it was looking messy. Now I've created the code in for loops extending the limit of steps is now a much simpler task. And requires changing the number in the for loops... :)


Thanks for reading and following the process, I'd love to hear from you. Feel free to drop a comment below. Feedback is key to my learning!
Sort:  

I've worked with software developers for the past 3 years and have never come across the "DRY" thing - great to learn something new!

Coin Marketplace

STEEM 0.21
TRX 0.20
JST 0.034
BTC 90489.61
ETH 3100.75
USDT 1.00
SBD 2.93