create account

Creating a new project | #05 - DRY code by slayerkm

View this thread on: hive.blogpeakd.comecency.com
· @slayerkm ·
$0.22
Creating a new project | #05 - DRY code
If you haven't read the last post you can find it [here](https://steempeak.com/dev/@slayerkm/creating-a-new-project-or-04-building-crud-posts-functionality)
<hr>
**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.
<hr>
So in the last post, I had made great progress. I now had the ability to create/delete & edit recipes(posts).
<br>
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).
<br>
https://files.steempeak.com/file/steempeak/slayerkm/fAV54Mjj-Screenshot202019-07-2620at2013.02.48.png
<br>
You can see the top 4 lines of code replace the 15 commented outlines.
<br>
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:
<br>
https://files.steempeak.com/file/steempeak/slayerkm/WugGVqXG-Screenshot202019-07-2620at2013.19.54.png
<br>
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):
<br>

```language
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']);
        });
    }
```
<br>
This is my approach:
<br>

```language
    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']);
        });
    }
```

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

```language
	php artisan migrate:fresh
```

<br>
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.
<br>
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... :) 
<br>
<hr>
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!
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 9 others
👎  
properties (23)
authorslayerkm
permlinkcreating-a-new-project-or-05-dry-code
categorydev
json_metadata{"app":"steempeak/1.14.8","format":"markdown","tags":["dev","developer","blog","app"],"users":["slayerkm"],"links":["/dev/@slayerkm/creating-a-new-project-or-04-building-crud-posts-functionality"],"image":["https://files.steempeak.com/file/steempeak/slayerkm/fAV54Mjj-Screenshot202019-07-2620at2013.02.48.png","https://files.steempeak.com/file/steempeak/slayerkm/WugGVqXG-Screenshot202019-07-2620at2013.19.54.png"]}
created2019-07-27 11:15:03
last_update2019-07-27 11:15:03
depth0
children1
last_payout2019-08-03 11:15:03
cashout_time1969-12-31 23:59:59
total_payout_value0.178 HBD
curator_payout_value0.042 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,159
author_reputation56,134,054,844,399
root_title"Creating a new project | #05 - DRY code"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id88,804,706
net_rshares677,990,801,385
author_curate_reward""
vote details (74)
@stav ·
I've worked with software developers for the past 3 years and have never come across the "DRY" thing - great to learn something new!
properties (22)
authorstav
permlinkpvypui
categorydev
json_metadata{"tags":["dev"],"app":"steemit/0.1"}
created2019-08-09 09:18:18
last_update2019-08-09 09:18:18
depth1
children0
last_payout2019-08-16 09:18:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length132
author_reputation52,141,742,331,876
root_title"Creating a new project | #05 - DRY code"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id89,325,333
net_rshares0