Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: Unmathematical interface (contract) in Java? Date: Wed, 19 Oct 2016 21:49:47 -0400 Organization: Aioe.org NNTP Server Lines: 45 Message-ID: References: NNTP-Posting-Host: PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:35841 On 10/19/2016 11:19 AM, Daniel Pitts wrote: > On 10/3/16 11:01 AM, Stefan Ram wrote: >> Dr. E. F. Codd wrote about mathematics: >> >> "A special concern is that of avoiding the need >> for special treatment of special cases except when >> absolutely necessary.". >> >> In Java, to print »s« indented by »n« spaces >> (assuming we are at the start of a new line), >> one can write: >> >> java.lang.System.out.printf >> ( "%" +( n > 0 ? n : "" )+ "s%s%n", "", s ); >> >> . Yes, you can write "%2s" or "%1s", but "%0s" >> is not allowed! Now, how unmathematical is this! >> > I mean, its weird to construct a printf format string, so I'm not > surprised that its troubling to do so... > > I imagine you can do something of similar effect without special cases, > if you give up on the idea of a one-liner... > > > final char[] indent = new char[n]; > java.util.Arrays.fill(indent, ' '); > java.lang.System.out.println(java.lang.String.valueOf(indent) + s); > > I would say its even more clear what the intent of this snippet of code > is, compared to yours. It is possible its more efficient too, though I > wouldn't put much weight on that. public static String genSpaces(int nospaces) { ... } and System.out.println(genSpaces(indent) + s); would be even more clear IMHO. Arne