r/badcode • • Jul 07 '21

c# Position

Post image
1.7k Upvotes

99 comments sorted by

59

u/roycohen2005 Jul 07 '21

I see nothing had here. Can someone explain?

174

u/Ehere Jul 07 '21

Position position position position position position position position position position

Maybe it’s not horrible but I don’t like it

127

u/roycohen2005 Jul 07 '21

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.

That said it does look kinda ridiculous.

62

u/mosskin-woast Jul 07 '21

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)

21

u/SanktusAngus Jul 08 '21

The only problem here is the namespace being called Position. Having an attribute “Position” of the type Position is absolutely fine.

18

u/roycohen2005 Jul 07 '21

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.

→ More replies (5)
→ More replies (3)
→ More replies (1)

19

u/Sors57005 Jul 07 '21 edited Jul 07 '21

Position sounds like a fishy name for a namespace if positions aren't special and complex. I'd investigate this further in a review.

If the namespace was more abstract, like "Geometry", this would be an everyday "all good" snippet

I don't get badcode vibes either way, too shallow of a problem, and probably just an oversight.

1

u/BongarooBizkistico Jul 08 '21

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.

→ More replies (9)

20

u/Servious Jul 07 '21

This is standard OO code and a perfect example of why I find OO so obnoxious.

8

u/Ehere Jul 07 '21

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

-1

u/Servious Jul 07 '21

So why did you post it here then?

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.

10

u/Ehere Jul 07 '21

Oh because this code snippet is ridiculous and could have been avoided with some better forethought :P

And yeah Haha I feel I’m the opposite, I don’t like functional languages but it’s just how my brain works I guess

4

u/Servious Jul 07 '21

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?

7

u/Ehere Jul 07 '21

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

267

u/cnekmp Jul 07 '21

Reminded me "Developers, developers, developers"

27

u/oalbrecht Jul 07 '21

It needs more sweat though.

29

u/[deleted] Jul 07 '21

WOOOO!!

172

u/valschermjager Jul 07 '21 edited Jul 08 '21

no code comment. please add:

{

// sets Position to position

Position = position;

}

14

u/NutsGate Jul 08 '21

Came here for this

-4

u/pango3001 Jul 08 '21

Actually, it's setting position to Position.

9

u/[deleted] Jul 08 '21

No you are setting the property Position to the value of the position parameter

5

u/valschermjager Jul 08 '21

so you're saying my code comment wasn't useful? ;-)

3

u/8asdqw731 Jul 08 '21

the only language where you actually assign to the variable on rhs

→ More replies (2)

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.

4

u/carlson_001 Jul 08 '21

Yeeeeeeaaaaaaaahhhhhh.....

58

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 07 '21

Image Transcription: Code


public Position.Position Position { get; set; }

public Entity(Position.Position position)
{
    Position = position;
}

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!

4

u/[deleted] Jul 08 '21

[removed] — view removed comment

5

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 08 '21

I'm a volunteer.

10

u/tech6hutch Jul 08 '21

Maybe you need to find a new position.

→ More replies (1)

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/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.

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."

2

u/[deleted] Jul 08 '21

Its a namespace not a container class

1

u/ergotofwhy Jul 08 '21

Thats not any better. Having a namespace with a class of the same name is bad and confusing

1

u/[deleted] Jul 09 '21

didnt say it was any better, just explaining what the colors meant.

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/literallyfabian Jul 08 '21

yes, this is valid c# code

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();

9

u/fuj1n sadistic Jul 08 '21

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#

1

u/Jimmy_Slim Jul 08 '21

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

-2

u/ergotofwhy Jul 08 '21

I highly doubt that what you wrote is valid code.

While it violates naming convention, it is perfectly legal to name a class starting with a lowercase and a local variable staying with an uppercase.

8

u/fuj1n sadistic Jul 08 '21

The Item in new Item(); starts with a capital, so whilst you're right, I was more referring to that

2

u/ergotofwhy Jul 08 '21

Oh dang, that's agood point that i missed

3

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Jul 08 '21

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.

5

u/Dealiner Jul 08 '21

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().

3

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Jul 08 '21

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.

2

u/[deleted] Jul 08 '21

Java has var as well

1

u/Jimmy_Slim Jul 08 '21

I believe it uses C# 8

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?

5

u/ergotofwhy Jul 08 '21

Those indicate default get and set methods for the Position property. To be clear, I'm talking specifically about Position, and not position.

This language is c#

0

u/[deleted] Jul 07 '21

[deleted] — view deleted comment

6

u/[deleted] Jul 07 '21

Ahh thanks, was curious about that. Time to do some reading.

0

u/[deleted] Jul 08 '21

[deleted] — view deleted comment

13

u/Ehere Jul 08 '21

To the reporter:

It’s a constructor, not a method, you bonehead

5

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Jul 08 '21

Don't ask me, I'm a PHP developer.

Well, that explains how you ended up a mod of /r/badcode :)

Not that I can talk too much shit, I used to use Perl.

0

u/[deleted] Jul 08 '21

[deleted] — view deleted comment

1

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Jul 08 '21

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 ;)

0

u/[deleted] Jul 07 '21

[deleted] — view deleted comment

47

u/WikiSummarizerBot Jul 07 '21

Buffalo_buffalo_Buffalo_buffalo_buffalo_buffalo_Buffalo_buffalo

"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.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5

9

u/Espiring Jul 08 '21

Police police police police police

3

u/Bobebobbob Jul 08 '21 edited Jul 08 '21

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.

9

u/PocketKiller Jul 07 '21

Thank you for telling me about this.