unit tests]IDE

i accept the need for individual tests for each important method. Background: IDE is necessity in the team.

Option 0: main method for now, unit test cases later
Option 1: junit

Tests are so frequently run that i don’t like the extra step of opening the test class. Using main method, i don’t have to.

i change my methods often. if i invest effort building the unit tests, the effort can be wasted. Perhaps build those methods after the initial volatile phase.

Too many classes open.

Advertisements

sybase trigger — unique id column

specific Purpose: input validation. Let’s zoom in on this. insert, update. not delete.

My observation: a unique id column is important to any input-validation trigger in sybase.

My reasons: you usually need UPDATE yourtable set …. WHERE yourtable.id=inserted.id. Without the where-clause, you could update pre-existing rows. Perhaps millions of pre-existing rows for every row inserted.

Because sybase doesn’t support before-trigger, you need this trick to update the rows just inserted.

FX settlement system

I once interfaced with the FX settlement (clearance) system of a major US bank (let’s call it CB). There are always 2 counterparties to each trade. Each counterparty has a nostro account with an “clearance bank” [1]. The settlement process must transfer one currency from clearance bank acct A to clearance bank acct B, and transfer the other currency from B to A.

For example, JPM could be a counter party, but for one particular FX trade, the HKD clearance bank could be HSBC in Hongkong.

In our case, one of the 2 clearance banks is always CB itself. In fact one of the 2 counter parties is usually a CB-controlled trading account.

The settlement system is also know as “cash manager”.

[1] For example nostro acct to receive HKD must exist in a HK bank — the nostro bank. Only HK banks can be the clearance bank for HKD. In the special case of Euro, nostro acct can be in any Eurozone country.

input-validation triggers — sybase

A few websites say that “at the time of writing, sybase has no before-trigger”, like Oracle, mysql or DB2.

Solution? you know the new values are already in the table, but u can UPDATE those new values and commit, or make an explicit rollback. This would ensure only valid data get committed.

a class HAS-A instance of the same class

The /casual/ observer encounters many patterns where Object1.java HAS-A instance of Object1.

if the nested instance is a static instance (static member or a local var in a static block), … i feel there’s no OO principle behind the pattern. The hosting object (a class object) and the nested object are very different — class object vs instance of Object1.

From now on, we exclude static instances.

If a domain object follows this pattern, usually(?) there’s a real-world relationship to explain it. Perhaps a parent HAS-A child, a course has a pre-requisite course, a company has an institutional shareholder …

boolean column ] database@@

Q: how to you design your table columns to hold boolean flags such as isOK, isAlive, isActive, which are heavily used in where? The challenge is, no index possible, at least in sybase =} FTS. For now, assume you want to select the minority of the rows. If you want the majority, then FTS may be legitimate.

A: Basically you only care about yes/no. But I would suggest use 0 for the majority of the rows, and 1-255 or 1 ~ FFFF for the minority. Keep this column narrow — 1 or 2 bytes, then build an index.

Even though ms sql and mysql can perhaps create an index on a binary or BIT column, it’s 1) less well-supported than regular indices and 2) questionable. In an enterprise enviroment, politically safer to stick to regular indices.

http://www.sql-server-performance.com/article_print.aspx?id=347&type=art has examples.

http://sqlserver2000.databases.aspfaq.com/can-i-create-an-index-on-a-bit-column.html has examples.

best way to sanity-check java method arguments?

Say you are writing a utility Class1.getByIndex(int arrayIndex). Best way to enforce that arrayIndex is positive?

– throw an unchecked error and crash the jvm? Recommended by many experts. I guess the downside is tolerable, and there are no better candidates as a standard, universal solution.
– throw a checked exception and force other programmers to try-catch? I think the overhead grows when you set this as standard practice.
– return a boolean result? Many methods/constructors can’t.

Ideally, you want to remind other programmers to check their input first, but not force them to put up a useless try-catch.

how to instantiate a new java Date object

Q: how to instantiate a new java Date object with 3 numbers for year, month and date?

I guess Calendar could be one standard solution, but not a one-liner.

Practical solution for the time being: use the deprecated constructors

— new java.util.Date?

Only deprecated constructors do that.
java.sql.Date.valueOf(“2008-04-02” )

–new java.sql.Date?

Only deprecated constructors do that.
java.util.Date.getTime() can feed a long to a java.sql.Date() constructor

SQL-CASE expressions — keywords

* evaluate — each case expression evaluates to a …
* per-row — the expression is usually evaluated for each row
* if-then-else

All above keywords apply to both simple and searched case expressions

which parts of the case expression can be “expanded”?
– when clause
– after-case expression, right after the “CASE” keyword
– ??? then clause

Where can a case-expression appear?
* group-by
* update->set
* select-columns