Search Unity

Modularization Code Examples? Unity Best Practice

Discussion in 'Scripting' started by resetme, Sep 1, 2015.

  1. resetme

    resetme

    Joined:
    Jun 27, 2012
    Posts:
    204
    Hi guys, i was reading the Unity Best practice and the section about Modularization is kinda difficult to understand.

    They explain how to convert this code;
    switch( behavior )
    {
    case 1: // some behavior
    case 2: // another behavior case 3: // yet another behavior case 4: // the last behavior}

    MyClass.behavior = someBehavior;

    link: https://unity3d.com/learn/tutorials/modules/intermediate/scripting/coding-practices

    in a Modular code.
    But i don't get it, lol.

    Can some expert explain it to me with an example code, or with apples?

    Thanks!!
     
    make-no-mistake likes this.
  2. Azmar

    Azmar

    Joined:
    Feb 23, 2015
    Posts:
    246
    Pretty much they are saying the case example is a horrible example that needs to be "modularized", the way they explain it is still confusing and not a very good solution. Pretty much if you feel like your function is getting too big or it does too many things it is a good time to modularize it into smaller functions. A good rule of thumb is to just make 1 function do 1 task and it should solve all future problems :)

    I hope that helped...
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This video kind of skirts around the issue. Its not exactly a treatment of modularisation. But it touches on some of the same ideas.



    Basic idea is that each class should be responsible for one thing, and one thing only. You can then build up your functionality by combining different classes. Along with this behaviours should not extend through multiple classes, they should be contained within a narrowly defined set of classes.

    The basic idea is you should be able to separate your code into distinct modules. You should then be able to pull out each module in isolation without breaking the rest of the code. Complex behaviour is built by combining various modules, rather then by making each module more complex.
     
    rbitard and georetro like this.