|
If you drift an electric car, are you doing the Electric Slide?
|
|
|
|
|
IMHO, in recent years, the best feature MS has added to the C# is Tuple. It helps make code cleaner and quicker to write.
It can be compared to Generic lists.
Behzad
|
|
|
|
|
Behzad Sedighzadeh wrote: It helps make code cleaner and quicker to write
Quicker to write, Okay!
Cleaner? I don't think so. Create a nested tuple and now you don't know which element you are referring to when you say Item2. It makes code difficult to maintain. So for simple return types that has like 2 or 3 primitive data types tuple is fine, otherwise just create a class.
|
|
|
|
|
GKP1992 wrote: Create a nested tuple and now you don't know which element you are referring to when you say Item2 Tuple "items" can be named, so:
var name = GetMyName(); where:
(string firstName, string lastName) GetMyName()
{
return ("Marc", "Clifton");
}
I can use name.firstName and name.lastName
Most of the time.
|
|
|
|
|
|
Yes
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
Yes, tuples are great, especially as a replacement for out string foo and I use them primarily for returning multiple things for rather low level methods when out or a C# class/struct is just overkill. The fact that the tuple parameters can be named was a huge advancement, rather than having to use Item1 , Item2 , etc.
That said, I use them judiciously and always ask myself, if I'm using a tuple here, is that the right approach or am I compensating for a possibly bad "design."
For example (this from code I have in a library):
public (HttpStatusCode status, string content) Get(string url, Dictionary<string, string> headers = null)
{
var client = RestClientFactory();
var request = new RestRequest(url, Method.Get);
headers?.ForEach(kvp => request.AddHeader(kvp.Key, kvp.Value));
RestResponse response = client.Execute(request);
return (response.StatusCode, response.Content);
}
Why am I parsing out the status code and content instead of just returning the response object? One answer is that returning response may probably require a using RestSharp and even a reference to the RestSharp package in the caller project.
OK, maybe that's a defensible argument, maybe not. After using this library of mine (REST is just one small part of this library) I'm not that thrilled with my initial wrapper implementation.
But because I started this "pattern", it continues, like:
public (T item, HttpStatusCode status, string content) Get<T>(string url, Dictionary<string, string> headers = null) where T : new()
{
var client = RestClientFactory();
var request = new RestRequest(url, Method.Get);
headers?.ForEach(kvp => request.AddHeader(kvp.Key, kvp.Value));
RestResponse response = client.Execute(request);
T ret = TryDeserialize<T>(response);
return (ret, response.StatusCode, response.Content);
}
And this illustrates mashing together various potentially bad implementation/designs. The tuple now returns three things, and the TryDeserialize catches exceptions silently, returning a null for T item , and what if I want the actual deserialization exception?
And now that I look at that code again after a couple years, what's with that AddHeader loop when there's a perfectly fine AddHeaders method?
Gads, I wish there were people smarter than me at work that could review my code!
Anyways, thanks for reading this longish post.
|
|
|
|
|
I think there are very few people smarter than you.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I don't use them much. I usually prefer to create a class. It kinda depends on how important the thing is.
|
|
|
|
|
Wordle 1,187 5/6
β¬β¬π©β¬β¬
β¬β¬π©β¬β¬
β¬π©π©π©π©
β¬π©π©π©π©
π©π©π©π©π©
|
|
|
|
|
Wordle 1,187 4/6*
β¬π¨β¬β¬β¬
π¨β¬β¬β¬β¬
β¬π©π©π©π©
π©π©π©π©π©
Happiness will never come to those who fail to appreciate what they already have. -Anon
And those who were seen dancing were thought to be insane by those who could not hear the music. -Frederick Nietzsche
|
|
|
|
|
Wordle 1,187 X/6*
β¬π¨β¬β¬β¬
π¨β¬β¬β¬β¬
β¬β¬π©β¬π©
β¬π©π©π©π©
β¬π©π©π©π©
β¬π©π©π©π©
|
|
|
|
|
Damn I hate when it happens
GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++* Weapons extension: ma- k++ F+2 X
The shortest horror story: On Error Resume Next
|
|
|
|
|
Wordle 1,187 5/6*
β¬β¬β¬β¬β¬
β¬π©β¬β¬β¬
β¬π©β¬β¬π©
β¬π©π©β¬π©
π©π©π©π©π©
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
β¬β¬β¬β¬β¬
β¬β¬β¬π¨β¬
β¬β¬π¨β¬β¬
β¬π©π©π©π©
π©π©π©π©π©
In a closed society where everybody's guilty, the only crime is getting caught. In a world of thieves, the only final sin is stupidity. - Hunter S Thompson - RIP
|
|
|
|
|
Wordle 1,187 5/6*
β¬β¬β¬β¬β¬
β¬β¬β¬β¬β¬
π¨π©β¬β¬π©
β¬π©π©π©π©
π©π©π©π©π©
|
|
|
|
|
Wordle 1.187 4/6
β¬β¬β¬π©π©
β¬β¬β¬β¬β¬
β¬π©β¬β¬β¬
π©π©π©π©π©
I've been so fast it's almost been a speedrun
GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++* Weapons extension: ma- k++ F+2 X
The shortest horror story: On Error Resume Next
|
|
|
|
|
Wordle 1,187 6/6
β¬π©β¬β¬β¬
β¬π©π©β¬β¬
β¬π©π©β¬π©
β¬π©π©β¬π©
β¬π©π©π©π©
π©π©π©π©π©
Phew...
|
|
|
|
|
Wordle 1,187 6/6
β¬β¬β¬β¬β¬
β¬β¬π¨β¬β¬
β¬π©β¬π©π¨
π¨π©β¬β¬π©
β¬π©π©π©π©
π©π©π©π©π©
|
|
|
|
|
Wordle 1,187 5/6
β¬β¬β¬β¬β¬
β¬β¬β¬β¬β¬
β¬π¨π¨β¬β¬
β¬π©π©π©π©
π©π©π©π©π©
First two rows with nothing??
This day is not starting well!
Ok, I have had my coffee, so you can all come out now!
|
|
|
|
|
What a man
David Attenborough
In a closed society where everybody's guilty, the only crime is getting caught. In a world of thieves, the only final sin is stupidity. - Hunter S Thompson - RIP
|
|
|
|
|
He is indeed a monument! I guess very few living people deserve as much to have a research vessel named after them. RRS Sir David Attenborough[^]
Mircea
|
|
|
|
|
We've watched quite a few of his programs and really like them.
A home without books is a body without soul. Marcus Tullius Cicero
PartsBin an Electronics Part Organizer - Release Version 1.4.0 (Many new features) JaxCoder.com
Latest Article: EventAggregator
|
|
|
|
|
|
Quote: These two books are essential to me.
I see that, because of the royalties.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|