Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laracon Online 2020 - Refactoring to Simplicity

Laracon Online 2020 - Refactoring to Simplicity

Avatar for Marcel Pociot

Marcel Pociot

August 26, 2020
Tweet

More Decks by Marcel Pociot

Other Decks in Programming

Transcript

  1. <?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)); } }
  2. 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', ]); }
  3. 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?
  4. I'm not a begi er! I write my own framework!

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

    I can understand it! — Senior Software Developer
  6. Yes

  7. 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(); } }
  8. ▸ Simpler code is more readable ▸ Unsurprising code is

    more maintainable ▸ Expressive code is fun !
  9. 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(); }
  10. RETURN EARLY public function doSomething($someParameter, $someOtherParameter) { if ($someParameter ===

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

    1) { return; } if ($someOtherParameter !== 1) { return; } // do the actual work }
  12. 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
  13. 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
  14. class SearchUsers extends Component { public $search = ''; public

    function render() { return view('livewire.search-users', [ 'users' => User::where('username', $this->search)->get(), ]); } }
  15. 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