Using "position" a lot of times isn't, by itself, a bad thing. This looks like standard naming. A namespace and a type both named "Position", ok so far. The property has the same name as its type, that's very common. And finally, the constructor parameter named after the property its linked to, this is also very common.
It's stutter and it's bad for readability. It's the product of languages that force you to use classes and objects for everything instead of letting packages export free functions or constants, IMO, which is a very annoying way of doing things (looking at you, Java)
I agree. Even though it's not nearly as flexible as python, for example, C# does have static using. For example, if in the top of a file you write using static System.Math;, in the rest of the file you can directly access members of System.Math like Abs or Sqrt.
I think what's best is flexibility in naming and importing packages. For example, in Typescript, you can import the whole namespaced package, only a few exported members into the current namespace, or only the default export. You can avoid stutter and namespace collision at your own discretion.
Then there's Go, which strongly discourages importing into the current namespace and even has built in tooling that bugs you for adding stutter to your API. I don't hate it, and it encourages good practices, but it can sure be annoying.
Then there's Java... Which basically forces you to either stutter or spend way too long thinking about naming things (which I already do anyway). Everyone solves problems in different ways I suppose
let avg l = (List.fold_left (+.) 0.0 l) /. (float_of_int (List.length l))
you could do either one of these instead:
let avg l = List.((fold_left (+.) 0.0 l) /. (float_of_int (length l)))
let avg l =
let open List in
let open Float in
div (fold_left add 0.0 l) (of_int (length l))
In both cases, the module opens only happen in the scope of the avg definition, so you avoid global namespace pollution while also cutting down on the silly boilerplate repetition.
I wish F# could do that as well, since it'd be really useful in that ecosystem since it shares a lot of the Java "stutter" as you call it, but any proposals to add it get shot down. .NET and Java land both have the "why do you care, your IDE should deal with that for you" mindset :|
I saw her mentioned in comments somewhere on Reddit. Even though I don't know German, watching the illustration on YouTube I can understand it. IDK why I find it really satisfying!
This is confusing for another dev to read. What's the difference in the different iterations of position, and why do we even need this code? The code doesn't even attempt to explain itself.
I'd argue that this code doesn't need to explain itself as it's perfectly clear what it is just by looking at it. It's an entity class with a property for its position and a way to initialize that property in the constructor.
I do get what you're saying about readability, and a namespace called Position isn't helping.
Yeah. I mean with context it may not be as bad but as a person who both didn't write this and doesn't work in this type of environment, I'm clueless. I imagine even people closer to it would need an extra moment to parse this, a moment that they maybe wouldn't need if the names were more descriptive.
Except the namespace Position, on which we need more info to decide, I feel that every name is pretty good. Do you have a better name in mind for any of these?
I'm telling you I don't understand the code because of how it's written and you're asking me to rewrite it to be understandable. Don't see an issue there?
Ok let's start with this then: there's an instance property named with an uppercase first letter? If so, that's stupid, anti-convention in my experience, and confusing. If that's not the case then I still don't understand what is being set.
In any case, I don't think 6 mentions of the same word, unmodified, in 3 lines is intuitive, at all. And that would be assuming the namespace was changed like you mentioned, right now it's 8. I don't think my opinion is controversial. I mean would it have killed the author of this code to throw in an adjective for a name, such as initialPosition?
Naming is important but the author of the code doesn't seem to get that. Reminds me of a coworker who used to use 1-3 letter names for all variables. When asked about it, he would say "it doesn't matter what you call it" missing the point entirely and not getting that code is written for humans, not computers. The entire point of higher level programming languages is to be human writable and readable.
First of all, in C# the convention is to name every public type or member with PascalCase, meaning that an instance property with an uppercase first letter is the norm.
Secondly, I think both initialPosition and position are suitable names here. It's obviously the initial position because it's in the constructor. For example, if you were writing a simple Point class with x and y coordinates, would the constructor be Point(double x, double y) or Point(double initialX, double initialY). Keep in mind how these might be called: new Point(x: /* some complex expression*/, y: /* another complex expression */) vs. new Point(initialX: /* some complex expression*/, initialY: /* another complex expression */). They're both ok.
Finally, I want to clarify that I don't think the code in question is "good code", I'm simply saying it's not "bad code". It looks like standard boilerplate code to me.
I think the fundamental difference in our thinking is I'm wanting subtle hints sprinkled throughout the code that remind me what it's doing and how it works. To say "it's obvious because.." especially in a context where the human brain can get bogged down by reading the same word several times, to me is not the right way to look at it.
Slight readability enhancements make it so both people who are very familiar with the dev environment get a small boost in readability, and novices like me who have never written that language can actually still see what's going on, at least quicker than they otherwise would've.
I write code all the time that reads confusingly later. With fresh eyes it's easily fixed with a few small changes. I think that's very typical. Everything was very obvious to me when I originally wrote it, but that's not the right measure for optimal readability.
Also consider this. If the code used better names you may not feel like you're speculating. Plenty of single lines can be taken out of context and still be instantly meaningful to devs not even familiar with the language used. None of this is imo.
I'm not saying all code must be that way, but if it is, that's probably a good thing. Code should be as readable as possible.
Yeah this is a common criticism but I’ve always felt that this type of thing is a necessary drawback for the benefits it ultimately provides. But that’s just my opinion
I'm not saying I think OO code is bad or that it doesn't have its place or that it isn't useful or that code like this should be changed in OO languages or anything like that. I agree that all the elements of this code are necessary for proper, scalable OO code and that OO as a whole has a very valuable and useful place in CS and software development in general.
I, personally, just find it not fun to work with when compared to functional code in languages like Haskell or Scala.
I'm really not trying to be a dick here but I'm genuinely confused. Is this a "necessary drawback" or could it have been avoided? Those two things feel mutually exclusive.
Do you just mean they could have imported Position more properly so it didn't need the namespace?
I think the addition effort needed to select good naming schemes and stay organized is the necessary drawback because it can get complicated quickly. Let me know if I’m not wording myself clear
that’s what makes my code comment above perfect, because it’s perfectly and semantically correct and incorrect simultaneously, which makes it perfectly useless, which I’ve found, after inheriting others’ code, that most code comments are.
"for keeping track of positions, we made a class called Position. This has numerous helper methods. We made a subclass to keep track of the actual position value. We named that Position, too."
With all due respect, Terraria is written in C#, and in C#, the type name precedes the name of a variable, so I highly doubt that what you wrote is valid code.
It should probably be Item item = new Item(); But I think that variable name is not very descriptive assuming you're in an environment where you have multiple items.
Edit: haven't actually modded Terraria, comment based purely on knowledge of C#
Yeah, it was Item item = new Item(); I was just going off of memory 😅 in not the greatest at C#, and I actually haven’t coded any of it in several months
Ah yes, the joys of lacking good type inference. Any idea what C# version you're using to mod it? At some point C# got type inference on constructors so you can just type var my_item = new Item() instead of having to explicitly annotate the variable. I think that's pretty recent, though, so it might be a while before games start being able to take advantage of it.
Type interference with var has been in C# from C# 3, so for about 14 years now. Additionally in C# 9 there is a new thing called target-typed new expressions, which means that this is possible: Item item = new().
Additionally in C# 9 there is a new thing called target-typed new expressions
That's what I was thinking about when I wrote my comment. I only tangentially follow C# because of interest in F# so I thought using var like that for new objects didn't work correctly until that change.
If var foo = new Bar() has worked for longer than that it seems like there's no reason to have been writing out the Java-style Foo foo = new Foo() crap at all.
This is pretty common as a Component. This is still up to team/project preference but the only down part I saw is Position property name should be Data. So every Component will be accessed with Position.Data, Speed.Data, Health.Data etc
Yeah I was surprised nobody already said so I decided to fill that gap.
And regardless of any truth (or not) to the statement, I doubt any PHP you've written can be as horrible as my flair, which is something I did write long ago, so I figured I was safe making the joke since I'm at least as bad ;)
"Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo" is a grammatically correct sentence in English, often presented as an example of how homonyms and homophones can be used to create complicated linguistic constructs through lexical ambiguity. It has been discussed in literature in various forms since 1967, when it appeared in Dmitri Borgmann's Beyond Language: Adventures in Word and Thought.
They don't police themselves? Tf? Or are are the police police police policing the normal police? B/c that's not their job, somebody needs to be payed overtime.
And everyone knows the police don't police the police police police, that's just absurd. Maybe you meant that police police police police do the action of policing, which i guess is true, though pointless to say. And the same applies to saying the police (whom the police police do the policing of) also do the action of policing. It's just a bunch of redundant information. Everyone knows the Police Police police the Police, that's what Police Police means. And obviously Police police, it's their job. Overall that sentence is pointless at best and blatant police, police police, or police police police misinformation at worst.
Edit: Unless you're telling me to police the police police police police, in which case that sentence makes total sense except for the fact that I am in no position to take up that job offer, and I must apoligise for this police misunderstanding.
59
u/roycohen2005 Jul 07 '21
I see nothing had here. Can someone explain?
20
u/Servious Jul 07 '21
This is standard OO code and a perfect example of why I find OO so obnoxious.
267
u/cnekmp Jul 07 '21
Reminded me "Developers, developers, developers"
172
u/valschermjager Jul 07 '21 edited Jul 08 '21
no code comment. please add:
{// sets Position to positionPosition = position;}34
u/Truth1e Jul 08 '21
It's funny how some people find this code great and others find it horrible, I guess it all depends on your position.
58
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 07 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
22
u/[deleted] Jul 07 '21
Position
16
u/StochasticTinkr Jul 07 '21
After I wrote this, I no longer had a position at the company.
2
u/rezv0l Jul 07 '21
Position
2
u/QueenVogonBee Jul 07 '21
Buffalo buffalo buffalo buffalo…..
2
u/knoam Jul 08 '21
Read it to the tune of Tradition from Fiddler on the Roof
2
u/[deleted] Jul 08 '21
Written in C plus plus plus plus.
2
u/DavePvZ Jul 08 '21
quantum superposition
2
u/gt4495c Jul 08 '21
Try that in VBA.NET which is case insensitive. Fun fun fun.
3
u/thesola10 Jul 07 '21
r/ontheledgeandshit
1
u/Imperator_3 Jul 08 '21
Position
1
u/knigitz Jul 08 '21
As long as the IDE can tell them apart, it's not bad. It's just not best practice because it's less readable. But it's not bad.
1
u/ergotofwhy Jul 08 '21
"for keeping track of positions, we made a class called Position. This has numerous helper methods. We made a subclass to keep track of the actual position value. We named that Position, too."
1
u/Franks2000inchTV Jul 08 '21
Baked beans are off.
Then I guess I'll have the spam spam spam spam spam spam spam spam spam and spam.
1
u/dumblechode Jul 08 '21
Ah, I know exactly what’s going on here
1
u/loydfar Jul 08 '21
Does this actually compile ? Thought that a property can't have the same name as the class in C#
1
u/redd_seth Jul 08 '21
This guy position
1
u/Toxic-Sky Jul 08 '21
Naming is hard. :P
1
u/JustThingsAboutStuff Jul 27 '21
I may have done something like this when first learning OOP. Now I'm happily back in ASM.
1
u/Jimmy_Slim Jul 07 '21
reminds me of how you create an item in Terraria:
item Item = new Item();1
u/franklincan Jul 08 '21
Wow, no one here ever seen ECS code before?
This is pretty common as a Component. This is still up to team/project preference but the only down part I saw is Position property name should be Data. So every Component will be accessed with Position.Data, Speed.Data, Health.Data etc
1
u/[deleted] Jul 08 '21
eh, just a poorly named member variable. Be like me - name your variables full sentences
-1
u/[deleted] Jul 07 '21
What language uses semicolons instead of commas, when passing parameters?
0
u/[deleted] Jul 08 '21
[deleted] — view deleted comment
0
u/[deleted] Jul 07 '21
[deleted] — view deleted comment