Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 25 Mar 2013
Offline Last Active Sep 22 2016 04:09 PM

Posts I've Made

In Topic: If-else coding style

09 July 2015 - 02:47 AM

I really doubt it's the style if if and else clause. If it really is then they are going to be hell to work with, just look how many answers people gave for what style they prefer.

But sadly, this was the problem, since the accepted solution is:

What was that pattern?

SomeType result = null;

if (somecondition)
    result = something;

return result;
Personally I'm not a huge fan of that, since it forces the reader to track the state of "result" as they look through the code, but it's used in many other places in our codebase, and consistency adds value.

Regarding the Name "GetSomething": properties in C# should never have a "Get", or "Set" as a prefix, so the name is wrong in any case.
But let's say, in order to get the value you want, you would need to do some calculations (the "magnitude" in Unitys Vector2 and Vector3 as an example), would you call a Method to get this value "GetMagnitude" or "CalculateMagnitude"? Does it matter for the calling site, whether or not the value is cached in a variable, or calculated on the fly?
In my opinion, "GetMagnitude" would be more suitable, since it doesn't define how the magnitude is retrieved. A "GetMeSomething" method doesn't imply the return of a member variable, and it shouldn't. You can see it this way: if you have a Vector and you want to get it's magnitude, you call it with "give me your magnitude" (assuming you can talk with your Vector, which I honestly can't do), what translates to the method name "GetMagnitude".

Before you respond, you should keep in mind: for me, object orientation isn't that much about the manipulation of data (member variables), but it's more about objects and their interactions.

In Topic: If-else coding style

07 July 2015 - 07:51 AM

As I tried to state earlier: "someCondition" is probably neither a member, nor a parameter, nor a local variable, nor a property. It's very likely just a placeholder for the real condition, as well as "something" is probably a placeholder for an evaluation/calculation.

In Topic: OOP: calling overridden method C#

07 July 2015 - 07:45 AM

If all components are assigned to the same GameObject, you should instead use the RequireComponent-Attribute.
public class SpartanKing : PlayerShape 

public class PlayerData : MonoBehaviour 

public class Player : MonoBehaviour
    void Start()
Unity will automatically assign the required components, as long as it's possible to assign them. Otherwise - an abstract class is required, such as "Collider" - you'll get an error message while trying to assign the component to a GameObject.

In Topic: If-else coding style

07 July 2015 - 02:24 AM

It really depends on the "somecondition", and we just don't know, what it actually is. If it's a simple null-check for "something" and "something" is just a private or protected member, just returning it would be the most simple thing. But if you're not just returning a member or if the condition is more than a simple null check, you can't omit the condition.

And by the way: it's a (C#) property, not a method. The name GetSomething is misleading (it should be called GetSomething instead), but you still can't call/invoke it. ;)

What do you mean by "not reevaluating the condition"? As soon as the value of the property is requested, the "get"-code is executed, thus the "somecondition" is evaluated. (And we can assume it's not just a variable, but a shortening of the real code.)

In Topic: what is best way to check a code is more effiecient and runs faster than others

30 June 2015 - 02:54 AM

By "i work with it as unity script" you mean you're using C# as scripting language in Unity, right? "UnityScript" is what the guys behind Unity call "JavaScript", without actually being JavaScript.
Then you should just take a look at the Unity Profiler first. I guess it's available for free, now that Unity 5 contains all engine features in the "personal edition" (in the free version).
Including any other profilers could be troublesome, since you don't have a Visual Studio solution to run the entire code yourself.