update jtable from inside an event listener: short sample

Note: one event only. In other cases, the event listener (on EDT) often generate cascading events to update other JComponents
Note: the stateful listener, which is common. The simplest listeners are stateless functors.
Note: setModel — “swapping” in a new model

Note the jtable object vs the table model object. Different addresses!

The listener ctor can receive the jtable object and save it as a field —
public SelectionChangeListener(JTable table) {
   this.table = table;
}
// now in the event handler
DefaultTableModel model = new DefaultTableModel(…);
this.table.setModel(model);
this.table.repaint(); // must run on EDT???

pimpl -> bridge pattern

My pimco interviewer (Burak?) pointed out, again, the link between pimpl and bridge pattern. Perhaps he read [[effC++]]

See other posts about pimpl.

Based on whatever little i know about c++ design patterns, i feel bridge is arguably the most powerful, colorful pattern. Here’s one variation of the pattern, featuring bridge-over-2-trees. See [[head first design patterns]] and this detailed yet simple sample code

First you need to thoroughly master the pimpl idiom. Realize the pimp classes provide a service to a client.
-> To grasp the bridge pattern, remember the client is unimportant. It’s outside our picture.
-> Next, looking at the service side of the client-service divide. Realize we have refactored a single service class into an empty public facade[1] + a private impl (therefore PIMPL) class.
-> Next realize both classes can be abstract and subclassed.
-> We end up with 2 trees i.e. 2 class hierarchies. P292Duffy
Note the field in the empty public facade should be a pointer for maximum polymorphism and easy-copy
-> We see a link between the 2 trees since the facade HasA pointer to the pimpl class.
-> That link is the bridge!

[1] imprecisely

For More flexibility, we can use factory to manufacture subclasses. Factory must return pointers to avoid slicing.

event pseudo-field ^ hidden delegate field

— Based on http://www.yoda.arachsys.com/csharp/events.html
First let’s review c# properties. Example – Property “age” in class Animal is really a pair of get()/set() methods manipulating a hidden realAge field. You can do anything in the get() and set(), even ignoring the realAge, or make do without the realAge variable entirely. To your clients, age appears to be a field — a pseudofield.

An event is a pseudofield referring to a hidden delegate field. MyEvent is really a pair of add()/remove() methods to append/remove each delegate (one at a time) from the realDelegate field.

If you declare MyEvent without explicit add/remove, compiler generates them around a hidden delegate field, just like an auto-implemented property.

Compared to properties, there’s some additional syntax complexity. The type of the hidden delegate field is the type mentioned after keyword “event”.

in a param declaration, const and & are both "decorators"

Sound byte — in a param declaration, const and & are 2 standard “decorators”

Q: Func1(const vector & v) { // It’s easy to get distracted by the some_complex_expression, but put it aside — what kind of param is v?
A: v is a reference to some vector object, and you can’t modify object state via this handle. It’s known as a “const reference” but
* This doesn’t mean the object is immutable.
* This only means the object is unmodifiable using this particular handle.

Note — when you pass an arg to this param, you don’t specify const and you don’t specify a reference —
Vector myVector; Func1(myVector); // the compiler will create a 32bit const ref based on the nonref variable myVector

Jargon warning — A “handle” is a loose concept, can be a pointer, a ref, or a nonref variable.

pimpl, phrasebook

pointer – the private implementation instance is held by (smart) pointer, not by reference or by value
** P76 [[c++codingStandards]] suggests boost shared_ptr

big3 – you often need to avoid the synthesized dtor, copier and op=. See P…. [[c++codingStandard]]

FCD – see other posts

encapsulate – the “private” class is free to evolve
** wrapper class vs a private class. The public methods in the wrapper class simplify delegates to the private class.[[c++succinctly]] has a complete Pimpl example showing such “delegations”

shared_ptr (sptr) vs auto_ptr (aptr)

Both of them delete intelligently. Both overload -> to mimic ptr syntax. See http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/cplr329.htm

aptr has unconventional copier/assignment, therefore unacceptable to STL containers; sptr is the only solution I know when storing pointers into containers — not scoped_ptr, not weak_ptr.

sptr is ref-counting; aptr isn’t as each aptr’s ref count is always 1.

aptr is sole-ownership; shared_ptr is shared ownership — that’s why there’s a ref count. shared ref count

shared_ptr ref counter implementation — a non-intrusive doubly-linked list to glue all sister shared_ptr instance. See http://www.boost.org/doc/libs/1_44_0/libs/smart_ptr/smarttests.htm.

Q: can you delete a smart ptr as you do a raw pointer?
%%A: no. a smart ptr is a class template. u can’t call “delete someNonrefVariableOfAnyClass”

Q: if a smart ptr is a field, what happens during destruction?
%%A: see post on DCB. Field destructors are automatically invoked.

Key weakness of ref-count is the cycle. Use weak_ptr instead.

For container usage with inheritance, see http://www.devx.com/cplus/10MinuteSolution/28347/1954

FX vol fitting, according to a practitioner online

— Based on http://quantdev.net/market/bootstrapping/4-buildinganfxvolsurface
The market provide fairly liquid quotes on volatility out to about 2 years for only 3 types of instruments —

ATM Straddle
Strangle
Risk Reversal

The quotes are provided in terms of volatility for a specific delta. For example: a quote will be given for the volatility for a 25 delta Strangle, or a 10 delta Risk Reversal for a specific maturity. In order to construct our volatility surface we need quotes for an ATM Straddle, a 25 delta Strangle and a 25 delta Risk Reversal, and a 10 delta Strangle and a 10 delta Risk Reversal with a range of maturities.

We can imply the volatility for the specific deltas at a particular maturity by using our quotes. The 50 delta implied vol is simply the volatility of the ATM Straddle. An ATM Straddle is a call and a put with the same strike and maturity, and chosen so the delta of the straddle is zero.

The 25 delta call implied volatility is the ATM Straddle volatility + (25 delta Risk Reversal volatility / 2) + 25 delta Strangle.

On a smile curve, the x-axis would include {10delta, 25 delta, 50 delta, 25 delta, 10 delta} in symmetry. The volatility calculated for the 25Δ call (OTM) is the same as that for a 75Δ put (ITM), so for call values you go along the curve from End A to End B, and for put values you would go along the curve from End B to End A.

Based on http://quantdev.net/market/bootstrapping/4-buildinganfxvolsurface says but I doubt “Usually you would then turn the vol curve for each maturity so it is denominated in strike rather than delta, as it then becomes much easier to use in practice given that you know the strike for the option you want to price, but not the delta.

interpreting const var declarations c++

(based on http://www.parashift.com/c++-faq-lite/const-correctness.html)

#1 simple rule — read   B a c k w a r d s
Q: What does ….Fred const * p1…. mean?
A: reading backward, p1 is a ….ptr to a const Fred….. Similarly,
Fred const & r1– means r1 is a …. ref to a const Fred…
Fred * const p — means p is a ….const ptr to a Fred…..
Fred const * const p — means p is a ….const ptr to a const Fred ….

Now we are ready to deal with func declarations.
#2 simple rule — identify var declarations embedded in func declarations. Essentially look at return type declarations and parameter declarations.

Fred const& operator[] (unsigned * const index) const; ← subscript operators often come in pairs

– return variable Fred const& — ref to a const Fred
– param unsigned * const — const ptr to an unsigned, just like a final Object parameter in java — you can’t reseat this ptr.

You can test most of these easily in eclipse.

table constraints

For simple data validation like Price > 0, you can implement in the application loading into the table, or in the application reading from the table, or you can use table constraints.

* reusable — What if another source system (beside LE) needs to load into this table? Table constraints are automatically reusable with zero effort.

* testing — Table constraints are extremely simple and reliable that we need not test them.

* Table constraints are easier to switch on/off. We can drop/add each constraint individually, without source code migration and regression tests

* flexible — we can adjust a table constraint more easily than in application. Drop and create a new constraint. No source code change. No test required. No regression test either.

* modular — table constraints are inherently more modular and less coupled than application modules. Each constraint exists on its own and can be removed and adjusted on its own. They don’t interfere with each other.

* risk — Table constraints are more reliable and there will be less risk of bad data affecting our trailers. Validation in application can fail due to bugs. That’s why people measure “test coverage”.

* gate keeper — Table constraints are more reliable than validations in application. They are gate keepers — There’s absolutely no way to bypass a constraint, not even by bcp.

* visible — Table constraints are more visible and gives us confidence that no data could possibly exist in violation of the rule.

* data quality — You know …. both suffer from data quality. They know they should validate more, but validation in application is non-trivial. Reality is we are short on resources.

* if we keep a particular validation as a table constraint, then we don’t have to check that particular validation inside the loader AND again in the downstream trailer calculator (less testing too). You mentioned reusable valiation module. This way we don’t need any.

* hand-work — In contingency situations, it’s extremely valuable to have the option to issue SQL insert/update/bcp. Table constraints will offer some data validation. From my experience, i have not seen many input feed tables that never need hand work. I believe within 6 months after go-live, we will need hand insert/update on this table.

* Informatica — Informatica is a huge investment waiting for ROI and we might one day consider using it to load lot data. Table constraints work well with Informatica.

* LOE — The more validation we implement in application, the higher the total LOE. That’s one reason we have zero constraint in our tables. We are tight on resources.

* As a principle, people usually validate as early as possible, and avoid inserting any invalid data at all. Folks reading our
tables (perhaps from another team) may not know “Hey this is a raw input table so not everything is usable.” Once we load stuff into
a commissions table, people usually think it’s usable data. Out of our 100+ tables, do you know which ones can have invalid data?

FXO sticky strike vs sticky delta

http://en.wikipedia.org/wiki/Volatility_smile#Evolution:_Sticky is brief and clear.

Heuristics show

Equity vol smile curve is typically stick-to-strike as spot moves. When we re-anchor a surface to a new spot, the curve plotted against Absolute strike looks unchanged, but curve plotted against delta would shift. I think curve plotted against relative strike would shift too. I don’t think it’s common to plot equity vol smile against delta.

FX vol smile curve is typically sticky delta. When we re-anchor a surface to a new spot rate, the curve plotted against delta would stay fairly stable.

C++ references — strict and clean rules

int i2; int & ri = i2;
Cat c; Cat & rc=c;

#1 keyword — a reference is an *ALIAS* or symlink [1] to another variable

* you can take address-of ri. looks the same as i2 i.e.&ri === &i2
* ri can be a lvalue. looks the same as i2, i.e. “ri=222” === “i2=222”. In fact, this also shows you can never reassign a reference to a second target object (different memory location).
* you never need to deference a reference. ri looks the same as i2, without the deref operator “&”.
* when the target of the ref is a user-defined object, then the ref looks the same as the original handle on the object. See P168 [[24]].
* you can put ri on the RHS when initialize a new ref. ri works the same as i2 the original nonref variable.

[1] unix hardlinks have equal status to each other, unlike reference vs referent

A reference has no “separate identity” because it lacks an observable address. Compiler completely hides whatever address a reference occupies.

A reference must be seated at birth and will never be null or reseated. A reference is like your baby boy (Dabao). He has a mother at birth and can’t change his mother. However, a reference can become “invalid” when referent memory is reclaimed.

/*
* The following two lines won’t work because reference has to
* be initialized at the time of declaration
*/
int &debug_level_ref;
debug_level_ref = level;
– so always initialize references
– because references always need to be initialized there could be no NULL references
– references cannot be re-initialized, i.e. throughout its lifetime it will serve as
an alias to the object it was first initialized with

web services j4, features — briefly

web service is an old technology (RPC) given a new lease of life 10 years ago.

* [#1 selling point] cross platform for eg between dotnet frontend and java backend
* loosely coupled
* good for external partner integration. Must be up all the time.
* beats MOM when immediate response is required.
* web service (soap) over MOM? should be feasible. One listener thread for the entire client system — efficiency

some methods should always/never run on EDT

If a method *might* run on EDT, then don’t invokeAndWait() on it. You can invokeLater() on it though. See [[java threads]]
If a method should always/never run on EDT, then those methods should call this assertion method below to make an assertion.
      public static boolean assertIsEDT() {
            boolean isEDT = javax.swing.SwingUtilities.isEventDispatchThread();
            if (!isEDT) {
                  // Always create new Exceptions as local variables, never fields
                  AssertionError e = new AssertionError(“One of the methods in this call stack is “ +
                              “found running on a wrong thread. This method is designed as EDT-only.”);
                  e.printStackTrace();
                  throw e;
            }
            return isEDT;
      }
      public static boolean assertNotEDT() {
            boolean isEDT = javax.swing.SwingUtilities.isEventDispatchThread();
            if (isEDT) {
                  AssertionError e = new AssertionError(“One of the methods in this call stack is “ +
                              “found running on the EDT. This method is designed as a never-EDT method.”);
                  e.printStackTrace();
                  throw e;

            }
            return isEDT;
      }

how would i make a living if i can’t find jobs in IT@@

In one of my Toastmasters’ practices, I asked the audience this same question — “How would you make a living if you can’t find a job in your current industry? You could assume the industry declines considerably (outsourcing), too many new comers competing for dwindling jobs, and your family or health doesn’t allow you to continue in this line.” Here are some of my options:

* librarian
* Life guard, security guard
* assistant restaurant manager, overseeing a bunch of young staff
* work in and then run a small cleaning company
* run a maid agency, or baby-sitter agency
* run a small franchise supermarket, perhaps as a joint investor, but this takes minimum $5K (?) each head. This amount is quite a lot if i am in a dire situation — can’t find job in IT.
* Elderly’s nursing home. Growing demands in greying S’pore.
* cold-calling — perhaps decent investment products from well-known banks. I did cold calling for real and i think i have what it takes.

* newspaper reporter, editor
* ditto for a magazine
* ditto for a tech news site, but not the Web Master (WM is IT).

* Nonprofits do hire people. i think i can help in a few ways including fund-raising (no talent) and hands-on work
~> buddhist organizations do hire people
~> volunteer organizer, a paid job to organize volunteers. I doubt i have the talent but i do have some suitable qualities.

* learn and practice accounting. Boring but i think i have a good foundation.
~> If accountant is hard, then book-keeping.

* private tutor for secondary school students
* teach (IT or something other than IT) in a training school
* ditto in a gov school
* get a master’s or PHD and do research/teaching.. Physics, Electronics, Math?
* lab technician in University

[09]global free func^free func]a namespace^static method #ADL

In java there are static methods and nothing else. In c there are “functions” and nothing else. In c++ there are 3 alternatives.

1) static methods — like java
** unlike the free functions, static methods can be private or protected
** can access private static fields of host class
** unlike the free functions, static methods are inherited. See Operator-new in [[eff c++]]
** usually don’t need name space prefix

2) free func/operators in a “package” like a boost library or STL, typically organized into namespaces. Better than global. See ADL technique

3) GLOBAL free func /operators — vestige of C syntax

In quick and dirty applications (not libraries), you see lots of global free functions.

y spend precious spare time learning swing

* Unlike c++, python, socket …, this is real project, real value to me, real requirement from myself. I tend to finish much faster and with tangible outcome
* Even though I did a few swing projects in AutoReo, this is relatively new territory, so ascent is fastest.
* Together, swing and wpf are in a class of their own. Specialized skill and experience.
* I live nearby and with no family. This is one of the best ways to utilize the advantage.

* SQL and Unix are more “civil-engineering” but very hard to make visible, lasting progress.
* Unlike WPF, i can leverage on my strong java foundation including threading.
* Also, my previous professional experience needs “something” to reach critical mass and hit the next level. That something is probably hands-dirty experiments. I recall my java design experiments in ErrorMemos.

T.operator==() needed in container of T

As stated elsewhere in this blog, any “element type” to go into a STL container should define operator==(). (Actulaly, operator=() too, but different story.) Unlike java, there’s no default equality test.

If no operator==() then std::find() can’t work in this case.

Now, all builtin types are fine, but most user-defined types aren’t.

Therefore, std::find() fails by default. This is yet another everyday issue in STL usage.

This is less critical — all containers define a container-level operator==(), which requires an element-level operator==().

CyclicBarrier and multi-processor parallel decomposition

One of the simplest yet most useful ways to utilize multi-processor hardware for *iterative* jobs is the cyclic barrier.

* Not for recursive problems. If your requirement is recursive in nature, there are other techniques of parallel decomposition.

** Sometimes (as in low-latency systems), we convert recursion into iteration for memory efficiency, without losing parallelism, on a multi-processor hardware. Note memory is critical in low-latency, for a number of reasons.

* segmentation

Q: how is a barrier different from a count down latch?

1+2 bond buy/sell trade flows (offer/bid..)

For a muni bond dealer, there’s just 1 way to sell and 2 ways to buy.

Dealers sell via _advertising_ i.e. publishing offers. Price is fixed. Quantity limit is fixed too. Clients can trade any quantity within the limit.

Similarly, dealers can buy via advertising i.e. publishing fully-disclosed bids. Price is fixed. Quantity limit is fixed too. Clients can trade any quantity within the limit.

Buyers more often initiate a buy via RFQ (bid-wanted). Buyer sends an RFQ (Bid Request) for a given quantity. A dealer (among other dealers) responds with Bid-Answer along with a final price. If he likes a particular bid answer (or “bid”), buyer sends an order.

In all 1+2 cases, price is decided by the dealer.

stateful^stateless Thread objects

Q: Say you have a Thread (either from a Runnable or Thread subclass). Is it good practice to remove all fields and make it stateless?

Obviously thread scheduler keeps state of the jvm thread, but application need not know those, and such state info is not exposed to the Thread object. As highlighted in other post, a Thread object is a poor handle on a real jvm thread…

Volatile boolean fields are popular, but must they be a field in the Runnable or Thread subclass?

I think ThreadLocal is implemented as an instance field of a Thread object. See http://stackoverflow.com/questions/1202444/how-is-javas-threadlocal-implemented-under-the-hood. As such it’s a stateful object inside a Thread object.

Let’s bear in mind any non-final field [1] in a Thread or Runnable can be modified by any (jvm) thread — Complex semantics. To simplify modeling[2] and semantics and improve maintainability, I’d avoid custom stateful Thread objects.

[1] or final reference field except String
[2] state ie instance fields are fundamental to OO modeling

Locking is all about state consistency, ie state of domain objects . There’s already enough state to complicate concurrent systems, even if our Thread objects are stateless.

threads and inner classes

Many programmers aren’t comfortable with nested classes or threading. Good to familiarize with common and uncommon usages. I feel concurrent OO modeling is more complex. May call for unusual modeling constructs such as nested classes.

* Anon classes are extremely widespread in concurrent programs.

* [[java thread programming]] details an active class that hides run() method, which is always public, using inner class. However, state can become complicated. Is it better to make these nested class stateless? I think so.

init method vs constructors

I usually start with and stick to constructors, and avoid creating init() until i see justifications such as
* multiple constructors need to share some initialization code. You can have init1(…), init2(..) as lego’s to be used by these constructors.

* init(..) can have weird arguments, unsuitable for constructors.
* constructors constraints — must call super(…) as*first* statement
* Spring init-method won’t work with constructors??

* a special justification in a multi-threaded context
All my constructors try to quickly button up the object and return. Before a constructor returns, the object is invalid. If a constructor creates a thread that thread might access this invalid object. You can put crazy things (DB, network access…) into init(), and you can choose to call init() after (or before?) a constructor returns.

dict – at the heart of python, perl and javascript OO

Q: why is dict so central to python OO?
%%A: method object — python methods are easily modeled as code objects (just like fields), therefore more dict-friendly. Recall the regular dict stores key and value objects.
%%A: ultra-virtual — both field and method lookup is a runtime dict search. Note java/c++ does runtime search only for methods, not fields.
%%A: namespace — is a kind of dict. Namespaces are central to inheritance (and presumably overriding)

dir(), vars(),__dict__  … often returns something like a dictionary.

Note the dict in this context is a kind of “kernel-dict”. On the other hand, the user-defined dict is probably a “userland dict”. They share the same syntax. Implementation wise, they may differ. kernel-dict is probably memory efficient and in implemented in C. These various types of dict probably implement a consistent “interface”.

When people say an “import sys” creates an object named “sys”, it probably means a dict created in memory.

———–
In Perl OO, a hash is at the heart of every class. This is the “userland dict”.

———–
In javascript, “associative array” is closely related to (if not the basis of) user-defined objects

python=an object-based lang !! OO lang

By java/c++/c# definitions, Python is not strictly object-oriented (“—–“), but in a special sense, python is more object-based (“++++”) than the big 3.

– a class is a dictionary/hash, just like perl
– no declared type for each variable
– no type checking, less type safety
– no strict ctor
– no private/protected ie no access control
– java and c# disallows python’s freestanding, standalone functions.
– no static members
– unclear separation of instance field and class field.
– subclass ctor doesn’t always call base class ctor to initialize the inherited fields
– a regular object instance can have any undeclared field/method injected into it in broad daylight, bypassing any type check.

I think python is object-based more than object-oriented. Further,
I guess python is more dict-based than object-based. I guess many OO features are implemented using dict. I guess a lot of meta-programming uses the dict.

+ a namespace is accessed as an object (and also a dict)
+ a module is an object (and also a dict)
+ a function is an object (and also a dict with attributes)
+ a method is an object (and also a dict with attributes)
+ a built-in function is an object
+ a type is an object, and perhaps is used more than java reflection is
+ an int, float etc is an object (and probably a dict)

callable tasks, simple thread pool set-up, shutdown, awaitTermination

What you added to task queue should not be a simple POJO or java bean. It needs a run() or call() method.

public class DualThreadPool {
//* Construct a thread pool (size 2) with an internal unbounded queue
private final ExecutorService eService = Executors.newFixedThreadPool(2);
private void addTask(Callable task) {
assert task != null;
this.eService.submit(task);
}
public static void main(String adfs32[]) throws FileNotFoundException, InterruptedException {
DualThreadPool pool = new DualThreadPool();
pool.addTask(new FileParserTask(Downloader.download("<a href="http://www.google.com/" target="_blank">http://www.google.com/</a>", ""), true));
pool.addTask(new FileParserTask(new FileReader("c:/windows/ie7.log")));
pool.eService.shutdown();
pool.eService.awaitTermination(22, TimeUnit.SECONDS);
MultiCounter.instance.dumpCounts();
}
}

calling assignment from copier (UBS IV

#include
// Using “this” in contructor? See http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.7
using namespace std;
struct CAT {
    string name;
    int age;
    CAT& operator=(CAT const & rhs) {
        cout << "entering operator= " << this << " <– " << &rhs << "\n";
        this->name = rhs.name;
        this->age = rhs.age;
        return *this;
    }
    CAT(CAT const & rhs) {
        cout <name << " is current name\n";
        cout <age << " is current age\n";
        *this = rhs; // using operator=
    }
    CAT() {}
};
int main() {
    CAT e;
    e.name = “Thomas “;
    e.age = 33;
    CAT copy(e);
    cout << copy.name << copy.age << endl;
    cin.get();
    return 0;
}

c++ exception handlers – global var

These “types” are aliases of the same type. Typedef over “ptr to void-void function”.

– terminate_handler / set_terminate() [1]
– unexpected_handler / set_unexpected() [1]
– new_handler / set_new_handler() [2]

Each setter modifies a global (implicitly static) variable. Global variable is quite common in C. The canonical global var is the errno global variable (which /bothers/ pthreads). I believe all of these global variables are shared by all threads.

For set_unexpected, Java offers similar things. (http://www.javapractices.com/topic/TopicAction.do?Id=229 highlights Swing EDT.)
* setUncaughtExceptionHandler()
* setDefaultUncaughtExceptionHandler()

[1] FAQ 271
[2] [[ effC++ ]]

in/out params in std lib – cf select() syscall

Now I feel pure-output and in/out parameters are rather common in C standard library. Here’s just one of many examples.

http://beej.us/guide/bgnet/output/html/multipage/recvman.html shows the last parameter “FROMLEN” is an in/out. Function recvfrom() will fill up FROMLEN with the size of struct sockaddr. (You must also pre-initialize fromlen to be the size of FROM or struct sockaddr.)

The 2nd last parameter is a pure-output parameter.

y we need threads, my take

1) GUI — need lots of threads
one thread to process user input. might block
one thread to return data to screen
one thread to send email
one thread to save data. might block
one thread to call a web service. might block
Paradoxically, all major GUI toolkits are single-threaded in terms of screen resources.

2a) network servers such as servlets, market data subscribers — multiple concurrent connections. A single thread can hardly handle them concurrently (even though NIO can help)
** nonblocking IO?

2b) database server — multiple concurrent users. but often multi-process is a viable alternative to multi-thread. What if one server machine needs to support 500 concurrent users?
) async. MOM is one example. onMessage() often runs on its own thread.
) alarms and timers?
) complete a long task faster, in parallel, like in number crunching or large database operations
) multi-processor machines -> higher throughput
** Suppose we take periodic snapshots of hardware threads and count how many are busy and how many are idle. Most of the time these threads aren’t simultaneously busy.

Note many of these needs can be satisfied with multiple processes. Therefore there’s a constant question to be addressed — “why choose threads”. Answer is obvious so I won’t elaborate — data sharing, light-weight…

y ibanks insist minimum X years finance job xp

… because they want someone 1) used to the pace and work culture, 2) knowledgeable in the domain. As an (unintended) consequence of this hiring criteria, South Asians (India…) candidates outnumber East Asians (Chinese, Korean…).

I notice adjustment problems in some (perhaps 1 out of 3) newcomers into financial IT. Some of them simply quit financial and joined telecom, google, Time.com, universities, hospitals…

It took me a few years — and counting — to get used to the culture.  Quick and dirty — occasionally even code cloning; eliminate purely theoretical scenarios and focus on realistic scenarios; tolerate minor bugs and ship faster; no documentation…. ultimately, focus on business value, that we are paid to deliver.

Financial Knowledge helps too. Last month I sent you a list of some 30 financial terms — take months to grasp.  But even if you know none of the jargon, ibanks still consider you if they think this candidate is battle tested on wall street.

As a concrete illustration of domain knowledge, remember the question you and I discussed repeatedly 2 years ago — how a brokerage house matches a buy order and a sell order and executes it profitably? I now understand it. Besides the jargon, this is example of another type of domain knowledge. Does it help a finance app developer? Valuable but not a must-have knowledge. However for an architect, a Business Analyst, or any manager (project manager, team lead, lead developer), such knowledge could be make-or-break.

recursive lock; test a thread already holds this lock

(Importance and value — I feel most non-library developer don’t need to this knowledge.)

java lets you test whether an arbitrary thread already holds a given lock. myThread.holdsLock(), but let’s explore it for c++.

In both java and boost thread, there’s an object (a “poor handle”) linked to the real world thread. There’s a one-to-one mapping between the handle and the real thread. I believe the thread library (not “the OS”) gives you a pointer to the real thread. Java or pthread builds an object around that pointer. This pointer is the key to determining if a thread already hold a given lock. P177[[pthreads]] shows pthreads api does provide a TID data structure. Typically an integer. Win32 offers 2 data structures —
– a kernel-level global thread id
– thread handle local to an OS process, so this handle is meaningless to another process. See post on user-kernel thread mapping.

Boost offers thread::native_handle() and thread::get_id()

http://tutorials.jenkov.com/java-concurrency/locks.html
shows simple java code of a re-entrant lock and non-re-entrant lock.

http://www.hdfgroup.org/HDF5/doc/TechNotes/ThreadSafeLibrary.html
Our implementation of recursive locks is built on top of a pthread mutex lock (which is –not–recursive–). It makes use of a pthread condition variable to have unsuccessful threads wait on the mutex. Waiting threads are awaken by a signal from the final unlock.

http://askldjd.wordpress.com/2009/10/26/prefer-simple-mutex-over-recursive-mutex/&#8211;
mutexes are recursive by default in Windows.

http://www.pdc.kth.se/training/Talks/C++/boost/libs/thread/doc/mutex_concept.html&#8211;
Internally a lock count[3] is maintained and the owning thread must unlock the mutex model the same number of times that it’s locked it before the mutex object’s state returns to unlocked. Since mutex objects in Boost.Threads expose locking functionality only through lock concepts, a thread will always unlock a mutex object the same number of times that it locked it.

[3]Conceptually, I feel a counting semaphore can be used.

http://groups.google.com/group/comp.programming.threads/msg/d835f2f6ef8aed99?hl=en&#8211;
is a more detailed discussion of recursive mutex

deque^vector

See also std::deque implementations #briefly

This is somewhat common IV question, and a small chance to showcase your knowledge.

[[Essential C++]] points out that deque offers efficient insert/delete at the first node. For a vector this would be mighty inefficient since all existing nodes must shift. This is the real advantage of deque over vector — O(1) insert/delete at the position 0. There are other deque advantaged listed below. If you don’t worry about these, then vector is (simpler therefore) faster.

Q: is a deque expandable?
AA: expandable at both ends

Q: Why is deque good at this?
AA: Wiki says STL uses multiple arrays — Storing contents in multiple smaller arrays, allocating additional arrays at the beginning or end as needed. Indexing is implemented by keeping a dynamic array containing pointers to each of the smaller arrays. I call this arrayOfEquallySizeArrays. I guess random access can be quite fast.

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?id=1073086407&answer=1069897449 suggests deque should be used if the list of items has any chance of being very large, whether you need insertions and erasure at the beginning or not.

— one big chunk vs multiple small chunks of memory

It also says a vector requires N consecutive blocks of memory to hold its items where N is the number of items and a block is the size of a single item [1]. This can be a problem if the vector needs 5 or 10 megabytes of memory, but the available memory is fragmented to the point where there are not 5 or 10 megabytes of consecutive memory. A deque does not have this problem, if there isn’t enough consecutive memory, the deque will use a series of smaller blocks.

[1] Each item is copied into the vector, unlike java vector’s pointer-copying.

— Another advantage is reallocation, as explained in std::deque implementations #briefly

— A minor drawback of deque is the always-invalidate-all-iterators behaviour when you insert (not erase) anywhere.

Not sure about this at the moment.

c++ virtual ctor #clone()

P263 ARM has a simple eg. Scott Meyers covered it too. Here are a few tips to help you remember some of the details.

virtual B * clone(); // declare

Q: take any argument?
AA: no-arg. The only input to the cloning is “this”  — The host object’s this-pointer

Q2: Return by value or reference? Has to be reference IMO. How about rvr?

Q3: Return type?
AA: covariant return type. since each derived class’s implementation instantiates an object of a distinct type. I wrote a separate blog post about this.

Q4: pointer to heap or stack?
%%A: stack means imminent destruction, so must be heap.

Q5: who will delete it?
%%A: you can use a smart ptr to automate the deletion. I feel sometimes it’s not possible, so the calling function should be responsible to delete it.

variable type declaration: when using container of pointers

Suppose you have a container of (raw) pointers — very common requirement. Almost anything you do would require an iterator (which works like a double-pointer). The iterator declaration would include the asterisk, like

   map<Account, Book*, less >::iterator

If an algorithm needs a functor (quite common) and if the functor operator() takes an argument, what’s its actual type to replace T in ? Book or Book*

Answer — Book*. In fact, you can safely assume T * is the correct type everywhere. See P90 [[effSTL]]

design patterns often increase code size

Using nested if and instanceof.., you can implement complex logic in much fewer lines, and the logic is centralized and clearly visible.

If you refactor using OO design techniques, you often create many Types and scatter the logic.

Case in point — Error Memos workflow kernel. We could use factory, polymorphism, interfaces…

Case in point — chain of responsibility

chain-of-responsibility keywords

#1 lookup — compare: exception handler lookup and method lookup

Compared to jvm’s method lookup, COR lookup can be modelled against business logic, IMO. polymophism is lookup across classes; COR is across objects.

# aggregation — most usages of COR occur on aggregation of objects, where A has a B1, B2 which has a C1…. A request comes to a child object, which can pass it on to the parent, similar to exception/method lookup.

#9 “responsibility” implies multiple objects elect among themselve one to service a client request.

OO modelling to mirror real-world relationship@@

One of the OOM principles is “mirror real world relationships” — Be careful.

It’s fine to create a class for each real world entity. However, this principle is less important than other OOM principles such as

* testability
* flexibility, adaptability
* de-coupled

Real-world relationships show lots of IS-A but these are troublesome in OOM. Often you need to model them as HAS-A.

I think mature and sophisticated OOM often uses far more class than there are real world entities. In such cases, i think those classes are often required to support flexibility.

what is an ACCOUNT

We hear the term “account” in such diverse contexts, I sometimes lose sight of the 2 essential things about an account.

An account exists for the purpose to hold assets[1] under an owner[2].
Analogy: a drawer with a label showing the owner.

[2] In a complex multilateral transaction, the participants are represented and identified by accounts. Accounts are avatars.
[1] stuff held in an account could be securities, assets. On websites, an account can hold history, personalization data, requests or anything — an account is an avatar of a user.

GS hosts many types of client accounts, but GS has just one account with NYSE.

Trading account — each P/L entity needs a trading account.

const nonref variable: reasonably immutable

Reading the c++ FAQ 18.10, I got the impression that most const variables are ptr or ref.

const nonref primitive is like a java constant or java immutable.

Q: are there const nonref class object? Are they immutable?
A: Yes according to http://www.learncpp.com/cpp-tutorial/810-const-class-objects-and-member-functions/

Once a const class object has been initialized via constructor, any 
attempt to modify the member variables of the object is disallowed, as it 
would violate the constness of the object.

Just like the built-in data types (int, double, char, etc…), class objects 
can be made const by using the const keyword.

Q: can you cast away its constness?
A: very tricky.  See effC++ and the IBM arcitlce.  Short answer is no.

what can u say when seeing a function parameter type

If you see a function param like f(Animal myArg, …), what can you say about myArg’s type “Animal”?

* it can be a dummy-type declared for templates like the T in
* it can be a real class
* it can be a typedef
** underlying type could be a ptr
** underlying type could be a func ptr
** underlying type could be an array

We need a better understanding of typedef….

c++ : first thing in parentheses of a func prototype

A parenthesis indicates a function call/prototype. The latter is a real readability issue — If there’s an unreadable param, then function call won’t be simple — doomed.

“First part” inside the parenthesis of a prototype is a parameter_type_specifier, just like variable declarations. However, a variable declaration is (and reads) simpler — it has 0 or 1 “=”, and everything on the left consists of the variable type and the variable name. There’s no obligatory “distractions”.

For a function prototype, there are other params and there’s a context ie the function as a whole. The function makes sense only as a whole [1]. It’s generally ineffective to try and interpret a “first part” without context. Therefore when you encounter a monstrous “first part”, you have to swallow it along with the context — more indigestion.

[1] In the same vein, a non-static field/method in isolation has limited meaning compared to the class as a whole. We are trained to think in terms of functions — deep rooted in our psyche. Function is the standard level of abstraction.

Now let’s look at some of the things that wash up as “first part”.

* a dummy type (template param) like T, S or a misleading name like InputIterator.
* indistinguishable from type params, we see type aliases defined in typedef. Like size_t.
<=== These are the 2 deceptive kind of "first part", as they often resemble user-defined class types.

* since T is allowed, so is pair
* a specialized template — since pair is allowed, so is vector
* an iterator, like std::vector::const_iterator
* functors like ….?

To make things worse, in the “first part” we also see ptr/ref to the above items, such as
– ref/ptr to a type param T
– ptr or ref to a type alias
– For template names, things get complicated — we can get vector and vector*. Example qq[ Property& ] as in [[pricing]] P51
— throw in iterator, you can get qq[ map(K,V*)::iterator* ]

In java, a method param can be as complicated as Map<T, List> but for C++ as soon as ref/ptr meets templates, complexity quadruples.

Some people put in a function ptr in the “first part”. I feel this should be a typedef.

c++ array variable doesn’t carry size information

Java arrays are objects and, unlike c, has extra storage to hold its size. C arrays have exactly { sizeof(element) x initialized elementCount} bytes, so about the only way to pass on the array size information is to pass another variable. Examples follow.

Q: in c++ standard, why must we pass in an arraySize param to main(int arraySize, char ** args)?

Answer is below, but let me clarify that array params are widely used in C and (consequently) c++ financial apps.

Q1: what’s the difference between f(sometype*) and f(sometype[] )?
A: no diff. Either way f() receives a pointer.
A: [[Absolute c++]] section on array parameter explains nicely that an array variable has 3 parts
– starting address
– type
– size

….but the size is not passed to f(). f() could increment the pointer forever without knowing it trespasses on the real estate of another object or de-allocated wasteland in the free list.

Rule on array parameter — Always add a size param to an array param.

To pass a std::vector into a f(MyType* data, int size), do it this way

    f(&vec[0], vec.size(); //P74 [[effSTL]]

Q2: But why is there never array size in C string functions?
A: the exception to the Rule. c-string is null-terminated, so pointer won’t trespass.

non-method remove() can’t shrink a container

Suppose 5 items are to be removed and 10 items to remain. Scott Meyers said something to the effect that “remove() will put the 10 items in the front-segment, and return an iterator to indicate end of the front-segment, but the container size will remain 15.

Now, to shrink the container, you need to get a pointer to the container not its elements. With the pointer, either a method or a function accepting the ptr can shrink the container.

Function remove() gets no such ptr. It only gets iterators. Iterators provide access to the elements, not the container.

An STL container has pointers to the data structure (almost always on heap), but the data structure doesn’t have pointer to the host container, which can be on stack or global memory.

AOP ] %% own lang

“crosscutting”, “concern” … are too abstract for many (SG, Indian,
Chinese) programmers I work with.

A “property” (related to “concern” and “aspect”) is a characteristic,
quality, personality of a class. A class can have 0 or more
properties.

AOP can implement interceptor, which is widely used for security, transaction etc.

Simple code eg 1: without AOP, adding a word “thread-safe” requires
additional code in many classes
[[ java reflection in action ]] P 175

Simple code eg 2: “serializable”

Simple code eg 3 of a property: “persistent”

code eg 4: perl AOP solution http://www.perl.com/pub/a/2007/04/12/lightning-four.html

,,proxy, intercession
[[ java reflection in action ]] P 74
solution for “property-implementation”

,,code gen
[[ java reflection in action ]] P 175

proxies in Spring (and java)

Here’s a random list of places we see proxies popping up. I think 80% of these use reflection

* proxy are often used for network-enabled runtimes
** corba
** rmi
** web service
** ejb

* spring remoting
* runtime generated proxies — cglib and jdk
* aop
* TransactionProxyFactoryBean
* method injection