7 ms·
I love generics! Simplifies all my api creations deeply, huge re-use of code and very flexible implementations. Eg. An API to a IoT cloud that lets user define
by NicoJuicy 9y ago
I love generics! Simplifies all my api creations deeply, huge re-use of code and very flexible implementations.
Eg. An API to a IoT cloud that lets user define their own type. The payload that i fetch is <T> so users can define their own type.
Another one is an api where i take a generic approach, so my only code that i write ( for every "class" / " model" ) is:
//every endpoint does a complete crud + jsonpatch + batch, also, all methods can be overridden to customize code. This is the code i need to write every time :p
https://pastebin.com/fp8MtpBu https://pastebin.com/fp8MtpBu
Note to myselve: extend the implementation to support expressions ( odata ) in the url ( similar to sql on top of an API through changing url parameters) and return the dto as response. :)
- lloydjatkinson 9y agoVery similar to something I'm working on. Except in my case I have an interface that the consumers implement to be able to talk to their service/database/whatever. It's designed for time series data (e.g., cpu usage, room temperature, that kind of thing). It's meant to only allow for numeric values and strings. But because there is no base type or interface for numeric types I unfortunately can't do something like: public Result WriteValue<T>(T value) where T : INumeric/Numeric { } https://stackoverflow.com/questions/32664/is-there-a-constraint-that-restricts-my-generic-method-to-numeric-types https://stackoverflow.com/questions/32664/is-there-a-constra... :( So instead the interface has an identical method for ints, doubles, floats, decimals, strings...
- apk-d 9y agoMy favorite taunt from the .NET team: according to the .NET Reference Source, built-in numeric types implement an IArithmetic<T> interface... And then they comment it out. http://referencesource.microsoft.com/#mscorlib/system/double.cs,1a65cbdb09544ba1 http://referencesource.microsoft.com/#mscorlib/system/double...
- louthy 9y agoHere's an ad-hoc polymorphic way of dealing with numeric types in C# without the boxing that would be caused by using an IArithmetic interface: public interface Num<A> { A Add(A x, A y); A Subtract(A x, A y); A Multiply(A x, A y); A Divide(A x, A y); A FromInteger(int value); } public struct NumInt : Num<int> { public int Add(int x, int y) => x + y; public int Subtract(int x, int y) => x - y; public int Multiply(int x, int y) => x * y; public int Divide(int x, int y) => x / y; public int FromInteger(int value) => value; } public struct NumDouble : Num<double> { public double Add(double x, double y) => x + y; public double Subtract(double x, double y) => x - y; public double Multiply(double x, double y) => x * y; public double Divide(double x, double y) => x / y; public double FromInteger(int value) => (double)value; } public struct NumBigInt : Num<BigInteger> { public BigInteger Add(BigInteger x, BigInteger y) => x + y; public BigInteger Subtract(BigInteger x, BigInteger y) => x - y; public BigInteger Multiply(BigInteger x, BigInteger y) => x * y; public BigInteger Divide(BigInteger x, BigInteger y) => x / y; public BigInteger FromInteger(int value) => (BigInteger)value; } public static class TestGenericNums { public static void Test() { var a = DoubleIt<NumInt, int>(10); // 20 var b = SquareIt<NumInt, int>(10); // 100 var c = NegateIt<NumInt, int>(10); // -10 var t = DoubleIt<NumDouble, double>(10); // 20 var u = SquareIt<NumDouble, double>(10); // 100 var v = NegateIt<NumDouble, double>(10); // -10 var x = DoubleIt<NumBigInt, BigInteger>(10); // 20 var y = SquareIt<NumBigInt, BigInteger>(10); // 100 var z = NegateIt<NumBigInt, BigInteger>(10); // -10 } public static A DoubleIt<NumA, A>(A value) where NumA : struct, Num<A> => default(NumA).Add(value, value); public static A SquareIt<NumA, A>(A value) where NumA : struct, Num<A> => default(NumA).Multiply(value, value); public static A NegateIt<NumA, A>(A value) where NumA : struct, Num<A> => default(NumA).Subtract(default(NumA).FromInteger(0), value); }
- colejohnson66 9y agoThat doesn't account for overflow, though...
- lloydjatkinson 9y agoI saw that too! WTF!
- flukus 9y ago> I also use dto's in some endpoints. So i should extend the implementation to support linq on the domain model ( on odata) and return the dto as response. But this is for later i suppose Please don't. I've ended up maintaining far too many abominations where developers got excited about generics and linq rather than solving real problems. You just end up with apps that should be simple and straightforward being a nightmare to maintain. Generics, linq and inheritance all have their uses but if they aren't used sparingly they create more spaghetti. Edit - at the very least, please be careful and use generics as sparingly as possible.
- wtetzner 9y agoDo you have any examples of how generics could create spaghetti code? Given that they aren't control flow constructs, I'm having a hard time imagining such a situation.
- Quarrelsome 9y agoIf you try to use generics too hard you make crazy shit. I'm working on a system that has a Something<T, Y> but the Y inherits from the T and its all just a mad mess that makes no sense at face value. I guess someone thought it was a good idea at some point but the end result is just confusing as hell. Then you realise its just sitting upon 5 really simple Sql tables and you realise its really convoluted. Its all about stepping back and going: "am I trying too hard?". Like that last 10% of making the code base 100% elegant is what ruins it.
- Quarrelsome 9y agoeverything is great... in moderation. That last bit is what everyone fucks up (including myself in the past). E.G. Composition + OOP is way better than "just" OOP.
- mhomde 9y agoBeware though of improperly (or too eagerly) used generics. I've noticed they tend to spread like a cancer throughout a library and reduce the polymorphism of the code. It's usually a good idea to be provide both an generic and a non-generic interface to an object. A good technique I've found is to have the base object be non-generic and then have a generic version that overrides the properties (with new) that you want to use generic types on. (using as against an underlying field for conversion)
- ahoka 9y agoThis is the most horrible advice I have ever seen on HN.
- sharpercoder 9y agoIt is not only good advice, it is required in fair amount of situations because of limitations in the C# language.
- louthy 9y agoIt's terrible advice. What limitations are you thinking of?
- sharpercoder 9y agointerface IValidator<T> { bool Validate(T thingToValidate); } class ThingValidator : IValidator<Thing> {} class ThingValidator : IValidator<OtherThing> {} var validators = GetAllValidators(); What type is `validators`?
- louthy 9y agoThe signature of GetAllValidators must be: GetAllValidators<T>(); So it's whatever type of T you pass to GetAllValidators.
- Cakez0r 9y agoYou would like Rails.