loading...

If Statement Golf: what's the longest one you've seen?

jsn1nj4 profile image JSn1nj4β€β€πŸ‘¨β€πŸ’» ・2 min read

Let's play a little game. Who has seen the most ridiculous set of conditions within the parentheses of an if statement?

Trigger Warning 😑

Before commenting, please know I understand there are developers out there who hate if statements with a passion. If you are such a developer, please stay away to avoid needing heart medication and/or a psychological examination.

Now, getting back to our little game...

The rules πŸ“–

  • Only what's within a single set of outer parentheses of an if statement counts
    • JS example: if ( condition ) {
  • If parentheses aren't required for your language of choice, then everything after the if and before the terminating character counts
    • e.g., BASH if statements wrap individual conditions in sets of parentheses or brackets, so they also count.
    • if [ -r $1 ] && [ -s $1 ]; then
  • You can't count multiple sets of statements from something like an if...elseif...else. In cases like those, only use the set of conditions from a single if or elseif.
  • In this case, I'm not counting multiple parts joined by conditional operators as a single condition. Everything between conditional operators is counted individually.
    • This refers to operators such as and, or, ||, && and any others your language uses.
  • The character count I'm giving is exact because it's in front of me. If you can't pull it up immediately, it's ok to estimate. But please do estimate as close as you can.
One last note πŸ“

Honestly, I'm not sure if I'm using the word "condition" incorrectly here. Should I be using the word "expression" instead? This part of dev grammar is a little fuzzy to me.

With all of that out of the way, let the brain hemorrhaging commence. 😡🧠

Discussion

markdown guide
 

I have this "little gem" to share :P

9 conditions and 203 characters long

if ( $hasNoDate || $hasNoDuration || $isPlannedWithoutProgress || $hasNoTask || $isConsolidated || $noTotalTaskWork || $notItemProgressIsOverLimit || $itemProgressIsOverLimit || $targetTaskIsMilestone)

It's used as a validation inside a foreach loop, each statement is a "challenge" that different attributes of the element currently in the loop have to pass, some are a simple bool cast of an atributte, others are longer comparisons or function returns.

It's original format was WAY LONGER :(

 

Thanks God of Binary Operation for inventing bit sets. I think this expression would clearly be simplified by packet bits here.

 

Still at least readable, and using only variables.

 

My entry: 18 conditions and 619 characters long.

I'm also including the actual statement, for anyone curious. I've added line breaks in some places to make it somewhat easier to read, but it was originally all on 1 line.

if ($FBtype == 'status' && $FBmessage == '' && $FBstory == ''
  || $FBtype == 'event'
  || $FBtype == 'event' && strpos($FBstory, 'shared their event') !== false
  || $FBtype == 'status' && strpos($FBstory, 'changed the name of the event to') !== false
  || $FBtype == 'status' && strpos($FBstory, 'changed the privacy setting') !== false
  || $FBtype == 'status' && strpos($FBstory, 'an admin of the group') !== false
  || $FBtype == 'status' && strpos($FBstory, 'created the group') !== false
  || $FBtype == 'status' && strpos($FBstory, 'added an event') !== false
  || $FBtype == 'event' && strpos($FBstory, 'added an event') !== false)
 

The first version of my if statement looked very similar to that, until I started to combine some of the comparisons into single variables, made it less intimidating almost instantly :)

 

Holy jesus, have mercy on that dev...

 

Recently refactored this

else if (ue && customType != tok!"" && ue.unaryExpression && ue.unaryExpression.primaryExpression &&
                ue.unaryExpression.primaryExpression.identifierOrTemplateInstance &&
                ue.unaryExpression.primaryExpression.identifierOrTemplateInstance.identifier == customType &&
                ue.identifierOrTemplateInstance && ue.identifierOrTemplateInstance.identifier.text == "init")

into

else if (const IdentifierOrTemplateInstance iot = safeAccess(ue)
                .unaryExpression.primaryExpression.identifierOrTemplateInstance)

There was not that much conditions but since the ?. operator missed (D language) we had to check nested member against null to reach safely the end of the chain. Now the same is done with metaprograming magic.

 

I have something to admit: my entry isn't actually my code. It's just the code that gave me the idea for this post (although I'll also admit I've written a few bad-looking if statements myself).

I actually posted this because I figured it could possibly be a fun thing to see some crazy if statements (for those of us that aren't outraged by seeing the word if, anyway).

I'll also admit that coming up with writing ideas isn't something I'm terribly good at.

On that last note, thanks for 2 ideas in 1! (posts for motivation, and posts for sharing our own code that we need to learn from)

Edit: In your case, it's nice that you put everything inside of a function. That at least reduces clutter, and the function can be named something meaningful.

 

I don't know if this counts, but this is a multi-nested CASE statement in one of my dynamic SQL queries. CASE acts as a if-then-else statement.

case 
    when LIST_TYPE = 1 
    then 
        case when NEW_TAB = 0 
             then apex_util.prepare_url('f?p=' || 'INSITE' || ':10000:' || :APP_SESSION || '::::P0_MENU_ID:' ||  MENU_ID)
             else 'javascript:apex.navigation.openInNewWindow(' || chr(39) || utl_url.unescape(LINK_TARGET) || chr(39) || ')'
        end
    when LIST_TYPE = 2 
    then 
        case 
            when NEW_TAB = 0 
            then apex_util.prepare_url('f?p=' || APP_ALIAS || ':' || LINK_TARGET || ':' || :APP_SESSION)
            else 'javascript:apex.navigation.openInNewWindow(' || chr(39) || 'f?p=' || APP_ALIAS || ':' || LINK_TARGET || ':' || :APP_SESSION || chr(39) || ')'
        end
    when LIST_TYPE = 3
    then apex_util.prepare_url('f?p=' || 'INSITE' || ':10000:' || :APP_SESSION || ':' || 'APPLICATION_PROCESS=GET_FILE:::P0_MENU_ID:' || MENU_ID)
    else null
end as LINK_TARGET, 
 

I was actually going to see if I could refactor it, but I'm not exactly a SQL pro (see what I did there?)

Thanks for sharing though! Even gave me something new to look at (SQL cases).

 

Mine is not that horrible and terrifying I guess:

if ((this.videoPlayer.currentTime >= action.start) && (this.videoPlayer.currentTime <= action.end) && !action['readmore']) {

I always try to minimize the code, otherwise it's hard to follow, and even easier to miss some value and break your head about :p