I don't agree with "single exit point" and so did I practically never apply it.

Glad to find an entry on this website:
http://stackoverflow.com/questions/1701686/why-should-methods-have-a-single-entry-and-exit-points

It said that:
This advice is outdated and bad for code pratice because it will lead to temporary variable which is hard to maintain.

To me, temporary variables smell bad a lot rather that multiple exit points.


public void uglyAndWrong(final int hamsandwich) {
int answer;
if (hamsandwich % 2 == 0) {
answer
= 27;
} else {
answer
= 13;
}
return answer;
}

vs:

public void comelyAndHip(final int hamsandwich) {
if (hamsandwich % 2 == 0) {
return 27;
}
return 13;
}



How about multiple entry point ?

Look at this code:

public void doSomething(MyObject person, boolean beNice) {
if (beNice) {
//code to do something nice
} else {
//code to do something not nice
}
}

vs:

public void doSomethingNice(MyObject person) {
//code to do something nice
}


public void doSomethingNotNice(MyObject person) {
//code to do something not nice
}


Which one do you prefer ?
I prefer the first one coz it would reduce code duplications because most of the time, code to "do something nice" will just be different in a little way with "do something not nice".


Links to this post
Quoted from one of Jakarta Globe's article:
In polite Indonesian language, these are recommendations. In more direct language, these recommendations should be implemented.
This sentences remind me about Malcolm Gladwell's newest book "Outliers" about how airplane accidents happened because of miscommunication between the pilot and the copilot when they came from 2 different countries. And it make sense to me because I see that kind of thing happened at our office a lot, which has expatriates as managers.

So, according to Indonesian polite language, recommendations or suggestions mean that it should be implemented. But according to west people, recommendations mean recommendations.

Because I was born here, shame on me I do that a lot. So we sort of understand what people "mean" when they "say" something different. And whether they actually want something or not, when they say they don't want it.

You see??? It's so complicated, maybe east people [we] should fix their [our] way of communicating things by using words only without too much feeling inside.


Links to this post
To try this input box, copy paste the code below and save as test.html and then open it with a browser, or use the "Try Me" section on the right side bar of this blog.

<html>
<script type="text/javascript">

function format(fieldName) {
decSeparator = '.';
thousandSeparator = ',';
field = document.getElementById(fieldName);
caret = getCaretPos(field);
newValue = '';
oldValue = '';
sign = '';
decimal = decSeparator + '00';
for (i = 0; i < field.value.length; i++) {
if (field.value.charAt(i) == '-') {
sign = '-';
} else if (field.value.charAt(i) == decSeparator) {
decimal = decSeparator;
for (j = i+1; j < i+3; j++) {
if (field.value.charAt(j).match('[0-9]') != null) {
decimal += field.value.substr(j, 1);
}
}
break;
} else if (field.value.charAt(i).match('[0-9]') != null) {
oldValue += field.value.charAt(i);
} else if (field.value.charAt(i) == thousandSeparator) {
caret--;
}
}

for (i = 0; i < oldValue.length; i++) {
newValue += oldValue.charAt(i);
if ((i != oldValue.length - 1) && ((oldValue.length - i) % 3 == 1)) {
newValue += thousandSeparator;
caret++;
}
}
if (newValue != '') {
field.value = sign + newValue + decimal;
}
setCaretPos(field, caret);
}

function insertAtCaret(obj, text) {
if(document.selection) {
obj.focus();
var orig = obj.value.replace(/\r\n/g, "\n");
var range = document.selection.createRange();

if(range.parentElement() != obj) {
return false;
}

range.text = text;

var actual = tmp = obj.value.replace(/\r\n/g, "\n");

for(var diff = 0; diff < orig.length; diff++) {
if(orig.charAt(diff) != actual.charAt(diff)) break;
}

for(var index = 0, start = 0;
tmp.match(text)
&& (tmp = tmp.replace(text, ""))
&& index <= diff;
index = start + text.length
) {
start = actual.indexOf(text, index);
}
} else if(obj.selectionStart) {
var start = obj.selectionStart;
var end = obj.selectionEnd;

obj.value = obj.value.substr(0, start)
+ text
+ obj.value.substr(end, obj.value.length);
}

if(start != null) {
setCaretTo(obj, start + text.length);
} else {
obj.value += text;
}
}

function setCaretPos (obj, pos)
{
if (obj.selectionStart) {
obj.focus();
obj.setSelectionRange(pos, pos);
} else if (document.selection) {
var range = obj.createTextRange();
range.move("character", pos);
range.select();
}

}

function getCaretPos(obj) {
if (typeof obj.selectionStart != 'undefined')
return obj.selectionStart;
else if (document.selection)
return Math.abs(document.selection.createRange().moveStart('character', -1000000));
}
</script>

Try Me!!!!
<br/>
<input name="tryMe" id="tryMe" type="text" value="" onkeyup="format('tryMe');" onfocus="format('tryMe')" />
</html>


Links to this post
Almost every application need the feature of paging, whether it's web application or desktop application. Whenever you want to display some data to the user, there is no way that you display the entire set of the data to the user. Imagine that you have 1 million data on the DB, it would be inefficient either time nor resources. Especially if the application is a public website, the server might crash, if everyone is viewing the page at the same time.

If it's a desktop application, it will consume a lot of memory to display 1 million data in the table (or would you say, JTable), and then the application might crash due to out of memory exception.

For this reason, everyone should implement paging to all kind of data display. PHP MyAdmin is a good example for this. If the application is a desktop application, a proper calculation has to be done to determine how many rows can be display on the screen, so only the displayed data will be retrieved. And whenever the user is doing scrolling at the table, another set of data will be retrieved based on the calculation of which rows the user is displaying now if he was scrolling up/down for how many times.

For the retrieval, below is the query syntax comparison for MySQL and PostgreSQL. First query is the retrieval of 1st - 10th row, second query is for 11th - 20th row.

1. MySQL

SELECT select_list FROM table_expression LIMIT 0, 10
SELECT select_list FROM table_expression LIMIT 10, 10


2. PostgreSQL

SELECT select_list FROM table_expression LIMIT 10 OFFSET 0
SELECT select_list FROM table_expression LIMIT 10 OFFSET 10


Unfortunately, MSSQL and Oracle don't have the "offset" feature, and thus implementing such kind of paging will need ugly subquery that will take a long time to execute, or helper "view" that will slower the performance when inserting data. So in this case, it's to be considered whether the efficiency of data displaying is very important to the application or not.



Links to this post
First of all, proxy server is a server that acts as an intermediary for requests from clients seeking resources from other server (or Internet). Usually companies' Internet connection facility use proxy to make it possible to control their employees' browsing activities, such as:
  • Define someone's bandwidth limit for a specific period
  • Define a blacklist of websites, within a specific time schedule or not
  • Storing employees' browsing history log
  • Storing employees' chatting history log
Commonly proxy servers are used for security reason, and those points above have to be implemented with a certain networking skills or by using some helper tools.

By adding these 3 JVM arguments to java, your application will use the proxy network to create HTTP requests:
  • http.useProxy=true
  • http.proxyHost=[domain or IP]
  • http.proxyPort=[port number]
  • http.nonProxyHosts=[regex of proxy exclusions]
Example:
java -Dhttp.useProxy=true -Dhttp.proxyHost=10.1.1.2 -Dhttp.proxyPort=8877 -Dhttp.nonProxyHosts=127.0.0.1|localhost -jar testsomething.jar

For HTTPS, the arguments are:
  • https.proxyHost
  • https.proxyPort
  • http.nonProxyHosts
For FTP, the arguments are:
  • ftp.proxyHost
  • ftp.proxyPort
  • ftp.nonProxyHosts
If the proxy network is a HTTP Proxy that requires authentication with username and password, then you have to use java.net.Authentication and it means that you have to touch your code.

If the proxy network is a SOCKS v5 Proxy, then again it can be done by using JVM arguments:
  • socksProxyHost=[domain or IP]
  • socksProxyPort=[port number]
  • java.net.socks.username=[the username]
  • java.net.socks.password=[the password]
Example:
java -DsocksProxyHost=10.1.1.2 -DsocksProxyPort=8877 -Djava.net.socks.username=alibaba -Djava.net.socks.password=secret

For further information:
Networking Properties reference from sun.java.com
Java Networking and Proxies from sun.java.com


Links to this post