SuitedUpDev, (edited )
@SuitedUpDev@mastodon.online avatar

I'm a big proponent of doing fewer if statements and early returns! Doing stuff in your if statements only increases cognitive load when trying to read the code.

public function sendActivationEmail($user) {
if (! $user->isActivated) {
// Send out activation email
}
}

versus.

public function sendActivationEmail($user) {
if ($user->isActivated) {
return;
}

// Send out activation email
}

waldi,
@waldi@chaos.social avatar

@SuitedUpDev Today I again found a way worse example. Something in line with this:

if(!a() || b() <0) {
error()
}
else if (!c() || !d()) {
error()
}
else {
success()
}

Of course with much longer names and variables over multiple lines.

SuitedUpDev,
@SuitedUpDev@mastodon.online avatar

@waldi there is a special place in hell for that code.....

Joe_C_London,
@Joe_C_London@mastodon.world avatar

@SuitedUpDev I feel like that if statement shouldn't even be in that method, given its name.

SuitedUpDev,
@SuitedUpDev@mastodon.online avatar

@Joe_C_London I completely agree. I happen to find a similar piece of code at work today, which sparked my previous post.

Based on data coming from Salesforce and if Salesforce was considering an account as customer we would basically do this in our codebase;

if ($account->Is_Customer) {
// Create customer
}

Joe_C_London,
@Joe_C_London@mastodon.world avatar

@SuitedUpDev Clear as mud, that is.

SuitedUpDev,
@SuitedUpDev@mastodon.online avatar

@Joe_C_London yes exactly. When I got to work on that piece of code, the first thing I did was refactor it because, oh my .... That was unbearable to interpret.

Alister,
@Alister@mastodon.cloud avatar

@SuitedUpDev I've been a long time fan of these ways to end up with cleaner code. I'd also call it "tell, don't ask" as in "Do something - if it needs to happen".

Its almost like a lot of the refactoring & simplifying code that I do, is extracting functions and inverting conditions (sometimes extracting complex conditionals). Either way, with some decent method names, the end result is so much easier to understand - and you can still dive deeper to get the nitty-gritty.

jamesjm,
@jamesjm@mastodon.social avatar

@SuitedUpDev

Just because this is a pedantic code grammar post….it should be “doing fewer if statements”!

SuitedUpDev,
@SuitedUpDev@mastodon.online avatar

@jamesjm Very sharp, thank you! I've updated it

waffles,
@waffles@masto.yttrx.com avatar
SuitedUpDev,
@SuitedUpDev@mastodon.online avatar

@waffles I love this video! It explains everything sooo well.

objectvoice,
@objectvoice@mastodon.social avatar

@waffles @SuitedUpDev

and once you've dedicated yourself to the religion of nesting only when necessary, there really are some good techniques you discover out of pure stubbornness!

SuitedUpDev,
@SuitedUpDev@mastodon.online avatar

@objectvoice @waffles I'm already a member of that religion. Currently just doing some work for the religion 😉

  • All
  • Subscribed
  • Moderated
  • Favorites
  • php
  • ngwrru68w68
  • rosin
  • GTA5RPClips
  • osvaldo12
  • love
  • Youngstown
  • slotface
  • khanakhh
  • everett
  • kavyap
  • mdbf
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • megavids
  • InstantRegret
  • normalnudes
  • tacticalgear
  • cubers
  • ethstaker
  • modclub
  • cisconetworking
  • Durango
  • anitta
  • Leos
  • tester
  • provamag3
  • JUstTest
  • All magazines