Tutorial Correct use of If Statements

Discussion in 'Tutorials & Resources' started by goody599, Jun 23, 2019.

  1. goody599

    Joined:
    Nov 13, 2015
    Messages:
    11
    Likes Received:
    2
    It's been a long time but I had to speak up as I constantly see people having issues and issues with overheads especially in their bots and the exact same issues and same mistakes keep popping up at least from my view and the code snippets they provide.

    You might not know me but ive been botting in OSRS for close to 4 years now. I constantly use other peoples bots not only to see how they create "player sense" but I mainly use my own as if you want something done its best you make it yourself and the main computer I run my bots on is to be honest shit, thus as a result any bot I run on it has to be optimized to create the lowest possible overhead.

    When to use multiple if statements and when to use if elif else statements. Note this is a code snippet taken from another websites bot and parts have been taken out for other reasons.

    When you write code like this:

    If (Me.Distance.Other < 5){
    Logout();

    }

    if (Me.Distance.Other > 5 && Me.Distance.Other < 15 ){

    Hover.Logout();
    }

    if (Me.Distance.Other > 15 ){

    PLayerList.Add("PlayerName");

    }

    if (PLayerList.Size > 0){
    //Not disclosing my methods ;D

    }

    Essentially with this structure it goes through every single if statement and we dont actually want that as not only does it create more lines of code that have to read but also it creates that awful overhead that crash's some peoples computers but its just not the correct way to write efficient code and in the real world (if you really like coding ) it just wont be accepted by any code reviews as it is not optimized at all.

    A more approiate way to actually reduce these if statements would be something like the following which utilizes the else if type of if statements

    if (Me.Distance.Other < 15){
    Hover.Logout();
    PLayerList.Add("PLayerName");
    if (Me.Distance.Other < 5){
    Logout();
    }

    Else if (Me.Distance.Other >= 15){
    PlayerList.Add("PlayerName");
    if (PLayerList.Contains.PlayerName > 5){
    Logout();
    }
    }

    Else{
    PlayerList.Add("PlayerName");
    }


    }

    When using nested if statements if the very first if statement dosnt fire then the if statement inside that if statement will never be reached and not run, thus reducing the number of lines that the computer actually has to read and compute. For example if the distance of a player is not < 15 the computer will never read if < 5, reducing the lines however while using this method it still ensures that when a player is to close it takes the appropriate ways as well as it allows us to categorize the player and see if they are a possible Pker or simple a scout as we know when they are going in for us or if they are scouting us and we simply do this by having 2 different lists and running a simple combination script bot looking for the preset pattern we made.

    Essentially dont just use if statements, they really hurt people with shit computers, the theory of elif statements isnt to complex and it really saves users and its not hard to get around unreachable code when debugging with breakpoints or doing simple step through methods.

    A more simple version is found on StackOverflow - if/else and if/elseif

    Also if your interested in machine learning in OSRS i will hopefully be releasing my first bot which is currently after 2 months and after 360 hours of gameplay is still undetected with no strikes with doing only fletching and cooking for the entire 360 hours. If your interested in how it works DM me.
     
    #1 goody599, Jun 23, 2019
    Last edited: Jun 24, 2019
  2. Savior

    Savior Java Warlord

    Joined:
    Nov 17, 2014
    Messages:
    4,852
    Likes Received:
    2,707
    If statements themselves dont add any real overhead whatsoever. The real cause of unoptimized bots is not reusing already processed data.
    For example you should calculate the distance once and save it in a variable, instead of calculating it 3 times in a row for the same block of code.

    Doesnt mean that you shouldnt care about an optimized controlflow though, in fact it can solve other problems such as sticking to one action per loop.
    --- Double Post Merged, Jun 24, 2019, Original Post Date: Jun 23, 2019 ---
    Also you picked a bad example, the original code is perfectly fine and only 1 of those if statements can be executed anyway. Whereas your version has side effects.
     
    Derk and Defeat3d like this.
  3. goody599

    Joined:
    Nov 13, 2015
    Messages:
    11
    Likes Received:
    2
    What I was trying to point out is that I see a lot of people choosing only if statements and not having and nested if elseif else statements in their code to the point where its 100's of just if statements which was why i went over if statements as so many people doing use them.

    The code example I used like I said wasnt from any of my bots and if it was you'd see primarily inheritance and a lot of polymorphism going on as well as I prefer to use linkedlists over normal lists especially in this case due to the constant updating. Nevertheless the me.distance.other is a variable its just how I prefer to have my naming conventions when writing sudo code and I can tell from experience that it does affect overhead greatly when considering a poor computer vs a good computer which was the main point of the post.

    As well as the original code is shocking. Runemate occassionally spits out the wrong value especially when calculating distance as with almost any client and you want to have some fail safe or a brief update period to verify that the value coming through is actually correct. If you used the code above you would most likely accidentally logout or actually die as well as its much harder to step through code and add alot of advanced playersense aspects into that when you have multiple if statements. I dont know how you do yours but you couldnt accurately or even effceintly implement advanced playersense into code which is just 100's of if statements.
     
  4. Defeat3d

    Defeat3d Prime Celestial

    Joined:
    Oct 30, 2014
    Messages:
    2,028
    Likes Received:
    1,279
  5. RobinPC

    Joined:
    Jun 9, 2015
    Messages:
    2,918
    Likes Received:
    1,081
  6. Savior

    Savior Java Warlord

    Joined:
    Nov 17, 2014
    Messages:
    4,852
    Likes Received:
    2,707
    I wasn't immediately referencing your code, i was just expanding on the example you gave for simplicity.

    I mean this is pretty much irrelevant to the discussion but i dont see how distance calculations could possibly be wrong, assuming you're selecting the correct entities to begin with.

    PlayerSense is merely an API for persisent key-value mapping, how or where you apply it in your code is completely unrelated.


    So again, optimizing the control flow will almost naturally result in less redundant computations, but if statements are inherently not a factor considering how complex other computations are in comparison.
     
    Derk and Defeat3d like this.
  7. Defeat3d

    Defeat3d Prime Celestial

    Joined:
    Oct 30, 2014
    Messages:
    2,028
    Likes Received:
    1,279
    stackedoverflow
     
    Fabreze and Derk like this.
  8. goody599

    Joined:
    Nov 13, 2015
    Messages:
    11
    Likes Received:
    2
    Whenever you calculating something or evaluating some type of value their is always a chance that it can spit out an incorrect value and when you overlook that fact you end up with things going significantly wrong due to that value and you end up as a ignorant programmer in court in the real world when you overlook these facts. In this case its a broken bot less extreme but we don't want that and that's why you have fail safes .

    As well as I don't purely rely off of the player sense API like others do, what I was talking about is using that as well as with other features you build into that, and with 100's of if statements that is next to impossible without re-writing duplicate code over and over again as well as having to overload the exact same method 100's of times and that does cause instability issues overtime. Yes their are more advanced computations but most people who this is aimed at don't actually ever use them correctly and when you put that with their poor basic programming language it stems the overheads and this is because its much easier to optimize a if statement than it is calculations.
     
  9. x3r4

    Joined:
    May 30, 2019
    Messages:
    13
    Likes Received:
    9
    If you want to prove a point, provide evidence. Make some runtime tests comparing "shitty' if statements with your constructs. You're just guessing right now, wouldn't want to be an ignorant programmer in court now would we? :D
     
    Fabreze and Savior like this.
  10. Derk

    Derk 12 year old normie

    Joined:
    Jan 8, 2015
    Messages:
    2,719
    Likes Received:
    1,289
    You're hella clueless brother. 4 years developing bots and you're talking about if statement efficiency?

    Almost everything you say isn't fully correct and you sound like you have to Google what stuff means and then insert words that you think make you sound smart.

    What the fuck does polymorphism have to do with using if statements... Overloading functions does not cause stability issues.... Reusing implementations is actually a good thing to do to avoid rediscovering the wheel....

    And your English is abhorrent.
     
  11. Savior

    Savior Java Warlord

    Joined:
    Nov 17, 2014
    Messages:
    4,852
    Likes Received:
    2,707
  12. Fabreze

    Fabreze #1 Fabric Cleaner

    Joined:
    Mar 18, 2017
    Messages:
    389
    Likes Received:
    102
    Bro what???

    Don’t worry about the distance returning the wrong value, that’s a point of abstraction that is not your area of concern but that of the client developer/whoever created that method in the first place... This will hold up in court as it up to the creator of the method to ensure it works properly. If you build your code around these api abstractions, your code will run absolutely fine due to the very nature of Runemates bot architecture which can handle/correct inaccuracies loop to loop. In addition your code will be guaranteed much more efficient if you take saviors advice and cache complex api calculations instead of “optimizing if statements” and this not only creates more efficient code, it is easier to do as you dont have to mess with any of the already established bot logic, assuming that it works fine already.... you should really at least learn fundamental computer science concepts before spouting nonsense.
     
    #12 Fabreze, Jul 27, 2019
    Last edited: Jul 27, 2019

Share This Page

Loading...