Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Refactor your code - refactor yourself

Refactor your code - refactor yourself

Peter Dietrich

December 05, 2024
Tweet

Other Decks in Programming

Transcript

  1. Your entertainer while you digest  Peter Dietrich ([email protected], @xosofox)

     Freelancer  PHP since Version 3, Symfony since 1.0  Skipped 2.0 release while being in nodejs land  For 12 years VBA by day, Web by night  Since 2016 Full time/stack Freelancer  Since 2022 in training to become a therapist (Gestalttherapie)  Why am I here?
  2. Get yourself into a positive mindset  There is no

    bad code  Nobody writes bad code on purpose!  Don‘t blame, don‘t excuse
  3. Lack of  Knowledge/experience  Time  Support  Long-term

    perspective of project  Motivation / Interest $ git config --global alias.ack blame
  4. If you think "This is bad code"...  Celebrate 

    if it was yours – even more so!  Acknowledge  Become aware of better solution  Discuss (guidelines?)  Improve Refactoring should be a daily practice
  5. Main Take-Away  Nobody writes bad code on purpose 

    Celebrate that you learned a better way to do it WTF?
  6. Apply this to the world  Nobody does bad things

    on purpose  People do things because it is the only way the know  … being hurt, scared, insecure, attacked, disregarded  If you knew a better way to behave, you would do it  Changing your „inner programming“ is hard work as it is deeply written into your firmware
  7. <?php $body = Human::createBody(); $consciousness = Human::createConsciousness(); $self = new

    Human($body, $consciousness); $breathing = be_born($self); while ($breathing) { $state = $self->checkState(); $need = $state->getNeed(); $feedback = $self->express($need); $experience = $self->validate($need, $feedback); $self->addExperience($experience); }
  8. „Bad“ experiences lead to adaptation • Negative feedback leads to

    „bad feelings“ • We dont‘ want to feel bad • → If you experience a „bad feeling“, your body/mind tries to protect you in the future $state = $self->checkState(); $need = $state->getNeed(); $feedback = $self->express($need); $experience = $self->validate($need, $feedback); • Stop checking state • Stop feeling your needs • Stop expressing your needs • Stop validation
  9. Coping mechanisms • Stop checking state → Do not listen

    to your body, stop feeling (anger, ..) • Stop feeling your needs → Go on, drain your battery, exhaust yourself • Stop expressing your needs → „I‘m not worth it“ / „I am too much“ • Stop objective validation → „It‘s my fault“ / „I must not do this“ These mechanisms are being incorporated into your code base
  10. How does this change my code base?  Coping mechanisms

    intercept your main code  Unconcious  High-Priority EventListeners that kick in before/during the „main“ process (concious decissions)  High performance, impressive response time (react before you think)  Hard to debug
  11. class DoItForMumRequestSubscriber implements EventSubscriberInterface { public function onKernelRequest(RequestEvent $event): void

    { $request = $event->getRequest(); $requestor = $request->getRequestor(); if ($requestor->isFemale()) { // skip validation, mum would not like it and be sad, see logs1978.tar.gz to logs1983.tar.gz $response = new YesSureIllDoItResponse('Sure'); $event->setResponse($response); } } public static function getSubscribedEvents(): array {…} }
  12. class DontHurtThemSubscriber implements EventSubscriberInterface { public function onKernelException(ExceptionEvent $event): void

    { $exception = $event->getThrowable(); if ($exception instanceof NoIDontWantThisException) { $request = $event->getRequest(); $requestor = $request->getRequestor(); if ($requestor->mightNotLike($exception)) { // stop handling exception, do not hurt their feelings, they will stop loving you continue_execution_after_validation(); // magic method I invented myself } } } public static function getSubscribedEvents(): array {…} }
  13. class WhyBotherSubscriber implements EventSubscriberInterface { public function __construct(private EventDispatcher $dispatcher)

    {} public function onExpressNeedEvent(ExpressNeedEvent $event): void { $need = $event->getNeed(); if ($need->isThirsty()) { // dont bother asking for a glass of water, you're not worth anyone's trouble $this->dispatcher->dispatch(new GetUpEvent()); // get it yourself $this->dispatcher->dispatch(new GetWaterEvent()); } }
  14. How can we change this?  Xdebug  Set a

    breakpoint – become aware when your EventListeners kick in  Acknowledge, don‘t blame (yourself, parents, partner, colleagues, …)  Become aware of better solution  Discuss – get in contact and show yourself  Improve says:
  15. „Bad“ EventListener Construction Kit Triggers „Exceptions“ (Warning signs) Requests Needs

    Conditions Who is your contact (spouse, parents, manager, people in need) Validation Is this really good for me? Am I worth it? Is this a valid need? Response / Action Say „yes“ even if you should not
  16. „Bad“ EventListener Construction Kit Triggers „Exceptions“ (Warning signs) Requests Needs

    … Conditions Who is your contact (spouse, parents, manager, people in need) … Validation Is this really good for me? Am I worth it? Is this a valid need? … Response / Action Say „yes“ even if you should not … • Find a class name for the listener • Define event / event class • Draft code Where does it come from? Purpose? Fix?
  17. Sounds silly?  It works  I makes „talking about

    it“ easier  Biggest learning: We are all in the same boat #catcontent #wtfpowerpoint
  18. „If you don‘t understand it, it might be unnecessary nonsense“

    „Wenn Du‘s nicht verstehst, isses vielleicht einfach Quatsch“
  19. Find out your old codes purpose Figure out what that

    code did – and what ist intentions were (what it protected you from) Become aware that you, as a grown up, might not need that protection anymore
  20. Only you can change it  It has been written

    by you to adjust to your environment  It is your code base – and you are the lead dev  You CAN change it
  21. Might not work on your own Awareness can heal Show

    yourself Check Mental Health offers at your job You might not need „therapy“ – a therapist can help though
  22. …to leave you with a Positive Mindset Everyone is running

    the best code they have Once you spend time on learning new things and refactor your code, performance and maintainability will get better
  23. Feedback please, please, pretty please  [email protected]  @xosofox 

    Talk to me I did quite a lot of work on myself, I can take your feedback :-D