When to use functional programming approach and when not? (in Java)

let’s assume I have a task to create a Set of class names. To remove duplication of .getName() method calls for each class, I used org.apache.commons.collections.CollectionUtils and org.apache.commons.collections.Transformer as follows:

Snippet 1:

Set<String> myNames = new HashSet<String>();
CollectionUtils.collect(
        Arrays.<Class<?>>asList(My1.class, My2.class, My3.class, My4.class, My5.class),
        new Transformer() {
            public Object transform(Object o) {
                return ((Class<?>) o).getName();
            }
        }, myNames);

An alternative would be this code:

Snippet 2:

Collections.addAll(myNames, My1.class.getName(), My2.class.getName(), My3.class.getName(), My4.class.getName(), My5.class.getName());

So, when using functional programming approach is overhead and when it’s not and why?

Isn’t my usage of functional programming approach in snippet 1 is an overhead and why?

8

As others noted, you can’t really use a functional approach in Java, because it is way too verbose and simply lacks a lot of necessary features. So the resulting code will usually look more complex and ugly. Sometimes it is still worth it—because the alternative would include even more duplication and/or other ugliness—sometimes it isn’t. You need to decide on a case by case basis. Try out different approaches and choose the one resulting in the cleanest, simplest code. And don’t bother too much with labels. 🙂 Writing code in “functional” style is not superior per se than any other style—only if your code is actually shorter, cleaner, easier to maintain and extend and/or more efficient in some significant way.

This brings us to another important point. Even if you managed to implement some piece of code in a “functional” way really succinctly, you need to look at it from the point of view of the future maintainers. Will an average Java programmer understand your piece of code if it is so different from the mainstream and well-known Java idioms? Most likely not, which increases the probability of them introducing bugs during future development/maintenance. Your code must have very strong and provable advantages to counterbalance this, and you need to document it well.

5

Java was not designed for functional programming and so, while you can use a functional programming style in Java (immutable state, recursion, function composition, high-order functions, …), you will often miss certain features that facilitate functional programming (ad-hoc syntactic constructs, currying, function composition operator, tail-call optimization, …).

Therefore, even though a functional style in Java can make your code (sometimes) more readable or (more often) more robust, if you really want to use it heavily you’d better use a language that was designed for it. For the JVM you can use Scala or Clojure (here is a short article, that also discusses Groovy). Otherwise, you can try Haskell, Common Lisp, Scheme, F#, Ocaml, and others.

Functional approach usually gives you a better looking, shorter code. But dealing with immutability may result in a performance penalty. I think first snippet looks like overhead because functional syntax in Java is too verbose.

4

Theoretically a functional approach is simpler, more versatile, more verifiable, and easier to understand at-a-glance. If what you end up with doesn’t hit at least one of those goals, then perhaps it’s not the best approach for your circumstances.

Note that functional is, as previously mentioned, a bit of a reach for Java. But not for the JVM. Scala and Groovy are a bit more flexible in that regard and allow you to express functional concepts much more succinctly. And if you want to to all-out functional, there’s always Clojure. All of these target the JVM and should integrate reasonably well into an existing Java project.

If you’re stuck with a Java environment (JVM) but are getting tired of the language itself, I’d recommend trying out Scala or Groovy. It’s a bit like Java with a more modern syntax (or Jython if you’re feeling a twinge of python-envy).

Learning a bit about functional programming can improve the way you write object oriented software. For example the value of immutable objects, lack of side effects and new ways of seeing and appreciating abstractions – favoring ‘verbs’ over ‘nouns’. But as others noted, java is no functional language and you may make the lives of other programmers reading your code hard when using unexpected paradigms.

Have a look at what the authors of google guava have to say about the functional idioms built into their library.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật