Monday, September 8, 2008

DON'T generate too much temperate variables in c++

writing too much java or ruby makes people too comfortable. the prof asked me to rewrite the implementation in c++, so i turned to this these days. last time, i used c++ was back to the 2003. still undergraduate freshman. no experience with any programming language expect some qbasic. c++ to me at that time was ok. after that turned to java, then c++ turns to me very ugly. lots of things you need to consider, memory leaks, meaningless compiler errors, one mistake leads hundres of error complaint and etc. after 5 years, c++ to me know, changed a lot. namespaces give somewhat name seperations, stl seems not that bad, boost provides many features whoever no binary which needs compilation yourself == at least 2 hours if you're lucky. after struggling several days, i still did not feel any beauty in it, though i'll still use it.

don't generate too much temperate variables in c++ since it has some copy constructor. if you defined a copy constructor, then any place the copy construction happens, the constructor will be used. but the problem is, without some knowledge about the circumenstance copy happens, the program is really tricky. programming methdology changed to agile these days. today lots of people do programming using those friendly langugages, can just refactoring without considering too much. however in c++, the situation is different. some step in refactoring leads to tricky problems, the reason of this maybe is its advantage : too many details. like this copy constructor.

the class i'm working with is some basic data class representing IP concept. at first, only id and address, like IP(0, 192.168.1.1), since i wanna use stl, for extension purpose, i override the default copy constructor. however, since the algorithm becomes complicated, some container sturcture added to the IP class, for example the history (map<int, vector<Site> > ). by forgetting modify the copy constructor, and my body reflection still in java and ruby something like that, the tricky happens:

Ip aip = system.getAllIps.findById(1)
aip.modify_history

guess what, if you print system.getAllIps.findById(1).history, no modification happened!! the sin is that aip temporate variable. in java, it is just a reference, to the real place of that object. however, the assignment operation by default will call that copy constructor and generate one brand new object to you. as i forgot that, 1 hour spent on it. after reminding some memory back to five years ago, then this was fixed. T, T

too many detail for a programming language to be industry wise used, is it a good idea or not ? maybe yes, maybe not. that's the powerness and productivity trade off in programming language design. most of application programmers who have not that much consideration on performance trend to be like those friendly. system programmer who has tough performance requirment tend to go to powerful first.

anyway i still do not like it very much. too many detail but friendless is one reason, there're more.

No comments:

Post a Comment