Proud of Functional Programming



  • Functional Programming has recently become a fashion. Many developers look into that topic, and some need to show off with their freshly acquired knowledge.
    There are some guys asking questions on StackOverflow or CodeProject for that purpose, e.g. https://softwareengineering.stackexchange.com/questions/350230/am-i-too-clever-to-be-readable-by-jr-devs-too-much-functional-programming-in or https://www.codeproject.com/Messages/5412461/Is-this-madness-The-pursuit-of-single-statement-me.aspx

    Bernie has not yet produced enough functional code to brag with it. So he decided for a different way: he analyzes the second example and submits it to The Daily WTF's sidebar.

    The author of that post proudly presents a refactoring of

    void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e)
    {
    // Create a set to hold a collection of lines that have already been processed.
    SortedSet<T> seen = new SortedSet<T>();
    // Create a StringBuilder to avoid the performance penalty of concatenating strings repeatedly.
    StringBuilder stringBuilder = new StringBuilder();
    // Split the text from the text box by lines and examine each one.
    foreach (string line in this.InputTextBox.Text.Split(new char[] { '\r', '\n' }))
    {
    // If the string is successfully added to the set, then it has not been processed before.
    if (seen.Add(line))
    {
    // Add the line to the output.
    stringBuilder.AppendLine(line);
    }
    }
    // Build the string and assign it to the text box.
    this.InputTextBox.Text = stringBuilder.ToString();
    }
    to
    void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e) => this.InputTextBox.Text = this.InputTextBox.Text.SplitLines().Unique().Aggregate(new StringBuilder(), (sb, line) => sb.AppendLine(line), sb => sb.ToString());

    Bernie sees immediately that that guy is a really smart developer. Any smart developer knows that he can show his knowledge by solving a problem in a one-liner. And of course, he won't break his smartness into pieces by introducing unnecessary linebreaks.

    Nota bene: already Backus' paper on Fortran dating back to 1957 is about the speed of writing code. Only whimps like Uncle Bob came up with the horrible idea of writing verbous multi-line "readable" code.

    Then Bernie looks at the signature of the function: void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e). That is an event handler, pretty much the style he used with VB6 as a junior long ago, and later with Windows Forms. This means that this great code resides in some element of a graphical user interface. The concern of a GUI is presentation. And what about the concern of this code? It removes duplicates from some list of strings. According to the author, that seems to be a concern of presentation, too.

    Oh wait, the EventArgs are RoutedEventArgs. That guy uses WPF. In the old VB6 way, ignoring Model-View-ViewModel. Microsoft allows that, they know that the smart developer will find it too cumbersome to learn XAML and Binding, so he can stick to what he is used to use.

    Bernie is a fan of testing. How can you test that function? By opening a GUI, entering some text into a text box, clicking on a button, and examining the new contents of that text box. That's very easy to automate, isn't it? Just hire some junior or intern.

    Now let's talk about the functional part.
    It is also great from the point of view of functional programming: the result of this void function is a side-effect only (InputTextBox gets changed). That "result" only depends on a state, i.e. the value someone entered into a text box. It does not at all depend on the parameters of the function: both sender and e are totally ignored.

    Now Bernie is confident that he can be proud of his excellence in software development and function programming.



  • @berniethebernie um, unless I missed something the principle point of "pure functions" is to write things without side effects...



  • @berniethebernie said in Proud of Functional Programming:

    ... by introducing unnecessary linebreaks.

    He deserves a special place in hell, where all writing, including signposts, are written in one place on one line and everyone has to learn a necessary skill to pick out the relevant bit of information because no unnecessary line breaks.

    Basically, fuck this guy. I've liked functional programming since I saw jQuery, but languages are not always syntactically well-suited to produce these solutions (e.g. Javascript when you try to create an array out of nothing is especially hard to read as a one-liner).

    Also there are linting rules for maximum-line-length for a fucking reason. I don't care how "smart" these people are, they're not team players and my instinct is not to trust them. </rant>



  • @shoreline said in Proud of Functional Programming:

    (e.g. Javascript when you try to create an array out of nothing is especially hard to read as a one-liner)

    I'm assuming you mean something other than var arr = [];


  • Discourse touched me in a no-no place

    @berniethebernie said in Proud of Functional Programming:

    The author of that post proudly presents a refactoring of

    void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e)
    {
    // Create a set to hold a collection of lines that have already been processed.
    SortedSet<T> seen = new SortedSet<T>();
    // Create a StringBuilder to avoid the performance penalty of concatenating strings repeatedly.
    StringBuilder stringBuilder = new StringBuilder();
    // Split the text from the text box by lines and examine each one.
    foreach (string line in this.InputTextBox.Text.Split(new char[] { '\r', '\n' }))
    {
    // If the string is successfully added to the set, then it has not been processed before.
    if (seen.Add(line))
    {
    // Add the line to the output.
    stringBuilder.AppendLine(line);
    }
    }
    // Build the string and assign it to the text box.
    this.InputTextBox.Text = stringBuilder.ToString();
    }
    to
    void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e) => this.InputTextBox.Text = this.InputTextBox.Text.SplitLines().Unique().Aggregate(new StringBuilder(), (sb, line) => sb.AppendLine(line), sb => sb.ToString());

    Fails the enforced code-style rules, and fails them by a lot. REFACTORING REJECTED WITH PREJUDICE. Please see your manager to schedule a session for “reeducation”.


  • Dupa

    @dkf said in Proud of Functional Programming:

    @berniethebernie said in Proud of Functional Programming:

    The author of that post proudly presents a refactoring of

    void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e)
    {
    // Create a set to hold a collection of lines that have already been processed.
    SortedSet<T> seen = new SortedSet<T>();
    // Create a StringBuilder to avoid the performance penalty of concatenating strings repeatedly.
    StringBuilder stringBuilder = new StringBuilder();
    // Split the text from the text box by lines and examine each one.
    foreach (string line in this.InputTextBox.Text.Split(new char[] { '\r', '\n' }))
    {
    // If the string is successfully added to the set, then it has not been processed before.
    if (seen.Add(line))
    {
    // Add the line to the output.
    stringBuilder.AppendLine(line);
    }
    }
    // Build the string and assign it to the text box.
    this.InputTextBox.Text = stringBuilder.ToString();
    }
    to
    void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e) => this.InputTextBox.Text = this.InputTextBox.Text.SplitLines().Unique().Aggregate(new StringBuilder(), (sb, line) => sb.AppendLine(line), sb => sb.ToString());

    Fails the enforced code-style rules, and fails them by a lot. REFACTORING REJECTED WITH PREJUDICE. Please see your manager to schedule a session for “reeducation”.

    It's not reeducation anymore. It's enfirenment.



  • @kt_ said in Proud of Functional Programming:

    @dkf said in Proud of Functional Programming:

    @berniethebernie said in Proud of Functional Programming:

    The author of that post proudly presents a refactoring of

    void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e)
    {
    // Create a set to hold a collection of lines that have already been processed.
    SortedSet<T> seen = new SortedSet<T>();
    // Create a StringBuilder to avoid the performance penalty of concatenating strings repeatedly.
    StringBuilder stringBuilder = new StringBuilder();
    // Split the text from the text box by lines and examine each one.
    foreach (string line in this.InputTextBox.Text.Split(new char[] { '\r', '\n' }))
    {
    // If the string is successfully added to the set, then it has not been processed before.
    if (seen.Add(line))
    {
    // Add the line to the output.
    stringBuilder.AppendLine(line);
    }
    }
    // Build the string and assign it to the text box.
    this.InputTextBox.Text = stringBuilder.ToString();
    }
    to
    void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e) => this.InputTextBox.Text = this.InputTextBox.Text.SplitLines().Unique().Aggregate(new StringBuilder(), (sb, line) => sb.AppendLine(line), sb => sb.ToString());

    Fails the enforced code-style rules, and fails them by a lot. REFACTORING REJECTED WITH PREJUDICE. Please see your manager to schedule a session for “reeducation”.

    It's not reeducation anymore. It's enfirenment.

    Out of a clue cannon. Into the sun.



  • @berniethebernie You could also mention he's using a textbox to store what appears to be a list. So he's an expert at GUI design, also.

    I wonder if there are any widgets designed to store lists. Like in a box. They could call it "BoxOfLists".



  • @shoreline said in Proud of Functional Programming:

    He deserves a special place in hell, where all writing, including signposts, are written in one place on one line and everyone has to learn a necessary skill to pick out the relevant bit of information because no unnecessary line breaks.

    He's also going against established C# style, which would be this more-or-less:

    void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e) => this.InputTextBox.Text = this.InputTextBox.Text.SplitLines()
        .Unique()
        .Aggregate(new StringBuilder(), (sb, line) => sb.AppendLine(line), sb => sb.ToString());
    

    Still pretty bad. You could line break in front of => but that's less typical.


  • Impossible Mission - B

    @blakeyrat said in Proud of Functional Programming:

    I wonder if there are any widgets designed to store lists. Like in a box. They could call it "BoxOfLists".

    Nah. In true geek style, it should properly be named "Box of Listing."


  • Dupa

    @blakeyrat said in Proud of Functional Programming:

    @shoreline said in Proud of Functional Programming:

    He deserves a special place in hell, where all writing, including signposts, are written in one place on one line and everyone has to learn a necessary skill to pick out the relevant bit of information because no unnecessary line breaks.

    He's also going against established C# style, which would be this more-or-less:

    void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e) => this.InputTextBox.Text = this.InputTextBox.Text.SplitLines()
        .Unique()
        .Aggregate(new StringBuilder(), (sb, line) => sb.AppendLine(line), sb => sb.ToString());
    

    Still pretty bad. You could line break in front of => but that's less typical.

    @blakeyrat said in Proud of Functional Programming:

    @shoreline said in Proud of Functional Programming:

    He deserves a special place in hell, where all writing, including signposts, are written in one place on one line and everyone has to learn a necessary skill to pick out the relevant bit of information because no unnecessary line breaks.

    He's also going against established C# style, which would be this more-or-less:

    void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e) => this.InputTextBox.Text = this.InputTextBox.Text.SplitLines()
        .Unique()
        .Aggregate(new StringBuilder(), (sb, line) => sb.AppendLine(line), sb => sb.ToString());
    

    Still pretty bad. You could line break in front of => but that's less typical.

    If it's multi line, better put it inside of the braces.

    But that's not hip enough!


  • Dupa

    @masonwheeler said in Proud of Functional Programming:

    @blakeyrat said in Proud of Functional Programming:

    I wonder if there are any widgets designed to store lists. Like in a box. They could call it "BoxOfLists".

    Nah. In true geek style, it should properly be named "Box of Listing."

    Boxing of listings, maybe?


  • 🚽 Regular

    @arantor said in Proud of Functional Programming:

    @berniethebernie um, unless I missed something the principle point of "pure functions" is to write things without side effects...

    Functional programming, fluent programming... it's all words. 🧐



  • @berniethebernie said in Proud of Functional Programming:

    Functional Programming has recently become a fashion.

    Yeah, in like 2005.


  • Impossible Mission - B

    @captain According to Wikipedia,

    LINQ was released as a major part of .NET Framework 3.5 on November 19, 2007.

    So I'd mark 2008 as the point where it started to become fashionable in mainstream development.


  • Winner of the 2016 Presidential Election

    @berniethebernie
    You remind me of a colleague of mine who insists of using Java streams for everything since we upgraded to 8. I really like that guy, but his code review comments ("this could be a one-liner" - yes, I know, but I deliberately chose not to write it that way!) are really annoying.



  • @berniethebernie said in Proud of Functional Programming:

    this great code resides in some element of a graphical user interface. The concern of a GUI is presentation. And what about the concern of this code? It removes duplicates from some list of strings. According to the author, that seems to be a concern of presentation, too.
    Oh wait, the EventArgs are RoutedEventArgs. That guy uses WPF. In the old VB6 way, ignoring Model-View-ViewModel. Microsoft allows that, they know that the smart developer will find it too cumbersome to learn XAML and Binding, so he can stick to what he is used to use.

    I used WPF a lot and while I used the databinding all the time, I found the command binding too cumbersome.



  • I was really confused why Boner was calling himself Bernie.



  • @marczellm That can be the case, depending on how you do command binding. I prefer to use a delegatecommand, which pretty much solves that.


  • :belt_onion:

    @shoreline said in Proud of Functional Programming:

    He deserves a special place in hell, where all writing, including signposts, are written in one place on one line and everyone has to learn a necessary skill to pick out the relevant bit of information because no unnecessary line breaks.

    I visited a Spanish-speaking country relatively recently and I'm pretty sure that hell already exists, in verbal form at least. I thought vocabulary was going to be my problem (I'm too old to learn new languages) and instead it was figuring out where the breaks were between words, much less sentences.



  • Was this supposed to be a front page submission? Or some bizarre spam?



  • @heterodox said in Proud of Functional Programming:

    @shoreline said in Proud of Functional Programming:

    He deserves a special place in hell, where all writing, including signposts, are written in one place on one line and everyone has to learn a necessary skill to pick out the relevant bit of information because no unnecessary line breaks.

    I visited a Spanish-speaking country relatively recently and I'm pretty sure that hell already exists, in verbal form at least. I thought vocabulary was going to be my problem (I'm too old to learn new languages) and instead it was figuring out where the breaks were between words, much less sentences.

    If only there were a way to indicate whitespace in speech*... 🤔

    I think parsing apart words is one of the hardest things about listening to an unfamiliar language, along with the corollary of being able to smudge words together "properly" when speaking it.

    * And we could get everyone to use it. 😐



  • @heterodox reminds me of listening to a certain ADHD Belarusian speak Russian. At the time I could barely understand normal people, and this dude was speaking a million words per second (:pendant: not really) and clipping the endings, jamming his words together. It was exhausting.



  • @heterodox said in Proud of Functional Programming:

    I thought vocabulary was going to be my problem (I'm too old to learn new languages) and instead it was figuring out where the breaks were between words, much less sentences.

    That is one of the hardest parts of trying to understand you english speakers too.


  • Discourse touched me in a no-no place

    @wharrgarbl I remember working with a (very competent) Spanish lady at Telefonica who was one of those people who managed to get a hundred words out in a single breath without stopping at all. Her boss was more inclined to saying as little possible. I suppose they averaged out to normal. ;)



  • @benjamin-hall said in Proud of Functional Programming:

    @heterodox reminds me of listening to a certain ADHD Belarusian speak Russian. At the time I could barely understand normal people, and this dude was speaking a million words per second (:pendant: not really) and clipping the endings, jamming his words together. It was exhausting.

    Reminds me of a 5hr COBOL class I took in Jr college (back in 1981). It started at 8a on Sat (once a week). I got off work at 7a (yeah, 3rd shift). The prof had such a thick accent it was really hard to understand him. Which was good - it kept me from falling asleep!


Log in to reply