Search Unity

Combine the methods is a good behavior in c# ?

Discussion in 'Scripting' started by Chinard, Sep 11, 2019.

  1. Chinard

    Chinard

    Joined:
    Mar 31, 2018
    Posts:
    9
    Hello,

    I start in C# and i asked me if it was a good thing to combine the methods or it's better to separate the methods ?

    exemple :
    Code (CSharp):
    1. int getWords = go.transform.name.Remove(0, go.transform.name.IndexOf("-") + 1).Trim().Split().Length;
    Or

    Code (CSharp):
    1. nbWordsScene = nbWordsScene + go.transform.name.Remove(0, go.transform.name.IndexOf("-") + 1).Trim().Split().Length;
    Thanks for your answer :)
     
    Last edited: Sep 11, 2019
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,545
    What does "combining methods" mean to you? What does your example have to with combining methods?

    Also, what do mean by "good"? Are you talking about performance? Maintainability?
     
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    306
    Both of your code examples do effectively the same. In one of them you are just saving it in a variable (getWords), while in the other you are adding it to some variable (nbWordsScene). Which of them you do depends on what you want to do.

    Other than that; Code Quality:
    • Following common coding conventions, you should name your variables after what they contain, while naming your functions after what they do. Judging from the name only, i'd assume "getWords" to be a function, not a variable.
    • When you do something like "x = x + y", then "x += y" does the same, but is shorter and easier to read for most people.
     
  4. Chinard

    Chinard

    Joined:
    Mar 31, 2018
    Posts:
    9
    The question has come when i saw that i created one "string array" on a simple "int", even if with the method "length" i had a int.
    it's more linked at that when I talk about if it's good or not to combining methods.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,281
    This again:
    I don't see anything I would consider "combining methods" in anything you've posted. Maybe this is a language translation issue, don't know.


    Combining methods would be something like taking this ridiculous example:

    Code (csharp):
    1. private int whateverValue = 0;
    2.  
    3. private void addToValue(int addition)
    4. {
    5.     whateverValue += addition;
    6. }
    7.  
    8. private void multiplyValue(int multiply)
    9. {
    10.     whateverValue = whateverValue * multiply;
    11. }
    12.  
    13. public void AddAndMultiply(int addition, int multiply)
    14. {
    15.     addToValue(addition);
    16.     multiplyValue(multiply);
    17. }
    And combining the methods to this equally ridiculous example:
    Code (csharp):
    1. private int whateverValue = 0;
    2.  
    3. public void AddAndMultiply(int addition, int multiply)
    4. {
    5.     whateverValue += addition;
    6.     whateverValue = whateverValue * multiply;
    7. }
     
    Last edited: Sep 12, 2019 at 1:30 AM
  6. Chinard

    Chinard

    Joined:
    Mar 31, 2018
    Posts:
    9
    remove() trim() split() is a methods no ?
    in fact i wanted to know if it's more correct to write for the reading by uniy
    Code (CSharp):
    1. int getWords = go.transform.name.Remove(0, go.transform.name.IndexOf("-") + 1).Trim().Split().Length;
    2.  
    or
    Code (CSharp):
    1. string[] getWords = go.transform.name.Remove(0, go.transform.name.IndexOf("-") + 1).Trim().Split()
    2. int nbwords = getWords.Length
    3.  
    it's the same result but we separate the array.
     
  7. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    771
    I think OP is talking about chaining methods at the end of their code examples.

    Code-wise, there's basically no difference. The compiler will take care of these micro-optimizations.
    What really matters is if it's human-readable.
     
    Joe-Censored likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,077
    My position on this sort of multi-chain-dot-dot-dot-statement is always the same.

    When it fails, and it WILL fail, how are you going to figure out where the null reference is? Where are you going to put the breakpoint? Where are you going to print out the intermediate result to see what's actually going on?

    One statement per line thank you very much. The compiler will make it just as fast (and possibly even faster!) than you can with chaining it all together in one line.
     
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    12,020
    And even when it's not failing, once there's nesting involved it's often just easier to read if you split it up. Plenty of programmers will tell you that they don't need it to be easier to read 'cause they can figure it out anyway as if that's some kind of badge of honor, but there's no good reason to make stuff harder than it has to be.

    That doesn't mean that chaining or nesting is always bad. Often it's useful. Just ask yourself "what makes this code easier to work with?" and follow the answer.
     
    bobisgod234 likes this.
  10. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    705
    1. Both are correct, 'correct' meaning that they will compile. We don't know if they do what you want them to do
    2. There is no 'more correct'. It either is or isn't (but I'm a bigger smartass than kurt)
    3. Chaining methods is a grat way to introduce hard to find bugs, and the hallmark of a lazy programmer. As others have observe, the code *will* fail, and then you'll be forced to break it up anyway
    4. "X += Y" is NOT easier to read than X = X + Y (natch, @Yoreki :) )
    5. Shorter source code will not translate into more performant or faster compiled code
     
  11. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    306
    I'd like to add to what others said, in that you generally want to try to keep your lines reasonably short for readability. Smaller examples like "...Trim().Split().Length" may not make things too unreadable, but imagine one or more of these chained functions received a parameter or two, and those were also derived by chaining methods, possibly with parameters too. It gets very hard to read and maintain very quickly. Eventually you'd break the line over multiple lines anyways, just for readability alone, so you might as well split it into different small actions and assign them to their own variables, which makes debugging (as others described above) easier anyways.

    Also
    I'm not entirely sure if this statement is only directed at the fact that "X = X + Y" technically makes more sense when read out aloud, compared to "X += Y" (even tho, both depend on the readers' previous knowledge. One just assumes more common knowledge, math, while the other depends on programming experience). I'd like you to note however that i wrote "for most people" as well. In my experience, unless people are very new to programming, they prefer the shortcut. Unless you are not used to it the readability is pretty much the same, people are lazy so they prefer shortcuts, and it even has the added benefit of requiring one less symbol, so one less opportunity to make mistakes. It's a win-win.
     
  12. Chinard

    Chinard

    Joined:
    Mar 31, 2018
    Posts:
    9
  13. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    705
    I fully concur, and would like to add the following: chaining in the above example is harmless enough. But since chaining methods always implies that the return value is an object, the shorthand makes it very, very easy to overlook bugs when one of the links in the chain returns null.

    Well, I've started programming on the Apple ][ in 1979 and still dont like it :p

    And (because I'm a smartass) would like to Point out that requiring one less Symbol can turn into a disadvantage if you accidentally drop the '+', making the whole Thing still compile, but now with a different meaning :)
     
  14. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    771
    While this is technically true, these short-hand arithmetic operators are used very commonly in every language that it really doesn't matter. Basically everyone aside from new programmers understand what they mean.
    The same concept can also be applied to something like a for-loop, for instance.
    This...
    Code (CSharp):
    1. for(int i = 0; i < 10; i++) {
    2.    array[i] = 1 + i;
    3. }
    ...Versus this:
    Code (CSharp):
    1. for(int index = 0; index < 10; index = index + 1) {
    2.    array[index] = 1 + index;
    3. }
    While the latter is technically more readable, "index" is typically shortened to just "i" and is understood by basically everyone.
     
  15. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    12,020
    Possibly... but also, optimisation is a somewhat advanced topic within programming, so you might just be jumping a bit too far ahead. You need to have a solid understanding of what's going on under the hood before you start worrying about how to optimise stuff. The stuff on the page you linked assumes a working knowledge that goes somewhat "under the hood" with regard to what Unity is doing.

    Don't worry about optimisation until you run into a performance issue, and/or you're starting to build your own understanding of what's happening underneath the code you're writing. There's a lot to learn, and you don't need to know it all at once. :)
     
  16. Chinard

    Chinard

    Joined:
    Mar 31, 2018
    Posts:
    9
    Ok thanks for your answers :)

    I will go little by little and i will search when a problem appear