Click here to Skip to main content
16,004,587 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralI love Tuples Pin
Behzad Sedighzadeh18-Sep-24 1:47
Behzad Sedighzadeh18-Sep-24 1:47 
GeneralRe: I love Tuples Pin
GKP199218-Sep-24 1:52
professionalGKP199218-Sep-24 1:52 
GeneralRe: I love Tuples Pin
Marc Clifton18-Sep-24 2:11
mvaMarc Clifton18-Sep-24 2:11 
GeneralRe: I love Tuples Pin
PIEBALDconsult18-Sep-24 2:55
mvePIEBALDconsult18-Sep-24 2:55 
GeneralRe: I love Tuples Pin
honey the codewitch18-Sep-24 2:56
mvahoney the codewitch18-Sep-24 2:56 
GeneralRe: I love Tuples Pin
Rob Philpott18-Sep-24 6:45
Rob Philpott18-Sep-24 6:45 
GeneralRe: I love Tuples Pin
raddevus18-Sep-24 7:42
mvaraddevus18-Sep-24 7:42 
GeneralRe: I love Tuples Pin
Marc Clifton18-Sep-24 2:06
mvaMarc Clifton18-Sep-24 2:06 
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? Laugh | :laugh:

Gads, I wish there were people smarter than me at work that could review my code!

Anyways, thanks for reading this longish post.

GeneralRe: I love Tuples Pin
Richard Andrew x6418-Sep-24 4:28
professionalRichard Andrew x6418-Sep-24 4:28 
GeneralRe: I love Tuples Pin
PIEBALDconsult18-Sep-24 2:58
mvePIEBALDconsult18-Sep-24 2:58 
GeneralRe: I love Tuples Pin
TNCaver18-Sep-24 5:14
TNCaver18-Sep-24 5:14 
GeneralRe: I love Tuples Pin
Sander Rossel18-Sep-24 5:26
professionalSander Rossel18-Sep-24 5:26 
GeneralRe: I love Tuples Pin
Jeremy Falcon18-Sep-24 14:07
professionalJeremy Falcon18-Sep-24 14:07 
GeneralRe: I love Tuples Pin
Daniel Pfeffer19-Sep-24 4:50
professionalDaniel Pfeffer19-Sep-24 4:50 
GeneralWordle 1,187 Pin
Shane010317-Sep-24 17:51
Shane010317-Sep-24 17:51 
GeneralWordle 1,187 - 4/6 Pin
ChandraRam17-Sep-24 18:15
ChandraRam17-Sep-24 18:15 
GeneralRe: Wordle 1,187 Pin
GKP199217-Sep-24 18:50
professionalGKP199217-Sep-24 18:50 
GeneralRe: Wordle 1,187 Pin
den2k8817-Sep-24 21:54
professionalden2k8817-Sep-24 21:54 
GeneralRe: Wordle 1,187 Pin
OriginalGriff17-Sep-24 20:12
mveOriginalGriff17-Sep-24 20:12 
GeneralRe: Wordle 1,187 - 5 4 me Pin
pkfox17-Sep-24 20:18
professionalpkfox17-Sep-24 20:18 
GeneralRe: Wordle 1,187 Pin
StarNamer@work17-Sep-24 21:47
professionalStarNamer@work17-Sep-24 21:47 
GeneralRe: Wordle 1,187 4/6 Pin
den2k8817-Sep-24 21:54
professionalden2k8817-Sep-24 21:54 
GeneralRe: Wordle 1,187 Pin
Sander Rossel17-Sep-24 22:28
professionalSander Rossel17-Sep-24 22:28 
GeneralRe: Wordle 1,187 - Phew indeed, too many possibilities! Pin
Rich Leyshon17-Sep-24 23:02
Rich Leyshon17-Sep-24 23:02 
GeneralRe: Wordle 1,187 Pin
Cp-Coder18-Sep-24 1:35
Cp-Coder18-Sep-24 1:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.