Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Should I use my own code instead of other people's code?

Discussion in 'General Discussion' started by GTHell, May 9, 2016.

  1. GTHell

    GTHell

    Joined:
    Jan 5, 2016
    Posts:
    256
    Hi everyone,

    I've been trying to get into Unity 3d but it seem it's harder to teach yourself how to play a guitar.
    I've been watching countless tutorial and example video still can't get well with the Unity 3d tools and the scripting.

    Let get to the point. So I want to make an object move and the the code I usually see is:

    Code (CSharp):
    1. float S = Input.GetAxis ("Horizontal");
    but I always has my own style and I use something like this:

    Code (CSharp):
    1. if(Input == w)
    2. objec.moveUp    // Sorry I forgot all the API code because I haven't use it for 1 month but I'm sure you understand my point.
    Is it has conflict with performance or is it the same thing?

    I just can't learn Unity 3D well. I teach myself C# and solving a lot of hard problem and I can say that it's still easier to get into compare to Unity 3D.

    Sorry for my English. I've been inactive in using Unity 3D for almost 1-2 month and I forgot everything.
    I want to build a habit that I can do it well instead of remember everything I've seen.
     
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Your code doesn't take analog joysticks into account. There's no way to move a little bit up, you're either moving full-speed or not moving at all. Even non-analog controllers get a little tweening from Unity, IIRC.
     
  3. GTHell

    GTHell

    Joined:
    Jan 5, 2016
    Posts:
    256
    I can tweak that. I hope you get my point. It wasn't the code but what I mean is are the other alternative code work the same? Or only
    Code (CSharp):
    1. Input.GetAxis ();
    will be need in all case scenario?
     
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,148
    Performance doesn't matter if you're only doing it once or twice. That said those two code snippets are not the same thing.
     
  5. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    Hard to say when it all depends of the other code you are comparing to which in your example did not even work
     
  6. GTHell

    GTHell

    Joined:
    Jan 5, 2016
    Posts:
    256
    I know that it's not going to work because I'm writing an Algorithm instead of a workable code!
    If you get the point then you will be able to understand what I wanted to ask.

    Maybe this thread can get me back to learn Unity 3D again. I'm not smart but at least I'm a try hard person.
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,148
    An algorithm is workable code. The definition is literally a "process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer". If your code isn't working, it means your algorithm isn't working.
     
  8. GTHell

    GTHell

    Joined:
    Jan 5, 2016
    Posts:
    256
    Code (CSharp):
    1. string Name = Console.Readline();
    2. int ConName = Convert.ToInt32(Name);
    and
    Code (CSharp):
    1. int Name = int.parse (Console.ReadLine());
    Is it the same? and if not then why?
     
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,148
  10. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    Again, now even if the code works your examples do not match. You are using parse in another and convert in another.
     
  11. GTHell

    GTHell

    Joined:
    Jan 5, 2016
    Posts:
    256
    Algorithm is a problem solving step following by a flowchart. (Translated)

    My algorithm should be work but the code snippet that I gave might not be but please try undertand it and get to the point..
     
  12. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,148
    I'm not the one having trouble understanding here.
     
  13. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    If the result is what you want, it doesn't matter how you got there.
     
    theANMATOR2b likes this.
  14. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    There is no point if you are trying to compare different functions which can be the same in the end and sometimes not. In some cases it does not matter, in some cases the other one can be slower. It all depends what you are actually doing.

    There is no generic answer.
     
  15. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,148
    It's just a shame you're focusing so hard on the points that don't actually matter. Don't worry about optimizing your code before you've even written any. Go build your game and then go back and determine what you need to improve. Chances are it won't be the portions you actually think are slow.
     
    Trexug and Kiwasi like this.
  16. GTHell

    GTHell

    Joined:
    Jan 5, 2016
    Posts:
    256
    If that so then I will continue doing thing my own way.

    How do you know that the thing you're doing is right or wrong? What is you trying to experiment thing and you don't know what you doing it right or wrong?
     
  17. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    If the question is if is a performance issue or problem if break things up into steps or combine them, then no, it isn't a significant issue. You are fine breaking them into separate lines if you prefer and it is easier to read for you.
     
    Kiwasi likes this.
  18. GTHell

    GTHell

    Joined:
    Jan 5, 2016
    Posts:
    256
    Sometime it just piss me off that I always do thing differently from other people. I remember quiting learning guitar for 2 week because I can't do the barre chord. I just try and try to get pass that and thing become easier.
     
  19. GTHell

    GTHell

    Joined:
    Jan 5, 2016
    Posts:
    256
    Yeah, It's easier to read and understand. I sometime get confuse with other people's code that why I always do thing my own way.
     
  20. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,148
    Ostwind likes this.
  21. Taschenschieber

    Taschenschieber

    Joined:
    Jun 8, 2014
    Posts:
    238
    Also, there is a documentation that tells you how things work. Reading, understanding and applying it is arguably the most important skill for a software developer.

    "Algorithm is a problem solving step following by a flowchart."

    No. An algorithm is a sequence of actions that solve a particular category of problems. An algorithm can be described through a flowchart, or it can be described with code, or with pseudocode, or in plain English.
     
    Kiwasi and Ryiah like this.
  22. HemiMG

    HemiMG

    Joined:
    Jan 17, 2014
    Posts:
    911
    You seem to be stuck trying to run before you've learned how to walk. Don't get caught up in these things yet. To give a baseball metaphor, a pitcher may want to know when to use a fastball, curveball, or knuckleball. But none of that matters if he hasn't learned to get the ball across the plate yet.

    Your first few projects won't be worth anything commercially, just as a pitcher who has never thrown a ball before won't get hired onto a pro team. Focus on completing the projects and as you go you'll learn what you need to know. Even if your goal isn't commercial, your first few projects are still going to largely be throwaways. It's just the nature of learning anything.
     
    landon912, Kiwasi and Ryiah like this.
  23. Acissathar

    Acissathar

    Joined:
    Jun 24, 2011
    Posts:
    669
    From what I can tell only @zombiegorilla understood what the OP was trying to ask.

    Essentially what I'm getting is that OP is asking if it is okay for them to write code like this:
    instead of putting it all in one line like this:
    And like ZombieGorilla said, if it is easier for you to break the lines apart then by all means go ahead. The main thing you want to ensure you do is be consistent with whatever you do.
     
    Kiwasi and zombiegorilla like this.
  24. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,148
    @Acissathar: Except that his code examples weren't simply formatted differently. If he were simply separating his code across multiple lines that would have been one thing. But the code examples he gave don't even function identically.
     
  25. Acissathar

    Acissathar

    Joined:
    Jun 24, 2011
    Posts:
    669
    Right, but given that OP doesn't seem to be a native English speaker and that they specifically stated "I know that it's not going to work because I'm writing an Algorithm instead of a workable code!" It looks like most people took the code literally and missed what OP was asking.
     
  26. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    I don't think he can use the profiler to worry about performance if there's no code to profile :p

    In all seriousness though, just get in and code something. Spend less time worrying about if its or right or wrong and just try it
     
    Ryiah and Kiwasi like this.
  27. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Code is right if it does the job reliably enough. Is performant enough. Is readable enough. Is maintainable enough. Is extendible enough.

    Enough depends on the exact context. Reliable in my day job is one failure every hundred years. On a game that might be works on 90% of customer devices.
     
    Ryiah likes this.
  28. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,328
    Well, by default everything people do while programming is wrong.

    Alright, I'm kidding.

    You know what impacts performance by using profilers. You measure performance, locate bottlenecks and exterminate them.

    ----

    Speaking of "right and wrong", the idea of "right" is not very useful, because you're balancing several factors.

    1. Code performance.
    2. Code development cost (time + money).
    3. Code maintenance cost (time + money).
    4. Your own happiness, salary and sanity.
    5. Deadlines.

    After that it turns into rock paper scissors from hell, because the factors affect each other.

    rock paper scissors.jpg

    At the moment you've written two lines of code and are worrying about style. It is a bit too early for that.
     
    Kiwasi likes this.
  29. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    learning to play guitar or any stringed instrument is actually easy once your finger tips toughen past the painful stage of beginning fretting.
     
  30. TylerPerry

    TylerPerry

    Joined:
    May 29, 2011
    Posts:
    5,577
    The code below takes the axis "Horizontal" (A value from -1.0 to 1.0) and assigns it to S, if you press right it will be 1.0 if you press left it will be -1.0.
    Code (CSharp):
    1. float S = Input.GetAxis ("Horizontal");
    The code below checks if the W key is currently pressed and returns true if that's correct or false if it's not. A function is called if it is.
    Code (CSharp):
    1. if(Input == w)
    2. objec.moveUp    // Sorry I forgot all the API code because I haven't use it for 1 month but I'm sure you understand my point.
    In some situations these could give identical results but in most the top one gives a non boolean value and allows for greater control. It's also not controlled by the key W and could be used with any two keys or axis.
     
  31. Meredoc

    Meredoc

    Joined:
    Mar 9, 2016
    Posts:
    21
    When I used to code (in goode olde C), I made the youthful mistake to try optimising stuff from step A.
    Total mistake - you won't be able to read your code in the end, since it's just getting more and more convoluted.

    If I'd restart again, I'd do a systematic repeat of every initialisation in exactly the same way, do every loop and if case in a structured way to recognise what's going on in every situation. I'd throw comments on like a maniac.
    But I'd rather be programming in a visual language nowadays, frankly.

    Forget about performance - it's totally unimportant compared to finishing off the codebase in a consistent manner.