Convention for marking methods as pure functions in Java

Reading code of some complex application I thought it could often be helpful to recognize if a method has side effects just by looking on its signature. And after inspecting some of such methods I thought it could be nice to mark them as purely functinal or not in order to make life easier for people reading this code in future.

Is there some convention in Java world (javadoc, method naming pattern etc.) which identifies method as pure function?


You have the JavaBeans Conventions. See http://docstore.mik.ua/orelly/java-ent/jnut/ch06_02.htm

. The JavaBeans framework facilitates this process by establishing naming conventions. One such convention, for example, is that the getter and setter accessor methods for a property should begin with get and set.

However, you can always javadoc or annotate your methods like for example.

/** 
 * This method is read only!
 */
@ReadOnly
public void setX(Object x)

The programmatic/safe way, would be using polymorphism, implementing "read only" and "read/write" interfaces. Related: How to create two interfaces to a Java class one read-only, one read-write?

IMO, if you dont want to mess with interfaces, the JavaBeans convention and/or the javadoc should be enough to guess what functions have side effects or not.

链接地址: http://www.djcxy.com/p/8852.html

上一篇: 如何记录(简单)Java方法的先决条件?

下一篇: 在Java中将方法标记为纯函数的惯例