DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > How stateless can you go?

How stateless can you go?

Jakub Holý user avatar by
Jakub Holý
·
May. 05, 11 · Java Zone · Interview
Like (0)
Save
Tweet
6.39K Views

Join the DZone community and get the full member experience.

Join For Free

i’ve attended an oslo coding dojo named “how stateless can you go?” lead by thomas k. nilsson . the goal was to write a tostring() method for a tree structure printing nodes with proper indentation w.r.t. their depth and then to make it as stateless as possible without any other regard (such as performance or cleanliness of the code).

it was very interesting to compare the original, stateful version and the resulting stateless one and to see the solution in various languages (haskell, clojure, groovy, c#, java, scala) – it looked actually pretty similar in all.

what i’ve learned is that stateless (i.e. functional-style) code looks much cleaner for you get rid of lot of noise such as local variables and loops. in practice it is important to use a language with an efficient implementation of recursion (especially tail-recursion ) and with data structures that lead themselves easily to recursive processing, i.e. make it easy and efficient to process the first element of a collection and do that recursively for the rest without modifying the collection (and providing utility methods like each). it is of course best to have languages that support map/reduce.

you can check the slides and various solutions at github and see our primitive and stateless implementations below. (we did it in a nearly tdd-manner, but i won’t include the test here as it isn’t essential.)

solution by jakub & anders

the primitive implementation

// ...
@override
public string tostring() {
return tostring(0);
}

private string tostring(final int nesting) {
string tabs = "";
for (int i = nesting; i > 0; i--)
tabs += "\t";

return tabs + this.content + "\n"
+ printchildren(nesting + 1, new linkedlist(this.children));
}

private string printchildren(int nesting, list children) {
string result = "";
for (node child : children) {
result += child.tostring(nesting);
}
return result;
}
// ...

going stateless

loops removed:

// ...
@override
   public string tostring() {
      return tostring("");
   }

   private string tostring(final string indentation) {
      return indentation + this.content + "\n"
         + printlist(indentation + "\t", new linkedlist(this.children));
   }

   private string printlist(string indentation, linkedlist children) {
      if (children.isempty()) return "";
      return children.pop().tostring(indentation) + printlist(indentation, children);
   }
// ...

cloning the list is perhaps not a good thing from the performance point of view, but that wasn’t the concern here; other languages can deal with that much more efficiently than java. anyway i’ve created a version without it (but with ugly integer constants instead):

// ...
   private string tostring(final string indentation) {
      return indentation + content + "\n"
         + printlist(indentation + "\t", children);
   }

   private string printlist(string indentation, list children) {
      if (children.isempty()) return "";
      return children.get(0).tostring(indentation)
         + printlist(indentation, children.sublist(1, children.size()));
   }
// ...

appendix: the node class

the rest of the node class representing the tree to be printed is the same in all our solutions:

package scotsman;

import java.util.linkedlist;
import java.util.list;

public class node {

   private string content;
   private list children;

   public node(string content) {
      this(content, new linkedlist());
   }

   public node(string content, list children) {
      this.content = content;
      this.children = children;
   }

   public node withchild(node child) {
      children.add(child);
      return this;
   }

   // tostring implementation comes here...

}

conclusion

functional programming is cool. being in oslo is cool. :-)

from http://theholyjava.wordpress.com/2011/04/29/how-stateless-can-you-go/

Functional programming Implementation Tree (data structure) Java (programming language) Printing Data (computing) Testing Scala (programming language) Cloning

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Exhaustive JUNIT5 Testing with Combinations, Permutations, and Products
  • What I Miss in Java, the Perspective of a Kotlin Developer
  • Major PostgreSQL Features You Should Know About
  • SQL GROUP BY and Functional Dependencies: a Very Useful Feature

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo