18
u/Manitcor Oct 30 '19
public object DoObjectOnObject(object def1, object def2, object def3)
20
u/Randolpho Oct 30 '19
mmm.... object on object action
Do it
6
u/RSGMercenary Oct 30 '19
While object def3 watches...
Or for the syntactically inclined...
while(def3.watches) { ... }
4
u/Manitcor Oct 30 '19
or in the "bad old days"
while (((Peeper)def3).Watches) { ... }
→ More replies (1)
29
u/aknop Oct 30 '19 edited Oct 30 '19
At the end, everything is just void *
10
3
Oct 31 '19
Look at mister fancy over there, with his rich architecture that supports memory addressing!
12
Oct 31 '19
I’m so glad I didn’t start using C# until until after C# 3. Not having generics and Linq must have been brutal.
8
u/crozone Oct 31 '19
I have to do work on .NET Compact Framework 2/3.5 every once in a blue moon.
It's hell. But at least they have generics!
26
u/PermanentlySalty Oct 30 '19
Living without generics was bliss compared to living without optional parameters. Mountains of overloads calling overloads calling overloads... *shudders*
6
u/bdcp Oct 30 '19
Uhh is this bad practice?
12
6
u/xampl9 Oct 30 '19
Having a couple isn’t the most horrible thing in the world. A dozen or more ... yeah it’s bad.
5
Oct 30 '19
depends on the definition of "mountains".
If you're rocking like 10 or something it might be an idea to create some argument objects because its likely you can reduce that to less.8
u/Manitcor Oct 30 '19
You didn't have a choice without optional parameters. It's mostly used in constructors and in APIs meant for others to consume. If your code is single purpose in the context of your your wider application then yes it can be a code smell.
3
u/PermanentlySalty Oct 30 '19
In my opinion, yes. Back in the day this was invalid syntax and would not compile:
int Foo( int x = 5 ) { return x * x; }so you'd need to write this instead to achieve the same thing:
int Foo() { return Foo( 5 ); } int Foo( int x ) { return x * x; }Methods with larger numbers of optional parameters would quickly balloon out of control, which is why you can often find methods with over a dozen overloads that all just call out to a different overload in the parts of the standard library that were written before optional parameters were introduced.
But there's no need for it anymore, so should be avoided.
5
Oct 31 '19
Are optional parameters just what they sound like, ie I have a constructor for a box class and I add length, width , height, and add an option to set the color, or let it default to brown.
2
2
u/RiPont Oct 31 '19
Yes. Optional parameters must have a constant value specified as default, and come after all other parameters.
public Box(int length, int width, int height, Color color = Color.Brown) { ... }The rule for a constant value means that it's not always possible to use a meaningful default value for a reference type, but that's usually solved by using
nullas the default value and then checking for null at the beginning of the function.→ More replies (2)2
u/RiPont Oct 31 '19
which is why you can often find methods with over a dozen overloads that all just call out to a different overload in the parts of the standard library that were written before optional parameters were introduced.
Or the other anti-pattern variations... overuse of
paramswhich led to more casting orFooArgsclasses/structures all over the place.1
u/crozone Oct 31 '19
It depends. Can the user overload the functions? Do you want the method to be CLI compliant?
For the BCL, very few things use optional parameters because they're not CLI compliant.
2
Oct 31 '19
Wait what? Hell no. Optional parameters are a nice bonus, generics are a core feature and i consider C# before generics to not even have been production readt retroactively given the whole class of bugs that arise from not having that. « look at this new fancy type safe language, it’s very type safe, at run time when it crashes in your face because you keep boxing structs slowly back and forth into lists of objects without any compile time type guarantees yipeee! »
1
u/PermanentlySalty Oct 31 '19
If strong guarantees of compile-time type safety and generics are a measure of whether or not a statically typed language is production ready, things start to get real interesting when you remember that C and Go exist, and that even Java and C++ didn't ship with generics or templates respectively despite both having had time to learn from their predecessors and academic papers available at the time.
And where does that leave dynamically typed languages like JS, Ruby, Python, and Perl? Without optional type annotations, third-party static analysis tools, or entirely new languages (like TypeScript), they have zero guarantees of type safety, and even with the type annotations it's still a problem that only makes itself known at runtime.
5
u/g5becks Oct 31 '19
All my code is using Go so I guess I’m still in stuck in that time. 😟
8
Oct 31 '19
I still can't believe they dared to release a programming language in 2009 without a way to make type safe generic data structures. Even weirder is that the fanboys are defending them to this day.
2
Oct 31 '19
It was in 2002 not 2009, generics were added in 2005. I agree it shouldn’t have been released for production before generics
4
1
u/BillyWasFramed Oct 31 '19
Some of the intentional limitations of the language are pretty annoying, as a developer. On the other hand, it doesn't seem to stop people from using the language to great effect.
4
Nov 01 '19
People have used PHP to great effect. That doesn't make it a good language.
→ More replies (1)1
u/g5becks Oct 31 '19
My problem isn’t so much that they released the language without them, but that it’s 2020 now and still doesn’t have them.😞 just hoping that changes soon.
5
Oct 31 '19
This applies to Golang nowadays
4
u/BillyWasFramed Oct 31 '19
Nothing like
being forcedhaving the opportunity to write a python script that generates implementations for each type, let me tell you.Some sass on the topic: https://github.com/StabbyCutyou/generics/blob/master/README.md
14
u/BirdFluLol Oct 30 '19 edited Oct 30 '19
Yeah the flip side of this is code ending up like...
internal class Foo<TBar> : IFoo<IList<ISomeOtherGeneric<string>>> where TBar : IBar, new()
And having to deal with the anxiety of debugging the monstrosity you've spawned.
11
u/lIllIlllllllllIlIIII Oct 30 '19
Could you call that debugging, though? Type errors occur at compile time (much better than runtime if you ask me).
9
u/BirdFluLol Oct 30 '19
No I mean when you're debugging, step into a generic method and be uncertain where you're going to end up. Happens all the time for us because we use a locator pattern. Can be annoying at times but it suits our needs for the most part.
7
u/DanielMcLaury Oct 31 '19
I mean, if the object you're working with really is an
IFoo<IList<ISomeOtherGeneric<string>>>, I doubt debugging the workaround nongeneric version is going to be any easier.3
u/crozone Oct 31 '19
This is why
varexists.Typically, code shouldn't have to use this many nested generics, but in the rare cases where it's needed,
varis a lifesaver.1
Oct 30 '19
ye but preferably you hide most of that away into just a few non-internal types in some other assembly you hope to forget exists.
i.e. imagine having all your builder types (to enable fluent builders) in your base namespace. ewwww.1
u/FrogTrainer Oct 31 '19
Lol now do a Mock object that handles a function that returns that type and requires 3 params that are also each nested generics.
1
u/perihelion- Oct 30 '19
I hate it, but i feel like I'm expected to write code like this
4
u/BirdFluLol Oct 30 '19
Yeah we have some gems like that in the codebase I work on, and I'm ashamed to say I've been responsible for a few. Like everything, it has its place, and if it causes more problems than it solves then there's no shame in tearing it down.
For some C# devs though it's a matter of pride to write obscenely generic code that's often unnecessary. There are times when I've thought it's bordering on gatekeeping.
2
u/skaNerd Oct 30 '19
That's what we call job security my friend. Started a new job this past April, and one of the codebases written about half a decade or a bit more ago made use of so many design patterns that it's absolutely insane.
1
u/xampl9 Oct 30 '19
“Keep back! I’ve got a copy of GoF and I think I know how to use it!”
1
u/RedTryangle Nov 01 '19
What is GoF and how should it (maybe not so intensely) be used?
2
u/xampl9 Nov 01 '19 edited Nov 01 '19
“Gang of Four” It’s the original design patterns book.
Like lots of new things, they got over-used. And misused.
Design Patterns: Elements of Reusable Object-Oriented Software
https://www.amazon.com/dp/0201633612→ More replies (3)1
5
u/CatDaddy09 Oct 30 '19
I totally disagree. Yet if it does have to do something crazy and specific like this, it should be so abstracted from 99.9% of everyday development and only used very specifically. I am a firm believer that too much abstraction, usage of interfaces, and multiple classes only serves to add undue complexity to your program. Yes, sometimes legacy systems do what legacy systems do. However, I really don't think this makes anything much better.
3
u/scandii Oct 31 '19
usage of interfaces
so you like to spend ages setting up your tests because you don't want to write an interface and use dependency injection?
1
u/CatDaddy09 Oct 31 '19
Nobody said dependency injection is bad. You just have to use it properly.
2
u/RedTryangle Nov 01 '19
I've been seeing the term dependency injection a lot recently, and usually regarding tests. I implemented my first interface yesterday and it is actually quite nice, but I have no idea how to set up tests.
Does this usually refer to testing on multiple machines, and users, etc? What are y'all testing with dependency injection? I'm having a tough time wrapping my head around it.
2
u/CatDaddy09 Nov 01 '19
It allows you to easily test because you can just spin up the full code right in the unit test. If my interface IOrders does "AddOrder" I can spin up multiple classes that maybe are different types of orders. One order needs to save to DatabaseA but also send an email notification. Another order saves to DatabaseB. In my test method I can just spin up the 2 classes and inject into the interface. Call their AddOrder methods, both doing 2 completely different things, then check results.
→ More replies (6)
3
3
Oct 31 '19
I’m just thankful they didn’t implement type erasure. It’s my biggest complaint about java generics
1
u/voroninp Oct 31 '19
This is the most terrifying thing I’ve met on Halloween! Don’t do that anymore, please, you are scaring me.
-9
u/leftofzen Oct 30 '19
Still can't do even basic variadic generics though, or partial specialisation, or proper type constraints, so they aren't very useful sadly. It's like the C# language designers forgot about modern metaprogramming entirely and implemented only basic, last-century features.
8
u/istarian Oct 31 '19
I mean technically C# first came into existence in 2000..,
1
Oct 31 '19
as in released or started development?
4
u/istarian Oct 31 '19
Going by wikipedia it was first released in 2001, but you have to develop something before you can release it.
The point is that it was practically statted last century.
→ More replies (1)5
u/DanielMcLaury Oct 31 '19 edited Oct 31 '19
C# generics aren't templates, so it makes sense that there's no analogue of template metaprogramming. The point of template metaprogramming is to do stuff at compile-time. C# generics are sorted out at runtime. Offering partial specialization wouldn't be anything more than syntactic sugar for checking the type at runtime via reflection, like so:
if(enumerable is List<T>) { // ... } else { // ... }Indeed, this is how code is written in .NET when it can be done more efficiently for certain types of containers.
Type constraints can be handled in a similar way.
Both of these are not possible to do at compile time, even in principle, because you can compile a generic method and then pass it objects of types that didn't even exist at compile time.
That said, you are right about variadic generics being a missing feature.
1
u/areller_r Oct 31 '19
What would be the difference between having a variadic generic and passing a value tuple to a normal generic? I just read about the term variadic generic so excuse me if it's a dumb question.
-9
Oct 30 '19
I hate casting in C#. It doesn't seem intuitive to me.
11
u/continue_stocking Oct 30 '19
What don't you like?
if (something is MyClass class) { /* use class variable */ }15
-7
Oct 30 '19
That's not casting.
What I don't like is the casting housed in the Convert class and it's just not the way my mind works. It's all personal preference.
14
u/Durdys Oct 30 '19
That...
iscasting? It's just safe, unlike usingasand the value without a null check or(MyClass)and praying.-1
u/istarian Oct 31 '19
Pretty sure if you had:
Class A { ... } and class B extends A { ... }
that a method expecting B couldn't be handed an A. without causing errors. You know unless C# has weird implicit casting like Java does autoboxing.
And even if it did, it would be just as bad as:
(B) instanceOfA
or whatever the exact syntax should be.
Since the thing you passed would be an A and not have any of the additional B bits.
2
u/crozone Oct 31 '19
if (something is MyClass class) { /* use class variable */ }just checks ifsomethingis assignable toMyClass, and if it is, casts it and stores it in the variableclass, then returns true (which triggers the if statement). It's almost the equivalent to:if(something is MyClass) { MyClass class = (MyClass)something; // ... Do stuff here }except it's cleaner and more efficient.
I'm not sure what point you're trying to make about implicit casting. In C# you can have a method that accepts some base class A:
public void SomeMethodThatAcceptsA(ClassA thingy)And call it with an instance of a derived/subclass B:
public class ClassB : ClassA { ... }ClassB derivedThingy = new ClassB(); // Works fine SomeMethodThatAcceptsA(derivedThingy);Of course,
SomeMethodThatAcceptsAcannot call any of the B specific methods unless it does a manual check and cast, like theisstatement above.1
u/istarian Oct 31 '19
What I was talking about is this:
ClassA Thingy = new ClassA();
SomeMethodThatAcceptsB( Thingy );
Casting is still actually required however you do it. And in any if whatever stuff is pointed to by Thingy doesn't have a B part and the method actually tries to do something with the B part of it...
I don't see why you should obfuscate the casting bit just to save a line of text, bot to mention that construction doesn't even look like sensible.
What I meant by implicit casting was that if C# tried to auto-cast things to the parameter type like Java autoboxes an int to an Integer if you pass the former to a function expecting the latter it. I.e. say some instance of ClassA was actually an instance of ClassB as long as ClassB extends ClassA.
3
u/crozone Oct 31 '19
The compiler won't let you do this, base class
ClassAcannot be assigned toClassBand the compiler will throw a compile time error if you try to run the code you provided.This means that code cannot "think" it's calling a method on
ClassBand then error because it was actuallyClassA, because it would first need to be safely cast, and casting will fail if the types are incompatible.C# does do implicit casting, it will automatically (implicitly) upcast types to any base type without being explicitly told to do so. It just won't downcast types, because it wants you to know there could be an error.
2
u/8lbIceBag Oct 31 '19
What you did right there won't actually compile. And if it does, you'll get an exception.
1
Oct 31 '19
So interesting that expressing my personal preference was voted down 6 times.
2
u/Durdys Nov 01 '19
Because your first statement was false. Your comment about the
Convertclass is more or less irrelevant. Uses forConvertare edge case at best and tend to be when you're doing reflection.1
Nov 01 '19
Because your first statement was false. Your comment about the Convert class is more or less irrelevant
Uh. No.
2
u/Durdys Nov 01 '19 edited Nov 01 '19
To which bit?
How about reading the docs? https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions
1
Nov 01 '19
I'm baffled why you're obsessed with my opinion but I found this at the URL you suggested:
"Conversions with helper classes: To convert between non-compatible types, such as integers and System.DateTime objects, or hexadecimal strings and byte arrays, you can use the System.BitConverter class, the System.Convert class,"
From that I conclude that there is a Convert class providing type conversions. Further research indicates the class is in versions 1.1 through 4.8 of the .Net Framework and 3.0 of the .Net Core Framework as well. I don't see any evidence that it's deprecated so I assume I can use it my code and teach it to my classes and talk about it on Reddit.
2
u/Durdys Nov 02 '19
Well your first statement wasn't opinion. It was factually incorrect.
isis casting.Secondly, the convert class is for edge cases and should not be seen as the go to pattern for casting. It's ugly, but how else would you do run time casting?
If you're not using the language in an idiomatic way, you can't really complain about the way it performs.
1
-20
Oct 30 '19
Wait,... are we using c# now???
4
u/wllmsaccnt Nov 01 '19
Its the fourth most popular programming language and you are in the csharp subreddit. You comments existence confuses me.
2
Nov 01 '19
Then my work here is done. saunters off.
2
u/wllmsaccnt Nov 01 '19
1
0
Oct 30 '19
[deleted]
7
u/Manitcor Oct 30 '19
You are still casting types, you are just doing it in one place making life easier.
0
Oct 30 '19
[deleted]
-1
Oct 30 '19
What would you expect it to do?
3
1
u/KevinCarbonara Oct 30 '19
By reading, I would have expected that loop to operate on every string contained in the collection. I'm familiar enough with C# to know it doesn't work that way, but there is a disparity between the syntax and the effect. I think there should be a foreach or foreach-style function that worked only on the classes you specifically referenced.
7
u/wordsnerd Oct 30 '19
Nowadays with LINQ you can use
OfTypeto do what I think matches your reading:foreach (var s in strings.OfType<string>()) ...And obviously
stringsis an evil name for that collection and needs to be renamed right this minute.4
Oct 30 '19
> I think there should be a foreach or foreach-style function that worked only on the classes you specifically referenced.
That's very interesting. I like the premise.
3
u/KevinCarbonara Oct 30 '19
It's not really that different from a priority queue in execution, to be honest.
0
Oct 30 '19
[deleted]
5
u/KevinCarbonara Oct 30 '19
I actually wouldn't expect it to do that. It is doing exactly what I expect it to.
Sure, if you know how C# works. But it's not intuitive. The words "for each string in collection" have real meanings that would indicate it operates on "each string".
85
u/[deleted] Oct 30 '19
My memories are just SO MANY COLLECTIONS...one for each type. List<T> is SO much nicer.