Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!postnews.google.com!news2.google.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!nx01.iad01.newshosting.com!newshosting.com!news-out.readnews.com!transit3.readnews.com!teleglobe.net!newsgate.cuhk.edu.hk!news.netfront.net!not-for-mail From: Wanja Gayk Newsgroups: comp.lang.java.programmer Subject: Re: Style Police (a rant) Date: Sun, 11 Sep 2011 15:33:05 +0200 Organization: Netfront http://www.netfront.net/ Lines: 153 Message-ID: References: NNTP-Posting-Host: 77.177.187.230 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: adenine.netfront.net 1315747983 68052 77.177.187.230 (11 Sep 2011 13:33:03 GMT) X-Complaints-To: news@netfront.net NNTP-Posting-Date: Sun, 11 Sep 2011 13:33:03 +0000 (UTC) User-Agent: MicroPlanet-Gravity/3.0.4 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7794 In article , avl@gamma.logic.tuwien.ac.at says... > > Wanja Gayk wrote: > > It's a shame that in Java not all references are implicitly final and > > only real variables get marked with "var" instead - that would serve the > > same purpose with less effort and less visual clutter. > > It seems like your general coding style differs from mine. The > percentage of re-assigned variables versus those assigned only > once is large enough, that a "var" keyword would cause more > clutter than putting "final" on each other variable. > > > I'd favor a different change: let final variables optionally > have their type inferred: > final myList = new ArrayList(); > Afterall, it is just a handle for some previously obtained value. Well, is it? I have a hard time believing that. Could you be so kind to post some code? Here's some undocumented reflection code I once wrote for a utility (I also stripped the first indentation for this posting). I thought I'd use that as I saw the class containted actually some code that looked quite typical to my eyes at the first glance. Though I know from my experience that most of my code has hardly any changing variable in it, apart from some index or so, but I was pretty surprised to see that there is actually not a single "changing" reference in it that had to be marked as "var" in ther languages. Just see for yourself and compare your coding style to mine, is it really that different? class ApplicationWorkerUtil { static interface InternalExceptionHandler { void handle(final TimeoutException e); void handle(final Exception e); } static Iterable getRunnables(final Object target, final Schedule lifecycle, final InternalExceptionHandler exceptionHandler) { final List tasks = new ArrayList(); for (final Method m : getMethods(target.getClass(), lifecycle)) { tasks.add(methodAsRunnable(target, m, exceptionHandler)); } return tasks; } private static CancellableRunnable methodAsRunnable(final Object target, final Method method, final InternalExceptionHandler exceptionHandler) { final Runnable r = new Runnable() { public void run() { try { method.invoke(target); } catch (IllegalArgumentException e) { exceptionHandler.handle(new IllegalStateException("Unexpected state running a GUI-Job", e)); } catch (IllegalAccessException e) { exceptionHandler.handle(e); } catch (InvocationTargetException e) { if (e.getCause() instanceof Exception) { exceptionHandler.handle((Exception) e.getCause()); } else { exceptionHandler.handle(e); } } } }; return new CancellableRunnable(r); } private static List getMethods(final Class targetClass, final Schedule lifecycle){ final List methods=getDeclaredMethods(targetClass, new Predicate(){ public boolean matches(final Method candidate){ assert candidate != null; final Job jobAnnot=candidate.getAnnotation(Job.class); return jobAnnot != null && lifecycle.equals(jobAnnot.value()); } }); ensureNoParameters(methods); sort(methods); return methods; } static List getDeclaredMethods(final Class c, final Predicate pred){ final List found=new ArrayList(); for(final Method m : c.getDeclaredMethods()){ if(pred.matches(m)){ AccessController.doPrivileged(new PrivilegedAction(){ public Void run(){ m.setAccessible(true); return null; } }); found.add(m); } } return found; } private static void ensureNoParameters(final List methods){ for(final Method method : methods){ if(method.getParameterTypes().length > 0){ final Job jobAnnot=method.getAnnotation(Job.class); assert jobAnnot != null; final Schedule lifecycle=jobAnnot.value(); final Class declaringClass=method.getDeclaringClass(); throw new IllegalArgumentException("The method " + declaringClass.getName() + "#" + method.getName() + " annotated with " + lifecycle + " must not have any parameters."); } } } private static void sort(final List methods){ Collections.sort(methods, new Comparator(){ public int compare(final Method o1, final Method o2){ final int i1=o1.getAnnotation(Job.class).index(); final int i2=o2.getAnnotation(Job.class).index(); return i1 > i2 ? 1 : i1 == i2 ? 0 : -1; } }); } static void waitForCompletion(final Iterable> futures, final long timeoutMs) throws InterruptedException, TimeoutException { try { final long timeoutTime = System.currentTimeMillis() + timeoutMs; for (final Future future : futures) { if (!future.isDone()) { if (timeoutMs < 0) { future.get(); } else { future.get(Math.max(0, timeoutTime - System.currentTimeMillis()), TimeUnit.MILLISECONDS); } } } } catch (ExecutionException e) { throw new RuntimeException("Unhandled Exception:", e.getCause()); } } } Kind regards, Wanja -- ..Alesi's problem was that the back of the car was jumping up and down dangerously - and I can assure you from having been teammate to Jean Alesi and knowing what kind of cars that he can pull up with, when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer] --- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---