1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Code question above my paygrade

Discussion in 'General Discussion' started by m309, Nov 23, 2013.

  1. m309
    Offline

    Admin Moderator ServUO Developer

    Joined:
    Mar 3, 2013
    Messages:
    1,243
    Likes Received:
    130
    Alright. So I'm finally getting around to merging in ServUO 54 into what...I think...is my version of like 49, or 50. I can't remember...I've had my face in CED+ for like 6-7 months and haven't written a line of code, much less read too much of it, other than posted changelogs, in that many months.

    So my question has to do with the addition of 10 billion "this."'s. When I moved from a RunUO server to ServUO (then ForkUO 17 I think - I don't know..whenever Insanity first set it up on GitHub) it was one of the first things I noticed. I never understood why it was done. Now, in revision 54, in the server files (notably Mobile.cs - the entire file is red in BeyondCompare) I see they're pretty much all but gone.

    Can someone with more coding knowledge than myself clue me (and maybe hopefully others) in on what exactly they were added for, and now why they're gone? If for no other reason I'd like to know for the simple understanding and self-gratification of learning more about C# specifically related to this (<- no pun intended) clandestine passion we all share.
     
    • Agree Agree x 1
  2. Hank
    Offline

    Hank Active Member

    Joined:
    Nov 14, 2013
    Messages:
    972
    Likes Received:
    152
    I've also been dieing to know this and why the formatting looks different.
    the this. reminds me more of standard C# code but why was it necessary to add it when it still works without it.
     
  3. Ravenwolfe
    Offline

    Moderator ServUO Developer

    Joined:
    Sep 7, 2013
    Messages:
    1,146
    Likes Received:
    123
    "this" is a keyword that is usually optional. It is often used for people who want to explicitly declare the class instance.

    So if the script is a Zombie, then "this" used in the script is going to refer to the current instance of the class (Zombie). So you could have:

    Zombie.HitsMax = 100;

    or

    this.HitsMax = 100;

    or, because it is assumed that you are referencing the current instance, you can just have:

    HitsMax = 100;

    Basically, the first two explicitly declare who's HitsMax you are referring to and the last one just assumes the current instance.

    The only time "this" is not optional is when there is an ambiguity within the scope:

    HitsMax = HitsMax;

    Must be:

    this.HitsMax = HitMax;

    As far as why they were removed, I imagine either Mark or Vorspire decided to clean up the code a bit. Using "this" when it is not needed will get flagged in some programs like Resharper from my understanding.

    As far as the formatting, most of what I saw was about having less lines of code, using a single line of code instead of breaking it out across several lines:

    Code (C#):
    1. public override void HitsMax { get {return 100;} }
    is less lines than:

    Code (C#):
    1. public override void HitsMax
    2. {
    3.     get
    4.     {
    5.         return 100;
    6.     }
    7. }
     
    #3 Ravenwolfe, Nov 23, 2013
    Last edited: Nov 23, 2013
    • Like Like x 1
    • Two Thumbs Up Two Thumbs Up x 1
  4. m309
    Offline

    Admin Moderator ServUO Developer

    Joined:
    Mar 3, 2013
    Messages:
    1,243
    Likes Received:
    130
    Thanks @Ravenwolfe, that certainly makes sense. The whitespace, extra spacing and/or lines part of the code never bothered me as I have BeyondCompare set to ignore it - but the lack of "this." present in the current revision definitely caught my eye.
     
    • Like Like x 1
  5. Ravenwolfe
    Offline

    Moderator ServUO Developer

    Joined:
    Sep 7, 2013
    Messages:
    1,146
    Likes Received:
    123
    I may be mistaken, but I think the less lines is not just about asthetics but is actually more efficient for the compiler to read.
     
  6. m309
    Offline

    Admin Moderator ServUO Developer

    Joined:
    Mar 3, 2013
    Messages:
    1,243
    Likes Received:
    130
  7. Ravenwolfe
    Offline

    Moderator ServUO Developer

    Joined:
    Sep 7, 2013
    Messages:
    1,146
    Likes Received:
    123
  8. Hank
    Offline

    Hank Active Member

    Joined:
    Nov 14, 2013
    Messages:
    972
    Likes Received:
    152
    I am using the latest release of servuo and the code has always looked like that second example. I'm aware that the first example uses less lines, which is why I like it and that is what runuo code looks like. But all the servuo Distro code I've seen is like the second example.
    I was just wondering why the formatting is using the 2nd example with it all spread out on many lines.
    Here is an example from a piece of armour. It all looks like this and for me it is not what I'm use to seeing format wise.
    [spoilerbb=armour example]
    Code (C#):
    1. public override int BasePhysicalResistance
    2.         {
    3.             get
    4.             {
    5.                 return 6;
    6.             }
    7.         }
    [/spoilerbb]
    So why is servuo code formatted this way? I'm aware there is no difference other then it being spread out on many lines.
    Now i know not every single script is this way, but there is enough examples of it to be intrusive.
    Who thought,.. "you know it would make it so much easier if we make a 100 line script 400 lines! Brilliant!"
     
    #8 Hank, Nov 24, 2013
    Last edited: Nov 24, 2013
  9. Insanity
    Offline

    Insanity Active Member

    Joined:
    Mar 2, 2013
    Messages:
    276
    Likes Received:
    61
    The reason being why some formatting has been changed in ServUO is because it was ran through ReSharper a long while ago. Then as other people have worked on the project, they either didn't use ReSharper...or used different ReSharper settings.

    Good news about all this though, this will all be addressed pretty soon. As we've added a source license to the repo (and I'll be adding a license page for the project here on the site soon.) I'll be applying for a team development license for ReSharper soon. Meaning everyone on the team, or anyone that wants to contribute can use ReSharper with the team development license and not have to pay for the software. Although with most good news, is bad news. Once we get the proper cleanup settings in place, it will end up modifying all files. So it will be a rather large update to pull, so I hope everyone is using Git properly.

    Once I get a team license, I will be including project based ReSharper settings in the repo (the repo is currently set to ignore the files atm.) This will allow anyone to run their own code through the same cleaner settings. Or change the settings and clean the code to how they wish.

    The reason why ServUO is going to use ReSharper to keep the code clean is for a few reasons. Namely, it allows for easier reading and reviewing of the code. Which makes it easier for anyone to quickly find issues, or find what they need. Instead of properties being scattered throughout a class (methods and shit in between various properties) the code cleaner moves them all together. Among other changes that make it easier to view a code.

    As for the this keyword. As mentioned above, it is a keyword used to specify the following variable's scope is local to the class the code is in. Meaning if you see this.HitPoints. You know you can find the HitPoints property in the same class, and not a base class. The this keyword is not need, and can be stripped with ReSharper with a single click. Although it helps with reading and reviewing code. So I'd recommend you use it and get used to it.
     
    • Two Thumbs Up Two Thumbs Up x 2
    • Like Like x 1
    • Agree Agree x 1
    • Informative Informative x 1
  10. m309
    Offline

    Admin Moderator ServUO Developer

    Joined:
    Mar 3, 2013
    Messages:
    1,243
    Likes Received:
    130
    Thanks for the explanation, Insanity - and the new changes sound awesome!
     
    • Like Like x 1
    • Agree Agree x 1
Similar Threads: Code question
Forum Title Date
Script Support Question About A little Peace of Code ;) Dec 5, 2015
General Discussion Can I Have Find Base Code ? Oct 7, 2017
Looking For Help New Server: Looking for Coder, Web Designer, and/or Community Manager. Sep 21, 2017
Support StatCodex Aug 23, 2017
Archived Bug Reports Monster Stealables missing code Jun 10, 2017
Support SkillCodex on CharacterCreation.cs Apr 15, 2017
Suggestions #codespam channel for discord chat Apr 2, 2017