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!
author | slayerkm |
---|---|
permlink | creating-a-new-project-or-05-dry-code |
category | dev |
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"]} |
created | 2019-07-27 11:15:03 |
last_update | 2019-07-27 11:15:03 |
depth | 0 |
children | 1 |
last_payout | 2019-08-03 11:15:03 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.178 HBD |
curator_payout_value | 0.042 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 4,159 |
author_reputation | 56,134,054,844,399 |
root_title | "Creating a new project | #05 - DRY code" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 88,804,706 |
net_rshares | 677,990,801,385 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
eftnow | 0 | 6,047,491,691 | 18% | ||
sergiomendes | 0 | 2,704,439,414 | 1% | ||
greencross | 0 | 8,298,093,715 | 55% | ||
toonpunk | 0 | 1,858,419,763 | 100% | ||
slayerkm | 0 | 2,439,666,148 | 27.5% | ||
passion-fruit | 0 | 5,960,102,353 | 100% | ||
fortune-master | 0 | 5,582,129,233 | 100% | ||
stefanmilde | 0 | 128,901,364 | 11% | ||
chrispy99 | 0 | 7,492,622,502 | 16.5% | ||
challengemaster | 0 | 523,285,092 | 100% | ||
lucyloo | 0 | 108,095,014 | 100% | ||
leotrap | 0 | 1,322,006,569 | 2.75% | ||
kromtar | 0 | 42,800,883,991 | 18% | ||
brandongiesing | 0 | 471,547,862 | 27.5% | ||
maneki-neko | 0 | 3,258,961,385 | 7% | ||
insanityisfree | 0 | 166,968,499 | 13.75% | ||
buttcoins | 0 | 39,871,619,779 | 11% | ||
petrolinivideo | 0 | 5,458,848,604 | 50% | ||
veganroma | 0 | 920,042,154 | 11% | ||
booster916 | 0 | 1,994,003,720 | 3% | ||
justindaviesart | 0 | 123,345,777 | 50% | ||
evecab | 0 | 17,080,281,242 | 27.5% | ||
fitcuriousgeorge | 0 | 302,917,929 | 50% | ||
tim.clarke | 0 | 255,260,490 | 8% | ||
lostinhappiness | 0 | 5,111,059,524 | 25% | ||
lukecreed | 0 | 160,174,814 | 27.5% | ||
joshbillings | 0 | 5,589,956,746 | 27.5% | ||
artakush | 0 | 7,931,308,733 | 27.5% | ||
adventuroussoul | 0 | 28,042,193,563 | 10% | ||
sv1rby | 0 | 333,012,704 | 1% | ||
myndnow | 0 | 1,145,854,999 | 10% | ||
evaslife | 0 | 286,242,186 | 50% | ||
tricki | 0 | 466,180,882 | 5.5% | ||
harshilpatel | 0 | 17,107,675,883 | 100% | ||
alexabsolute | 0 | 2,315,453,027 | 5.5% | ||
dtubedaily | 0 | 36,773,312,297 | 55% | ||
crowbarmama | 0 | 1,200,422,969 | 5.5% | ||
art.life | 0 | 749,736,378 | 3.85% | ||
gabbyg86 | 0 | 15,072,273,447 | 35% | ||
loolooasuna | 0 | 744,998,645 | 25% | ||
alphasteem | 0 | 1,124,532,720 | 5.5% | ||
jeffmcmullen | 0 | 1,554,893,961 | 22% | ||
ultrablackhole | 0 | 828,537,824 | 100% | ||
neopch | 0 | 2,498,987,257 | 20% | ||
gtelefon | 0 | 2,141,711,234 | 20% | ||
mrchef111 | 0 | 13,292,359,616 | 13.75% | ||
yanirauseche | 0 | 1,830,879,700 | 11% | ||
big-m | 0 | 1,509,789,367 | 25% | ||
illuminationst8 | 0 | 6,969,175,672 | 27.5% | ||
waybeyondpadthai | 0 | 2,808,851,341 | 3.85% | ||
davidcentenor | 0 | 561,060,733 | 27.5% | ||
moserich | 0 | 14,120,162,383 | 100% | ||
camuel | 0 | 35,705,258,228 | 50% | ||
scolari-ire | 0 | 20,911,730,951 | 60% | ||
hafizullah | 0 | 22,927,454,627 | 16.5% | ||
travellit | 0 | 1,404,265,701 | 0.55% | ||
dtalk | 0 | 434,015,656 | 55% | ||
sbi5 | 0 | 36,753,481,051 | 16.58% | ||
hmetu | 0 | 4,516,588,806 | 11% | ||
brogamer | 0 | 78,196,139 | 22% | ||
acousticguitar | 0 | 656,104,835 | 27.5% | ||
sunsets | 0 | 3,282,051,524 | 100% | ||
dlivestreamers | 0 | 210,516,264 | 50% | ||
ddaily | 0 | 140,207,479,088 | 55% | ||
thesassysergio | 0 | 1,414,880,502 | 50% | ||
spicereviews | 0 | 73,536,170 | 27.5% | ||
priyanarc | 0 | 24,542,090,329 | 27.5% | ||
megaraz | 0 | 130,823,525 | 100% | ||
devsup | 0 | 28,349,771,001 | 7% | ||
dtube.forum | 0 | 27,426,057,978 | 55% | ||
hungryharish | 0 | 657,931,237 | 22% | ||
annitakoxx | 0 | 70,492,870 | 27.5% | ||
k64 | 0 | 797,342,008 | 27.5% | ||
renyd | 0 | 0 | -10% |
I've worked with software developers for the past 3 years and have never come across the "DRY" thing - great to learn something new!
author | stav |
---|---|
permlink | pvypui |
category | dev |
json_metadata | {"tags":["dev"],"app":"steemit/0.1"} |
created | 2019-08-09 09:18:18 |
last_update | 2019-08-09 09:18:18 |
depth | 1 |
children | 0 |
last_payout | 2019-08-16 09:18:18 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 132 |
author_reputation | 52,141,742,331,876 |
root_title | "Creating a new project | #05 - DRY code" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 89,325,333 |
net_rshares | 0 |