some thoughts about immutable programming

ajoo 2002-05-07 07:23:21
Today, when I was reading C++ View, I saw an interesting example.
Description:
we have a list in memory that holds all the friends we have. We'll also need to add it to database.
Suppose we have a method
void addFriend(Person p)
{
friends.add(p);
addToDb(p);//if exception's thrown out, we need to undo in friends!
}
The problem exists when either friends.add() or addToDb() throws exception. then we have to undo the changes we've made to db and to the list.

According to the author, "try-catch" can work, but it'll become very tedious and hard to maintain when the number of statements requiring "try-catch" scales up. So a transaction object should be used here.

Here, although transaction object can work, but it still requires us to write an undo operation for each one, and sometimes it's not trivial to write an undo method.

So what do I suggest?
Although db operation cannot be immutable, in-memory operation certainly can! If we do immutable programming, we don't need to undo at all!
don't believe?
Suppose the friends list is an immutable structure, i.e.
List
List cons(Person);

where list.cons(p) only returns a new list instance without touching the old instance. (curious how this can be implemented by constant-time and linear storage? I'll give it later)

ok, if that's true, what can we do to the addFriend method?
void addFriend(Person p)
{
List tmp = friends.cons(p);
addToDb(p);
friends = tmp;
}
Ha ha, does that solve the undo easily?



Here is an immutable list impl

public interface List
{
public List cons(Object obj);
public Object getFirst()throws java.util.NoSuchElementException;
public List getTail();
public boolean isEmpty();
private static final EmptyList singleton = new EmptyList();
public static List instance(){return singleton;}

}
class ConsList
{
public List cons(Object obj){return new ConsList(obj, this);}
public Object getFirst(){return hd;}
public List getTail(){return tl;}
public boolean isEmpty(){return false;}
private final Object hd;
private final List tl;
ConsList(Object hd, List tl){this.hd = hd; this.tl = tl;}
}
class EmptyList
{
public List cons(Object obj){return new ConsList(obj, this);}
public Object getFirst(){throw new java.util.NoSuchElementException();}
public List getTail(){throw new java.util.NoSuchElementException();}
public boolean isEmpty(){return true;}
EmptyList(){}
}

...全文
50 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangwenyou 2002-05-17
  • 打赏
  • 举报
回复
唉,倒霉:(
呵呵,不行就来这
http://cosoft.org.cn/html/
活脱一个它的翻版:)
再不行就到IBM DW的开源站点吧
http://www-124.ibm.com/developerworks/oss/
麻烦:)
ajoo 2002-05-17
  • 打赏
  • 举报
回复
Got denied by sourceforge. :<
I chose the "web-host" for the license. And they don't accept it.

But the license page said they don't take any legal responsibility regarding the license. I'm afraid I may step into some legal trap if I choose a license randomly.
ajoo 2002-05-14
  • 打赏
  • 举报
回复
created one. Have to wait for the approval.
wangwenyou 2002-05-14
  • 打赏
  • 举报
回复
好东西:)资源共享无限!
据我所知jakarta.apache.org的Commons现在也有一个未发布的子项目是做Collection的,也实现了Bag、Heap等,具体实现我还没仔细看,源码可以通过Cvs取到,ajoo(jet pig)兄不妨去看看。
另外,我对你的实现也很感兴趣,相信也有人和我一样感兴趣,所以不建议你一份一份的发邮件,最好到http://sourceforge.net/去开一个开源项目,那样会好些:)
Kert_ake 2002-05-14
  • 打赏
  • 举报
回复
sp
wangwenyou 2002-05-14
  • 打赏
  • 举报
回复
好!:)
ajoo 2002-05-14
  • 打赏
  • 举报
回复
found a popular collection library that supports immutable data structures.
http://gee.cs.oswego.edu/dl/classes/collections/

With this library, you can have immutable map, immutable sequence etc.

(I also have my own impl of immutable map, which is based on JDK Hashtable and supports both synchronized and non-synchronized version of map, if you guys want it, let me know)
ajoo 2002-05-13
  • 打赏
  • 举报
回复
upupupup
babysloth 2002-05-11
  • 打赏
  • 举报
回复
在C++中,由于有运算符重载及析构函数,可以采用一种惯用法RAII(资源获取即初始化),借助smart pointer来完成对资源同步的管理,见C++ VIEW第5期Andrei那篇《Generic<Programming>:简化异常安全代码》。至于java,资源的回收管理被GC垄断,而资源同步的确是个令人头疼的问题:-)
Mike_sun 2002-05-11
  • 打赏
  • 举报
回复
en
ajoo 2002-05-11
  • 打赏
  • 举报
回复
smart pointer with ref-counting has troubles; smart pointer without ref-counting has other troubles.
I was just saying "借助smart pointer来完成对资源同步的管理" is not so perfect.
Hey, wait a minute! use smart pointer for resource sync?
That's not what you mean, right?
babysloth 2002-05-11
  • 打赏
  • 举报
回复
资源管理在哪里都头疼:-)
《Generic<Programming>:简化异常安全代码》中给出的那个smart pointer ---- scopeguard类,相当出色。它跟reference counting实在没有什么关系,也不要转换成pointer,当然,更是利用了跨越函数作用域自动调用析构函数的特点,实现异常安全。

那三点都与smart pointer这一概念无关。
ajoo 2002-05-11
  • 打赏
  • 举报
回复
the example is from that article.
the problem with his approach in the article is that it still requires you to write undo method. And undo is nontrivial sometimes.

In java, resource sync is harder than in C++? (it's not that flexible, but much easier!)
ajoo 2002-05-11
  • 打赏
  • 举报
回复
don't compare smart pointer with gc.
1. smart pointer has problem when it goes across function boundary.
2. it needs ref-counting. And ref-counting is nortoriously inefficient and circular-ref is a nightmare for ref-counting. (the very original and naive gc implementation was using ref-counting)
3. smart pointer is not a pointer itself. you achieve some syntactic convenience, but sometimes, it confuses programmers.
ajoo 2002-05-10
  • 打赏
  • 举报
回复
same idea can be applied to array. You could also implement an "almost immutable array"
The tech is called "update list"
ajoo 2002-05-10
  • 打赏
  • 举报
回复
good challenge!

immutable programming don't have array :<

as far as I know, functional languages only have list (ML has an array extension, but that violates the functional principle)
What those functional languages claim is: maintainability is more important than performance.

But, you can implement an "almost immutable hashtable".
"almost" means, the internal impl is mutable, but the observable behavior is immutable.
wangwenyou 2002-05-10
  • 打赏
  • 举报
回复
很好的解决方法,Immutable只在Patterns In Java中见过,且未消化吸收,所以,我想,能给你的,只有赞誉之辞:)谢谢你给我上了一课!
jimjxr 2002-05-10
  • 打赏
  • 举报
回复
If your list is not based on a linked list, but based on, say an array. Won't the cost of copying be very expensive?
wangwenyou 2002-05-09
  • 打赏
  • 举报
回复
今晚要走了,明天细细品味:)
ajoo 2002-05-09
  • 打赏
  • 举报
回复
Give more comments or critics bah. Argument is a good way to clear things up.

I can't give points just because I'm very flattered. :->
加载更多回复(4)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧