tag:blogger.com,1999:blog-67932964736260001802024-02-08T21:37:59.762+02:00Funny JavaThose funny java snippets from real codeasolntsevhttp://www.blogger.com/profile/16632087166612261207noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-6793296473626000180.post-7822296711796208882010-07-21T18:15:00.000+03:002010-07-21T18:18:02.677+03:00<span class="Apple-style-span" style="color: rgb(34, 34, 34); font-family: Calibri, Arial, Verdana, serif; "><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><span class="Apple-style-span" style="color:#0000FF;"><span class="Apple-style-span" style="color: rgb(34, 34, 34); font-family: Calibri, Arial, Verdana, serif; line-height: normal; white-space: normal; font-size: 16px; "><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><code class="java"><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: arial, sans-serif; line-height: normal; white-space: normal; border-collapse: collapse; ">Consider the following code snippet:</span></code></pre><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><code class="java"><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: arial, sans-serif; line-height: normal; white-space: normal; border-collapse: collapse; "><br /></span></code></pre></span></span></pre><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><code class="java"><span class="keyword" style="color: rgb(0, 0, 255); ">public</span> <span class="class"><span class="keyword" style="color: rgb(0, 0, 255); ">class</span> <span class="title" style="color: rgb(163, 21, 21); ">SomeClass</span> {</span></code></pre><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><code class="java"><span class="class"><br /></span></code></pre><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><code class="java"><span class="class"></span> <span class="annotation" style="color: rgb(0, 128, 0); ">@SuppressWarnings</span>(<span class="string" style="color: rgb(163, 21, 21); ">"unused"</span>)</code></pre><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><code class="java"> <span class="keyword" style="color: rgb(0, 0, 255); ">private</span> Validator validator;</code></pre><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><code class="java"><br /></code></pre><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><code class="java"> ... </code></pre><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><code class="java">}</code></pre><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><code class="java"><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: arial, sans-serif; line-height: normal; white-space: normal; border-collapse: collapse; "><br /></span></code></pre><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><code class="java"><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: arial, sans-serif; line-height: normal; white-space: normal; border-collapse: collapse; ">Variable </span></code>validator<span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: arial, sans-serif; line-height: normal; white-space: normal; font-size: 16px; border-collapse: collapse; "> really isn't use. It could be simply removed, by author decided to mark it as @SuppressWarnings("unused") - just in order to make compiler and Eclipse silent. </span></pre><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: arial, sans-serif; line-height: normal; white-space: normal; font-size: 16px; border-collapse: collapse; "><br /></span></pre><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: arial, sans-serif; line-height: normal; white-space: normal; font-size: 16px; border-collapse: collapse; ">Stupid!</span></pre><pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; overflow-x: auto; overflow-y: hidden; font-family: monospace; line-height: 18px; "><span class="Apple-style-span" style="font-family:arial, sans-serif;font-size:130%;color:#000000;"><span class="Apple-style-span" style="border-collapse: collapse; font-size: 16px; line-height: normal; white-space: normal;"><br /></span></span></pre></span>asolntsevhttp://www.blogger.com/profile/16632087166612261207noreply@blogger.com0tag:blogger.com,1999:blog-6793296473626000180.post-86559031644595503332010-04-22T18:04:00.000+03:002010-04-22T18:11:04.325+03:00Collection setterThere is two major ways to write setters for collection-type members.<br/>This example combines both of them.<br /><pre><code class="java">public void setPhones(List<PhoneNumber> phones) throws Exception<br />{<br /> this.phones = phones;<br /> this.phones.clear();<br /> this.phones.addAll(phones);<br />}</code></pre>asolntsevhttp://www.blogger.com/profile/16632087166612261207noreply@blogger.com0tag:blogger.com,1999:blog-6793296473626000180.post-35113656915429352862010-03-04T16:14:00.000+02:002010-03-04T16:17:28.521+02:00Compare float numbersThis is how Chuck Norris compares float numbers:<br /><pre>if (!Float.valueOf("0.0").equals(price.getActualPrice()))<br />{<br /> return true;<br />}<br /></pre>asolntsevhttp://www.blogger.com/profile/16632087166612261207noreply@blogger.com0tag:blogger.com,1999:blog-6793296473626000180.post-80505165716334830852010-02-22T13:44:00.000+02:002010-02-22T13:50:44.884+02:00As to me, this is a really unobvious way to parse strings: <pre><br />protected void parseSummaryLines()<br />{<br /> ...<br /><br /> // NOTE: First letters are ommited in order to <br /> // support capitalized words as well<br /> String RESULT_GOOD_TEXT_1 = "<font color="red">othing</font>"; // Nothing<br /> String RESULT_GOOD_TEXT_2 = "<font color="red">uccessful</font>"; // Successful<br /> String RESULT_BAD_TEXT_1 = "<font color="red"><b>assword</b></font>"; // Password<br /> String RESULT_BAD_TEXT_2 = "<font color="red">failed</font>"; // Failed<br /><br /> ...<br />}<br /></pre>asolntsevhttp://www.blogger.com/profile/16632087166612261207noreply@blogger.com2tag:blogger.com,1999:blog-6793296473626000180.post-82032329688775264642010-01-07T17:38:00.000+02:002010-01-07T17:39:07.385+02:00Next generation programming styleA very good article about programming:<br /><a href="http://codemonkeyism.com/generation-java-programming-style/">http://codemonkeyism.com/generation-java-programming-style/</a><br /><br />in particular, this one is great:<br /><a href="http://codemonkeyism.com/never-never-never-use-string-in-java-or-at-least-less-often/">http://codemonkeyism.com/never-never-never-use-string-in-java-or-at-least-less-often/</a>asolntsevhttp://www.blogger.com/profile/16632087166612261207noreply@blogger.com1tag:blogger.com,1999:blog-6793296473626000180.post-84610266043303237042010-01-07T17:34:00.000+02:002010-01-07T17:35:54.766+02:00Bad books samplesToday I read a blog entry <a href="http://kaczanowscy.pl/tomek/2010-01/programming-books-promote-code-smells">How Programming Books Promote Code Smells</a>.<br /><br />This is true: many books contain examples of bad code.<br /><br />One of my favorite examples is the main method with try/catch like <a href="http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml">this one</a>.<br /><br /><pre><br />public static void main(String args[]) {<br /> try{<br /> FileInputStream fstream = ...<br /> }catch (Exception e){//Catch exception if any<br /> System.err.println("Error: " + e.getMessage());<br /> }<br />}<br /></pre><br /><br />This try/catch is absolutely NOT NEEDED! If you skip them, the JVM does exactly the same work. the well-known "<a href="http://www.wikijava.org/wiki/10_best_practices_with_Exceptions#Throw_early_catch_late">Throw early catch late</a>" principle says that you shoudn't catch exception until you know what to do with it (except the trivial actions that the system does by default).asolntsevhttp://www.blogger.com/profile/16632087166612261207noreply@blogger.com0tag:blogger.com,1999:blog-6793296473626000180.post-14771011970879030092009-04-01T11:47:00.000+03:002009-04-01T11:56:59.129+03:00nStartFrom - 12Quite a fun usage of variable <tt>nStartFrom</tt>:<br /><pre><br />public void setStatementFromAddress(CCallableStatement ccstmt, <br /> CAddressStruct address, int nStartFrom) throws SQLException<br />{<br /> nStartFrom = nStartFrom - 12; <br /> ccstmt.setString(nStartFrom + 0, address.getStreet());<br /> ccstmt.setLong(nStartFrom + 1, address.getCityID());<br /> ccstmt.setString(nStartFrom + 2, address.getCity());<br /> ccstmt.setLong(nStartFrom + 3, address.getCountyID());<br /> ccstmt.setString(nStartFrom + 4, address.getCounty());<br /> ccstmt.setString(nStartFrom + 5, address.getRegionID());<br /> ccstmt.setString(nStartFrom + 6, address.getRegion());<br /> ccstmt.setLong(nStartFrom + 7, address.getCountryID());<br /> ccstmt.setString(nStartFrom + 8, address.getCountry());<br /> ccstmt.setString(nStartFrom + 9, address.getZIP());<br /> ccstmt.setString(nStartFrom + 10, address.getPhone());<br /> ccstmt.setString(nStartFrom + 11, address.getFax());<br /> ccstmt.setString(nStartFrom + 12, address.getEMail());<br />}<br /></pre>asolntsevhttp://www.blogger.com/profile/16632087166612261207noreply@blogger.com0tag:blogger.com,1999:blog-6793296473626000180.post-51183356810330990202009-03-20T12:29:00.000+02:002009-03-20T12:33:55.292+02:00Ugly conditionI have found a method for validating date:<br /><pre><br />// check that it is valid format<br />// d,m,y<br />// m,y<br />// y<br />if ((nYearFrom != null && nMonthFrom != null && nDayFrom != null ||<br /> nYearFrom != null && nMonthFrom != null ||<br /> nYearFrom != null) &&<br /> (nYearTo != null && nMonthTo != null && nDayTo != null ||<br /> nYearTo != null && nMonthTo != null ||<br /> nYearTo != null))</pre><br />After a short thinking, this condition can be simplified to:<br /><pre>if (nYearFrom != null && nYearTo != null)</pre>asolntsevhttp://www.blogger.com/profile/16632087166612261207noreply@blogger.com1tag:blogger.com,1999:blog-6793296473626000180.post-72233649744011685172009-03-20T11:56:00.000+02:002009-03-20T12:54:19.510+02:00File.separatorCharI have just found a funny snippet in Java code:<br /><br /><pre>File pom = new File(dir.getAbsoluteFile()<br /> + <font color="red">String.valueOf(File.separatorChar)</font> + "pom.xml");<br /></pre><br /><br />Actualy class <tt>java.io.File</tt> has two constants:<br /><pre>public static final char separatorChar = '\';<br />public static final String separator = "" + separatorChar;</pre><br />This is the same symbol, but <i>separatorChar</i> is a <b>char</b>, and <i>separator</i> is a <b>String</b>.<br /><br />So, instead if using <tt>String.valueOf(File.separatorChar)</tt> developer could use <tt>File.separator</tt>.asolntsevhttp://www.blogger.com/profile/16632087166612261207noreply@blogger.com0