| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
7:21 AM -
-
0
comments
This code will always throw ConcurrentModificationException:
for (Person person: group) {
group.remove(person);
}
Because:
You cannot remove an object from a collection while iterating through it.
for (Person person: group) {
group.remove(person);
}
Because:
You cannot remove an object from a collection while iterating through it.
To solve this problem, use the clear() method to remove all objects of a collection.
6:07 AM -
-
0
comments
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:
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".
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".
7:22 AM -
-
0
comments
Quoted from one of Jakarta Globe's article:
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.
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.
7:42 AM -
-
0
comments
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>
- algorithm (1)
- code design (10)
- database (1)
- eclipse (3)
- game (2)
- HR (3)
- HTML CSS javascript (1)
- java (14)
- personal opinion (19)
- script (1)
- SDLC (7)
- shell (1)
- tools (3)
- UI usability (4)
- virus (1)
- January 2010 (2)
- November 2009 (2)
- October 2009 (2)
- September 2009 (2)
- August 2009 (3)
- July 2009 (3)
- June 2009 (5)
- May 2009 (3)
- April 2009 (3)
- February 2009 (1)
- January 2009 (3)
- December 2008 (6)
- November 2008 (1)
- October 2008 (3)
- September 2008 (1)
- August 2008 (3)
- July 2008 (1)
- June 2008 (2)
- May 2008 (1)
- April 2008 (1)
- February 2008 (5)
- January 2008 (1)
- November 2007 (2)
- September 2007 (1)
- April 2007 (1)
- March 2007 (5)
Tags
Archives
Theme by Function
© 2008 A Geek's Story Bloggerized by Falcon Hive.com