Click here to Skip to main content
15,867,686 members
Home / Discussions / C#
   

C#

 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
User987432-Jan-18 23:06
professionalUser987432-Jan-18 23:06 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
Marc Clifton6-Jan-18 4:19
mvaMarc Clifton6-Jan-18 4:19 
AnswerRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
BillWoodruff4-Jan-18 23:07
professionalBillWoodruff4-Jan-18 23:07 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
User987435-Jan-18 13:18
professionalUser987435-Jan-18 13:18 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
BillWoodruff5-Jan-18 21:13
professionalBillWoodruff5-Jan-18 21:13 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
Marc Clifton6-Jan-18 4:14
mvaMarc Clifton6-Jan-18 4:14 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
BillWoodruff6-Jan-18 7:40
professionalBillWoodruff6-Jan-18 7:40 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
Marc Clifton8-Jan-18 3:03
mvaMarc Clifton8-Jan-18 3:03 
BillWoodruff wrote:
Perhaps you are suggesting using Tuples with multiple Types like a property-bag ?


Not really, but keep in mind that my experience with FP is limited.

BillWoodruff wrote:
I can't quite see how F# would, inherently, remove a need to have complex POCOS: if a POCO gotta have a lotta fields or properties


One of the differences between imperative and functional programming is the concept of currying and partial application. Currying - you call a function with only the first subset of parameters, partial application - you create a function that itself calls a function requiring additional parameters. In either case, what you get back from currying is a function (you can think of it as "waiting for the rest of the parameters") or with partial application, you've defined a function that is "waiting for parameters").

Given that capability, the need for POCO with lots of properties (or fields in FP parlance) often becomes unnecessary. A simple example:
class BirthDate
{
  public int Month {get;set;}
  public int Day {get;set;}
  public int Year {get;set;}
}

class Horoscope
{
  public string GetHoroscope(BirthDate date);
  // Alternatively:
  public string GetHoroscope(int y, int m, int d);
}

Here, I've used BirthDate as a container for 3 fields, or without the container, as three parameters. In either case, calling GetHoroscope requires that all three parameters are known before making the call, so they're wrapped in a container or perhaps in local variables.

In F#, you don't need that. You can do this:

let GetHoroscope y m d = [implementation details]

Let's say you have a simple text-based UI (pseudo-codish):
let curried1 = GetHoroscope PromptForYear
let curried2 = curried1 PromptForMonth
let text = curried2 PromptForDay

Notice a few things here. First of all, for y, m, and d parameters, I'm passing in a function "PromptFor...", not an integer variable.

Secondly, curried1 and curried2 are functions expecting day and year, and year, respectively.

Where are the variables, fields, properties? They exist ONLY in [implementation details] -- the caller never creates a variable, never creates a container, etc.

So, one of the reasons POCO with lots of fields is not necessary is that those fields, in FP, are actually implemented as functions in FP, called as needed. While the example can be written imperatively:

GetHoroscope(PromptForYear(), PromptForMonth(), PromptForDay());

The problem here, with the imperative code, is that "how" you're getting those values is hard-coded -- there's no easy way to curry the C# method GetHoroscope. Even more interestingly, curried1 and curried2 can be returned as functions!

Currying, and partial function application, in C#, becomes a mess.

So one has to get out of the mindset of thinking of data, not as values but instead data as functions. That is NOT easy to do!
Latest Article - Code Review - What You Can Learn From a Single Line of Code

Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

Artificial intelligence is the only remedy for natural stupidity. - CDP1802

GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
BillWoodruff8-Jan-18 5:18
professionalBillWoodruff8-Jan-18 5:18 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
User9874315-Jan-18 4:10
professionalUser9874315-Jan-18 4:10 
AnswerRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
Marc Clifton6-Jan-18 3:56
mvaMarc Clifton6-Jan-18 3:56 
QuestionC# Progammer Forum for Beginners? Pin
User987432-Jan-18 21:27
professionalUser987432-Jan-18 21:27 
AnswerRe: C# Progammer Forum for Beginners? Pin
Ralf Meier2-Jan-18 21:55
professionalRalf Meier2-Jan-18 21:55 
AnswerRe: C# Progammer Forum for Beginners? Pin
OriginalGriff2-Jan-18 22:09
mveOriginalGriff2-Jan-18 22:09 
GeneralRe: C# Progammer Forum for Beginners? Pin
Pete O'Hanlon2-Jan-18 22:15
subeditorPete O'Hanlon2-Jan-18 22:15 
GeneralRe: C# Progammer Forum for Beginners? Pin
OriginalGriff2-Jan-18 22:36
mveOriginalGriff2-Jan-18 22:36 
GeneralRe: C# Progammer Forum for Beginners? Pin
Pete O'Hanlon2-Jan-18 22:43
subeditorPete O'Hanlon2-Jan-18 22:43 
GeneralRe: C# Progammer Forum for Beginners? Pin
OriginalGriff2-Jan-18 22:49
mveOriginalGriff2-Jan-18 22:49 
GeneralRe: C# Progammer Forum for Beginners? Pin
Dave Kreskowiak3-Jan-18 2:23
mveDave Kreskowiak3-Jan-18 2:23 
GeneralRe: C# Progammer Forum for Beginners? Pin
OriginalGriff3-Jan-18 2:33
mveOriginalGriff3-Jan-18 2:33 
GeneralRe: C# Progammer Forum for Beginners? Pin
Dave Kreskowiak3-Jan-18 4:18
mveDave Kreskowiak3-Jan-18 4:18 
GeneralRe: C# Progammer Forum for Beginners? Pin
OriginalGriff3-Jan-18 4:46
mveOriginalGriff3-Jan-18 4:46 
GeneralRe: C# Progammer Forum for Beginners? Pin
Dave Kreskowiak3-Jan-18 4:56
mveDave Kreskowiak3-Jan-18 4:56 
GeneralRe: C# Progammer Forum for Beginners? Pin
User987432-Jan-18 22:36
professionalUser987432-Jan-18 22:36 
GeneralRe: C# Progammer Forum for Beginners? Pin
Pete O'Hanlon2-Jan-18 22:40
subeditorPete O'Hanlon2-Jan-18 22:40 

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.