Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35825 > unrolled thread

Re: Unmathematical interface (contract) in Java?

Started byDaniel Pitts <usenet@coloraura.com>
First post2016-10-19 08:19 -0700
Last post2016-10-19 21:49 -0400
Articles 2 — 2 participants

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Unmathematical interface (contract) in Java? Daniel Pitts <usenet@coloraura.com> - 2016-10-19 08:19 -0700
    Re: Unmathematical interface (contract) in Java? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-19 21:49 -0400

#35825 — Re: Unmathematical interface (contract) in Java?

FromDaniel Pitts <usenet@coloraura.com>
Date2016-10-19 08:19 -0700
SubjectRe: Unmathematical interface (contract) in Java?
Message-ID<nu82su$90g$1@dont-email.me>
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.

[toc] | [next] | [standalone]


#35841

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-10-19 21:49 -0400
Message-ID<nu97rr$fs1$2@gioia.aioe.org>
In reply to#35825
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web