[Rephrased]: The Importance of Proper Procedure.


  • [EDIT] As stated by @Link150, the word Algorithm does not fit in this context. Thus, I have changed it to a Procedure which are just steps to solution.

    Procedures are one of the most powerful tools a programmer has in their disposal. However, not many people understand and utilize this powerful step.

    What are Procedures?

    Procedures is the instructions or solution steps for a particular problem, used to solve that problem efficiently. Efficiently is a key word here. Of course I can create a small program without using small procedures. However, chances are, there could potentially be a problem in the code. In a situations like this, a logical procedurereally save the day.

    How to use Proper Procedures?

    Let's say I am creating anArtificial Intelligent Non-Player Character or in short AI NPC. Now, I want the NPC to run some errands for me. I want it to the following tasks.

    • Go to Mall.
    • Buy Grocery.
    • Return Home.

    Now, the above list is an algorithm, if you hadn't noticed it already. However, it is very vague. Let's break it down more and more.

    • Go to Mall.
      • Walk from Home.
      • Find a means of transportation.
      • Use that transportation.
      • Get off at the destination.
      • Get inside.
    • Buy Grocery.
      • Find food store.
      • Find items in list.
      • Go to the cashier.
      • Pay for food.
      • Leave the store.
    • Return Home.
      • Leave the Mall.
      • Find a means of transportation, preferable the same mean as before.
      • Get of at the destination.
      • Get inside.

    Whew. That is a bit better than the first one we had. It has steps inside the bigger steps. But wait, we are not done yet. Not even close! Re-read the steps we created. What do you notice? There are many flaws in the procedure we created. Here are some possible questions that is raised.

    • What happens if NPC cannot find the means of transportation?
    • What happens if NPC cannot find the store in the mall?
    • What happens if NPC cannot find the food written in the list?
    • What happens if NPC does not have enough money?.
      and so on. I am hoping you get the idea

    These are what we call bugs in the code. They play a vital role in creating a perfect procedures. Let's update our procedures.

    • Go to Mall.
      • Walk from Home.
      • Find a means of transportation.
        • If no transportation found then hitchhike.
      • Use that transportation.
      • Get off at the destination.
      • Get inside.
    • Buy Grocery.
      • Find food store.
        • If no food store found then leave.
      • Find items in list.
        • If no items found then leave.
        • If particular item not found then skip item.
      • Go to the cashier.
        • If cashier's path is full then go to next cashier.
        • If particular cashier is full then wait in line.
        • Wait for the turn.
      • Pay for food.
        • If no money then apologize, leave.
        • if short on money then tell situation.
      • Leave the store.
    • Return Home.
      • Leave the Mall.
      • Find a means of transportation, preferable the same mean as before.
        • If no transportation found then hitchhike.
      • Get of at the destination.
      • Get inside.

    Now the cool thing about writing procedures is that it keeps playing around in your head. Of course this procedure is still not complete. It still has many flaws. But it is a lot better than what we had begun with. And this is why steps are essential in programming, especially when create a major programs. You want to program to be bug free. You cannot do everything on top of your head.

    Of course, some people might not have a need for writing procedures. I understand that. If I wanted to create a quick small programs as well, I tend not to spend time creating procedures. However, if it is a major task you are working on, I highly recommend you take some time to create a working procedure.

    That is all I have for now. I might create another article, depends on my mode. I am upset as Germany broke my World Cup Dreams and need some time to process it. :P


  • Please post more on algorithms. I really enjoyed reading this. While I know what algorithms are and how they work in theory, somehow I don't know how to apply them in scripting. I found this really helpful and I hope to learn more if you are willing to write more. So please do.


  • Your article is quite abstract. Would you consider posting an additional example in Roblox Lua so we can get better context of how we can apply it in our code.


  • @Phlegethon5778 and @LateralLace Yes. I do plan on creating a more focused tutorial on Algorithms. Not today as I am pretty busy.


  • @LateralLace

    function makeNpcDoStuff()
        local storePart = game.Workspace.Store.HumanoidWTP
        script.Parent.Humanoid.WalkToPart = storePart
        -- above lines define the part to make humanoid walk to,
        -- and make humanoid walk to the part 
        
    end
    
    makeNpcDoStuff()
    
    --Obviously just an example  
    
  • Global Moderator

    I wouldn't call that an algorithm.


  • @Zafirua

    Great post, even if it was posted quite some days ago xD

    It's explained in a way it is easy for beginners to understand.


  • @Link150 Of course the example is meant to be basic.

    The general definition of Algorithm include:

    Of Course, the procedures, if that is the better word to use, are very vague. And it was meant to be. Its main purpose was to give a general idea on what and how of the steps.

    If you could provide on why the word "algorithm" should not be used here, then it would be very helpful. I am not sure on how the word doesn't work in this context when clearly, it is just a set of instructions. Is there a definition I don't know about it? This was what my teacher had taught me and also what I learnt from the definitions I found in Internet.

    Looking forward for your answer.


  • wouldn't all functions be algorithms? They are all sets of instructions to achieve a result. Depends when you are you using selection/iteration though.

  • Global Moderator

    @Zafirua While it is true that algorithms are defined as "A process or set of rules to be followed" to reach a certain goal, this wouldn't fall under "algorithms".

    Algorithms are more about the little things, like sorting arrays, compressing text, or searching for a value in a list. What you explained here is really just how to break down code into smaller and smaller procedures, and then proceeded to explain edge-cases and how your program can get into an invalid state if you do not carefully handle them.

    Edge-cases are not "bugs", by the way. A bug is a problem introduced by a programmer mistake and which causes unexpected behavior. An edge-case is not a bug itself, although failing to acknowledge and properly handle them can introduce bugs.

    If you really wanted to give a tutorial in algorithms then you'd have to talk about algorithm complexity -- notably space and time complexity -- and Big-O notation. You would present different algorithms to the reader, observing them and explaining the reasoning behind their runtime complexity, and consequently explain why some solutions would be better than others.

    Any self-respecting algorithm tutorial/course will address Big-O notation and an algorithm's runtime complexity. Algorithms also go hand in hand with data structures, and a good algorithm tutorial would probably address about them as well.

    Well, that's all I have time for today. I wish I could continue this conversation, but I'm at work currently, and my shift is about to start. I may decide to write up a tutorial one day, who knows. But writing good tutorials would be a huge undertaking, and with work and all, I don't have much time for that. When I come home from work there's only one thing I want to do, and it is to relax and play video-games. Anyway, hope that answers your question. :)


  • @Link150 thanks a lot for your answer. I will update the title so there wont be a misunderstanding in future for other readers

Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.