Laracon Online 2020 - Refactoring to Simplicity

Laracon Online 2020 - Refactoring to Simplicity

F02afcecaa4f0ac77531c6efdead2e67?s=128

Marcel Pociot

August 26, 2020
Tweet

Transcript

  1. REFACTORING TO SIMPLICITY

  2. MARCEL POCIOT BEYOND CODE @MARCELPOCIOT

  3. WHAT DOES "SIMPLE" MEAN?

  4. <?php use PHPUnit\Framework\TestCase; class StackTest extends TestCase { public function

    testPushAndPop() { $stack = []; $this->assertSame(0, count($stack)); array_push($stack, 'foo'); $this->assertSame('foo', $stack[count($stack)-1]); $this->assertSame(1, count($stack)); $this->assertSame('foo', array_pop($stack)); $this->assertSame(0, count($stack)); } }
  5. public function test_profile_photo_can_be_updated() { $file = new UploadedFile( public_path('img/color-logo.png'), 'color-logo.png',

    'image/png', null, null, true ); Storage::shouldReceive('disk') ->once() ->with('public') ->andReturn($disk = Mockery::mock('StdClass')); $disk->shouldReceive('put'); $disk->shouldReceive('url')->once()->andReturn('/profile/photo'); $this->actingAs(factory(User::class)->create()) ->json('POST', '/settings/photo', [ 'photo' => $file, ]); $this->seeStatusCode(200); $this->seeInDatabase('users', [ 'photo_url' => '/profile/photo', ]); }
  6. WHEN WE TEACH, WE START SIMPLE ▸ Focus on what

    you want to teach ▸ Let the learner focus on one thing at a time ▸ ... it fits on a slide?
  7. REALITY KICKS IN Real-life projects are not simple

  8. DEVELOPERS LOVE COMPLEXITY

  9. I'm not a begi er! I write my own framework!

    — Every developer, at some point
  10. L k at these 1000 LOC that I wrote. Only

    I can understand it! — Senior Software Developer
  11. None
  12. None
  13. None
  14. SIMPLE CODE IS... ▸ Expressive ▸ Readable ▸ Understandable ▸

    Self explanatory ▸ Reassuring ▸ Fun
  15. IS SIMPLE CODE BETTER?

  16. Yes

  17. Thank you for coming to my TED talk

  18. IS SIMPLE CODE FASTER TO WRITE?

  19. Nope

  20. None
  21. None
  22. None
  23. TINKERWELL

  24. FAMILIAR CODE != SIMPLE CODE

  25. IS SIMPLER CODE BETTER TO TEST?

  26. Usua y, Yes

  27. class MetricController extends Controller { /** * List the metrics

    for the given resource. * * @param \Laravel\Nova\Http\Requests\MetricRequest $request * @return \Illuminate\Http\Response */ public function index(MetricRequest $request) { return $request->availableMetrics(); } }
  28. OKAY...SIMPLE IS GOOD AND ALL, BUT WHAT'S IN FOR ME?

  29. ▸ Simpler code is more readable ▸ Unsurprising code is

    more maintainable ▸ Expressive code is fun !
  30. HOW CAN I WRITE SIMPLE CODE?

  31. KNOW WHAT SIMPLE CODE LOOKS LIKE

  32. READ OTHER PEOPLES CODE

  33. DO CODE REVIEWS

  34. REFACTOR CODE AS IT GROWS

  35. SIMPLE RULES FOR SIMPLER CODE

  36. NAMING VARIABLES $list = MapSpot::orderBy("name")->get(); foreach($list as $key => $row)

    { $max = MapSpotSight::where("map_spot_id", "=", $row->id)->max("datetime"); $list[$key]->sights = MapSpotSight::where("map_spot_id", "=", $row->id) ->where("hides_at", ">", time()) ->where("datetime", "=", $max) ->orderBy("hides_at", "ASC") ->get(); }
  37. USEFUL METHOD NAMES public function updateData($user, $project) { // ...

    }
  38. RETURN EARLY public function doSomething($someParameter, $someOtherParameter) { if ($someParameter ===

    1) { if ($someOtherParameter === 1) { // do the actual work } } }
  39. RETURN EARLY public function doSomething($someParameter, $someOtherParameter) { if ($someParameter !==

    1) { return; } if ($someOtherParameter !== 1) { return; } // do the actual work }
  40. Any inte igent f l can make things bi er,

    more complex, and more violent. It takes a touch of genius—and a lot of courage to move in the opposite direction. — Albert Einstein
  41. Any inte igent f l can make things bi er,

    more complex, and more violent. It takes a touch of genius—and a lot of courage to move in the opposite direction. — E.F. Schumacher
  42. SIMPLICITY TAKES COURAGE

  43. None
  44. None
  45. SPEAKING OF LARAVEL

  46. Route::get('user/(:num)', function($id) { // });

  47. if (Auth::check()) { return "You're logged in!"; }

  48. test('asserts true is true', function () { assertTrue(true); });

  49. class SearchUsers extends Component { public $search = ''; public

    function render() { return view('livewire.search-users', [ 'users' => User::where('username', $this->search)->get(), ]); } }
  50. TAKE COURAGE ▸ Know what you can skip to make

    things easy and simple ▸ Be brave enough to present your code ▸ Leave behind code that reflects you and your abilities
  51. Now that my code is expressive, can I be replaced?

  52. ▸ Learn ▸ Read ▸ Care ▸ Test ▸ Communicate

  53. None
  54. Writing simple code is not simple But worth it

  55. THANK YOU