Should I learn two programming languages at the same time?

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Should I learn two programming languages at the same tim

Post by albinopapa » January 16th, 2017, 3:14 pm

cyboryxmen wrote:The examples that some common textbooks provide does no justice to the transformative, polymorphic properties C#'s getter and setter functions provide.

Code: Select all

public void Update()
{
	pointer.position = system.mouse.position;

	float length = pointer.position_from_centre.length;
	if ( length > threshhold_radius )
	{
		pointer.position_from_centre.length = threshhold_radius;
	}

	turn_speed = length * speed * system.delta_time;

	...
}

public void OnRelease()
{
	pointer.position_from_centre = new Vector2 ( 0, 0 );
}
So really you just don't want to have to write:

Code: Select all

Vector2 &PositionFromCenter();                  //Setter
const Vector2 &PositionFromCenter()const;  //Getter
for example, for each class you want access to it's members?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
cyboryxmen
Posts: 190
Joined: November 14th, 2014, 2:03 am

Re: Should I learn two programming languages at the same tim

Post by cyboryxmen » January 16th, 2017, 3:35 pm

You don't understand. You can't just do a Vector2& to position_from_center because there is no position_from_centre.

Code: Select all

public class ScreenPosition
{
	private Vector2 position_;
	private Screen screen_;

	public Vector2 position
	{
		get
		{
			return position_;
		}
		set
		{
			position_ = ClampToScreen(value);
		}
	}

	public Vector2 position_from_centre
	{
		get
		{
			return position - screen_.centre;
		}
		set
		{
			position = screen_.centre + value;
		}
	}

	...
};
Zekilk

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Should I learn two programming languages at the same tim

Post by albinopapa » January 16th, 2017, 3:51 pm

To say there is no "position_from_centre" is a bit confusing, could you explain? It has to come from somewhere. One of the more confusing things for me about C# is the . is used for member access and scope resolution, so it's hard to tell what is a static member or a non-static member, a global in some namespace some where, etc.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
cyboryxmen
Posts: 190
Joined: November 14th, 2014, 2:03 am

Re: Should I learn two programming languages at the same tim

Post by cyboryxmen » January 16th, 2017, 4:36 pm

In C#, you are not supposed to change the variables of an object but its properties instead. For example, Vector3 has the properties x, y, z, and length. Internally however, it only contains the variables x_, y_, z_ and the properties are derived from them. x, y and z derive their values from x_, y_ and z_ individually. length however is derived from x_, y_ and z_ together.

Code: Select all

public class Vector3
{
	...

	public float length
	{
		get
		{
			return Mathf.Sqrt ( x * x + y * y + z * z );
		}
		set
		{
			float prev_length = length;
			x *= value / prev_length;
			y *= value / prev_length;
			z *= value / prev_length;
		}
	}

	...
}
What this does is provide another level of abstraction by allowing users to configure an object through their properties rather than directly with their variables. This together with operator overloading and member functions to represent actions and reactions(Fight(), Fly(), OnDamaged() etc.), you have a large playground of syntax to design with. You can even add a direction property that returns the normalised version of the Vector3 and also sets it to a different direction without changing the length.

Code: Select all

public void UpdateVelocity()
{
	Vector3 position_to_target = target.position - position;
	velocity.direction = position_to_target;
	...
}
This one additional syntax incentivises you to design your classes in a way that you would not have otherwise. There is no C++ equivalent(in the same way there is no Java equivalent for operator overloading. I really hate that.)
Zekilk

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Should I learn two programming languages at the same tim

Post by chili » January 17th, 2017, 3:52 am

I'm with you cyborg about the love for C# properties. You play your hand a little to strongly though, it's not quite the game-changer you make it out to be. It-like operator overloading-is basically syntactic sugar when you get right down to it. It doesn't enable anything that you couldn't do before, it just makes it sexier.

void SetPosition( ... )
const Thing& GetPosition() const
void SetPositionFromCenter( ... )
const Thing& GetPositionFromCenter() const

Just like papa says.
Chili

User avatar
cyboryxmen
Posts: 190
Joined: November 14th, 2014, 2:03 am

Re: Should I learn two programming languages at the same tim

Post by cyboryxmen » January 17th, 2017, 4:27 am

Yeah but that pretty much applies to all high level syntaxes. You can really do away without classes, inheritance and even virtual functions and still be able to program quickly and efficiently. In fact, this is basically one of the excuses anti-C++ers use to avoid OOP and advocate their viewpoints to other people.

The one real advantage that these high level syntaxes provide and the reason why we aren't programming in pure C right now is how they change the way you design and use your code. Before I learnt about properties, I was perfectly happy with my Vector3 overloading all the operators and having the functions Dot(), Cross(), Normalised() and Length(). Anything else can be done manually through these functions and operator overloads. Now though, I have learnt to think of my classes as a set of properties and make them configurable through these properties. I would have never bother making a SetLength() or SetDirection() function because i was not in the right mindset to think of such a function.

It's really the same as how Classes make us think of code in terms of objects rather than the C way of thinking of them as an array of numbers to be calculated as efficiently as possible. On the flip side, Haskell is making rounds among old school programmers because of how their syntax emphasises pure functional programming. When it comes to choosing a language to use, the syntax is key.
Zekilk

Post Reply