Thread: Can you convert this code to not use an IF statement?

Page 3 of 3 FirstFirst 123
Results 21 to 29 of 29
  1. #21  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Quote Originally Posted by Savions View Post
    100% agree

    this thread is nonsense, either ur senior dev is stupid or u didn't understand his point properly, but avoiding conditional checks for a performance gain, which in most cases (see OP's self called 'excellent solution' code for example) will only make things worse
    I'm pretty sure you must of skipped part where I mentioned that it isn't something I would personally do, but the point was to show that anti-ifers do exist and the rest of this thread is more of a coding challenge. Hence why everyone is getting creative.

    As for my example, the use of polymorphism provides a direct application flow over a conditional one hence why I referred it to excellent. But everybody on this community knows that using an IF is better.
    Reply With Quote  
     

  2. #22  
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,227
    Thanks given
    3,283
    Thanks received
    2,875
    Rep Power
    5000
    I'm pretty sure the "anti-iffers" in reference are not against using conditionals, rather strive to write more generic code outside of their business logic, which is something a lot of amateur developers fall victim to. It just boils down to more fluid design.

    Developing any application without conditional checks is borderline psychotic.
    Reply With Quote  
     

  3. Thankful users:


  4. #23  
    Banned
    Join Date
    Mar 2021
    Posts
    29
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    You could write it like this

    *� X� =*� X� t6*� Z� t6**� X� x�
    Reply With Quote  
     

  5. #24  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Quote Originally Posted by Mister Maggot View Post
    I'm pretty sure the "anti-iffers" in reference are not against using conditionals, rather strive to write more generic code outside of their business logic, which is something a lot of amateur developers fall victim to. It just boils down to more fluid design.

    Developing any application without conditional checks is borderline psychotic.
    That's partially right however, if's are conditional statements and there are people who are very strong against conditionals in general although the people that hate GOTO's are much more common.

    At this point though, this thread was just ended up being more of a challenge.
    Reply With Quote  
     

  6. #25  
    BoomScape #1
    BoomScape's Avatar
    Join Date
    May 2013
    Posts
    2,422
    Thanks given
    289
    Thanks received
    234
    Rep Power
    48
    Code:
    ((input > 10) && !(handleHigh())) || (handleLow())
    Attached image
    Reply With Quote  
     

  7. #26  
    (Official) Thanksgiver

    Arham's Avatar
    Join Date
    Jan 2013
    Age
    23
    Posts
    3,415
    Thanks given
    7,254
    Thanks received
    1,938
    Rep Power
    3905
    Quote Originally Posted by BoomScape View Post
    Code:
    ((input > 10) && !(handleHigh())) || (handleLow())
    He said no conditionals. Also he never said handleHigh or handleLow return boolean types.
    Attached image
    Attached image
    Quote Originally Posted by MrClassic View Post
    Arham is the official thanker!
    List of my work here!
    Reply With Quote  
     

  8. #27  
    Respected Member


    Join Date
    Jul 2015
    Posts
    781
    Thanks given
    206
    Thanks received
    394
    Rep Power
    524
    yeah surely he's just generally referring to using something like strategy pattern. conditionals of some sort are unavoidable

    Code:
    val highValueStrategy = { num: Int -> println("high number is $num") }
    val lowValueStrategy = { num: Int -> println("low number is $num") }
    
    object UserInputStrategyFactory {
        fun getStrategy(value: Int) = when {
            value > 10 -> highValueStrategy
            else -> lowValueStrategy
        }
    }
    
    fun getUserInput() = (9..11).random()
    
    fun main() {
        val input = getUserInput()
        val strategy = UserInputStrategyFactory.getStrategy(input)
        strategy.invoke(input)
    }
    Reply With Quote  
     

  9. Thankful user:


  10. #28  
    Registered Member
    Velocity's Avatar
    Join Date
    Jan 2009
    Age
    28
    Posts
    2,028
    Thanks given
    1,013
    Thanks received
    2,376
    Rep Power
    4112
    that senior developer sounds like the kind of 70 year old bloke to be in the c++ committe voting against std::embed
    xxxxxxx
    Reply With Quote  
     

  11. Thankful users:


  12. #29  
    Registered Member

    Join Date
    Dec 2009
    Posts
    774
    Thanks given
    367
    Thanks received
    455
    Rep Power
    927
    You didn't specify a programming language, so here's a Haskell solution
    Code:
    handle 1 = handleLow 1
    handle 2 = handleLow 2
    handle 3 = handleLow 3
    handle 4 = handleLow 4
    handle 5 = handleLow 5
    handle 6 = handleLow 6
    handle 7 = handleLow 7
    handle 8 = handleLow 8
    handle 9 = handleLow 9
    handle i = handleHigh i
    link removed
    Reply With Quote  
     

  13. Thankful user:


Page 3 of 3 FirstFirst 123

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Convert this code to PI
    By Eclipser in forum Help
    Replies: 7
    Last Post: 06-29-2012, 12:35 AM
  2. Replies: 1
    Last Post: 12-07-2011, 03:34 AM
  3. Replies: 4
    Last Post: 06-22-2010, 09:07 PM
  4. Can someone explain this code to me?
    By jordan641 in forum Help
    Replies: 3
    Last Post: 04-24-2010, 07:52 AM
  5. Can anyone convert this to espeon (508) Repp++
    By massacre215 in forum Requests
    Replies: 2
    Last Post: 08-02-2009, 01:39 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •