#Usage of Vector2, Vector3, etc... Is it correct to use it only as a Container of n elements ?

1 messages · Page 1 of 1 (latest)

honest raven
#

General question regarding C# in general (but could also be about C# usage in Unity or any programming language really), I talked with some people about me creating a Vector2 class (in this case I'm using Java, so that class doesn't exist by default) and I wanted to use that Vector2 as a way to store 2 values (x and y) to use it in a method that checks if a value is in range between a min and a max and they told me it's bad practice because Vector2 is mathematical concept, so it doesn't make sense to use it to store 2 values to do a check on a range.

Are what they're saying correct ? After checking the official Microsoft doc about Vector2 in C#, the doc seems to say the same thing as them. But then, if it's true, is there another structure / other name that I can give to my class that can hold 2 (or more) values (here they're floats) ?

Please ping me if any answer 🙂

still valley
#

there's no "correct" here in terms of design

#

you can have something like that to hold 2 numeric values. syntactically, that's fine. structurally, that's fine.

semantically, it's very questionable (but not technically wrong)
the name "vector", "x", "y" give an impression of geometry, of the math concept. if you aren't using it for that, it's misleading and can cause confusion and code that's hard to read and maintain.

honest raven
still valley
#

just make a structure for that concept specifically, you don't necessarily need to make it hypergenericized.
making it specific to your usecase makes it easy to introduce relevant helper methods or modify them in a way that makes sense

honest raven
#

a structure ? Structs aren't available in C# nor in java if I'm not mistaking 🤔 ?

still valley
still valley
#

you could have classes or records in java as analogues

still valley
#

you could just use that

#

(why do you have a separate Range and Vector2 to begin with, are those not just the same thing in your usecase)

#

oh, a Range isn't actually a "Range", but a bounded value, in your impl

honest raven
# still valley you could have classes or records in java as analogues

I'm already using records in this case, but having arguments like Rotation or Vector2, people told me it's bad because those are Mathematical concepts and I should go for classical floats, but the issue is that if I go for classical floats, for Rotation I need to call the method ensureIsInRange() 3x in my code and for Vector2 2x (1 for each component) which is horrendous to see in code and painful to write each time, that's why I went for a grouping way of those things.

still valley
#

Rotation is probably fine

#

Vector2 is questionable

#

Range is named poorly

honest raven
#

what should I call it ?

still valley
#

Range is named poorly for what it is - i don't think it really needs to exist, and you could have Vector2 be called Range

clever ruin
#

Vector2 for a min max is totally common and fine, it’s just a name as Chris said

#

The equivalent in shader code is legit called float2, float3 etc.

still valley
#

float2/float3 are just describing what it contains, but Vector2 describes what it is conceptually, so i kinda disagree that it's perfectly fine

honest raven
still valley
#

which is exactly why i think Vector is not a good name

honest raven
#

hence my question, what's a good name for it ?

honest raven
#

oh you said ValueBounds, mmh

honest raven
#

how would Pair be / workd in this case ? 🤔

still valley
#

Range is a good name for what Vector2 currently is, and the logic of the currentRange can all be merged there as well
i don't think the structure that is (value, min, max) needs to exist

still valley
honest raven
still valley
#

it isn't

#

why would it be reserved

#

all the keywords are lowercase (that extends to most languages as well)

honest raven
#

my bad not keyword but rather an already exisitng class

still valley
#

it's in awt or something like that, not in java.lang

honest raven
#

mhh

granite bridge
#

I used to do this, but nowadays I prefer separating ranges into separate values. I think code clarity matters a lot. However, if the variable is named someValueMinMax it's relatively clear. Just range could be misinterpreted as a 2-dimensional range starting from 0.

I am not fond of the term "range" for a struct name, since ranges can be represented in many ways, and additionally there's some overlap with other programming terminology (e.g. ranges in C++ are something different)

struct MinMax
{
  float min;
  float max;
}

In general, I think writing good code is about understanding other people's expectations and habits, and thus writing your code your way while minimizing the room for misunderstandings and ambiguities.

#

@honest raven