my dog learned polymorphism*
The moose likes Programming Diversions and the fly likes who can get the simplest code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "who can get the simplest code?" Watch "who can get the simplest code?" New topic
Author

who can get the simplest code?

Dan Maples
Ranch Hand

Joined: Jun 21, 2004
Posts: 153
Here is a simple program that just displays a little animation.
The challenge: who can simplify this code the most, while still having the exact same output?
(this is purposly not as simple as it can get)



-Dan
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Has the 'sleepit' to occur in the same rhythm?

[ July 15, 2004: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
Sonny Pondrom
Ranch Hand

Joined: Jun 05, 2001
Posts: 128
You know after looking at these three solutions, I like Dan Maples the best. Why?
Sonny Pondrom
Ranch Hand

Joined: Jun 05, 2001
Posts: 128
I know this diversion was meant to show how short we can make code, but don't get into the habbit of programming this way. I think we should remember that code is supposed to be reusable. While these smaller codes work, they also hide details. To often I have seen code not used because it was easier to start from scratch then to figure out what the clever programmer was trying to do. IMHO it should look something like this:

Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

You don't mean this seriously?
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
You know after looking at these three solutions, I like Dan Maples the best. Why?

Because you define "simplest" the way you do

Other people might have other definitions for the word "simplest", and most of them make sense at certain conditions, don't they?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I see a lot to call the opposite of simple - the completely bogus 1000, misleading for loops, unnecssary modulo arithmetic - in all but Dmitry's.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

no '1000' and no modulo in my code.
And what is wrong with my for-loop?

for (; is a very common expression which is identified as endless-loop for every semi-advanced programmer.

'int x' and 'int num' arent used outside the loop, so the right place to introduce them, is - very natural - the initialising block of a for-loop.

Well - of course 'String line' could be placed here too with the same argument.

Aesthetic reasons - not to make a 2-line 'for'-head, lead me to this decision.

Where is your code, Stan? Arguing from the galery is simple!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Sorry, Stefan, missed that. I'd call yours free from evil, too. The empty clause in the for loop communicates that it is not intended to terminate. I don't think I've seen that before, but it made good sense when read with care.

This loop: for(int x=1;x<1000;x++) is misleading in that it looks like it should terminate, and obfuscatory (probably on purpose) in that 1000 has nothing to do with the problem. Of course it was in the baseline with the note that one should be able to improve it. But this one: for(int x=1;x<num+1;x++) just perpetuated the problem.

I didn't post a solution cause I liked Dmitry's just fine.

"Simple" is very subjective, so feel free to continue to disagree. Your flippable summand had a very familiar feel ... I'm sure I've done something like that in some other language.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I too liked Dmitry's just fine. For fun, I made two versions. The easy-to-understand version:

And the shortest-is-best version:

Admittedly, this version doesn't duplicate the output exactly, but it's pretty close.
[ July 19, 2004: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923


Admittedly, this version doesn't duplicate the output exactly, but it's pretty close.



[ July 21, 2004: Message edited by: Stefan Wagner ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
So consider it a challenge. What trivial changes could be made to my second method above which would generate the exact same output originally specified? Where "trivial" means "simple but slightly unappealing from an aesthetic perspective." (More precisely, my aesthetic perspective.) The fix would make the code uglier, IMO, and the discrepancies between the requirements and the output seemed pretty unimportant to me (though I did dutifully make note of them nonetheless - would you have even noticed otherwise?).
[ July 21, 2004: Message edited by: Jim Yingst ]
Sonny Pondrom
Ranch Hand

Joined: Jun 05, 2001
Posts: 128
What trivial changes could be made to my second method above which would generate the exact same output originally specified?


IMHO, it is hard to get motivated to change your 2nd method. Your 1st method has a lot going for it. Most important, it gives the correct answer. Second, it is readable. Dmitry's has only one problem. It is not as readable as yours because the correct number of spaces in the definition for "line" is not obvious (and a typo could foil it).

Readability is mute if you never have to change the code. You change the code when the original situation changes. How often does that happen?
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
Dmitry's has only one problem. It is not as readable as yours because the correct number of spaces in the definition for "line" is not obvious (and a typo could foil it).

To make the correct number of spaces in the definition for "line" more obvious, I'd suggest replacing the definition of "line" for one of the following pieces of code (at your choice):



OR



Is it more readable now?

BTW, I am curious now. When you use the word "readable", whom is it referred to? What kind of "reader" (in your opinion) should be able to recognize a piece of code as "readabe"? Are all the readers the same? Do they read or think the same way? How do you know if a piece of code that is not readable for you, is not readable for everybody else also?

Just think about it for a minute
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Jim:
The original question was very short:

The challenge: who can simplify this code the most, while still having the exact same output?

Nearly impossible to oversee 'exact'.
And while I produced my solution, I had a leaner, cleaner - but not exact - solution very early too.


Fiddling with this, it is the only interesting question in the diversion.
How should I oversee it?

And of course I can't oversee: What is happening, when your 'x' reaches MAX_LONG?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The original question was very short:

And it was answered very well by Dmitry long ago. You solution was good too. Given that, there was little motivation to continue answering the question as written unless there was something new to offer.

Nearly impossible to oversee 'exact'.

Which is why I dutifully noted the fact that my second solution was not exact. I just didn't care all that much.

What is happening, when your 'x' reaches MAX_LONG?

You and I will have been dead for about 877 million years. I will probably have lost in the outcome of this experiment by that time.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Dmitry: In my experience, readability can be a very subjective thing. Often it means, did you code the method the way I would have done it? I thought your first solution was very readable and elegant. Except for the number of spaces in the String literal, I suppose. Which was the biggest thing I changed. Your subsequent alternatives are pretty clear too.
Sonny Pondrom
Ranch Hand

Joined: Jun 05, 2001
Posts: 128
When you use the word "readable", whom is it referred to? What kind of "reader" (in your opinion) should be able to recognize a piece of code as "readabe"? Are all the readers the same? Do they read or think the same way? How do you know if a piece of code that is not readable for you, is not readable for everybody else also?

By readable, I am referring to the programmer who has to modify the code. All programmers are not the same. They have different backgrounds and think in different ways. (For example, Dmitry came up with a clever way to easily identify the number of spaces in "line". BTW, why is there 64 spaces?) Anyway, you know a piece of code is readable, if the next programmer can modify it easily. It does not have be read by everybody, it has to be read by anybody who may be asked to change it. For an example, lets say I want to reuse this code for a hand-held device that can only accept 40 character per line. How long would it take to fix it? How many code lines need changing? Will the JTest still work?
[ July 23, 2004: Message edited by: Sonny Pondrom ]
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
JY: In my experience, readability can be a very subjective thing. Often it means, did you code the method the way I would have done it?

Absolutely. It can be a very cultural thing also.
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
By readable, I am referring to the programmer who has to modify the code.
All programmers are not the same. They have different backgrounds and think
in different ways.


How do you know then, if the programmer who has to modify the code accepts
your definitions of "readability" or mine?

And BTW how do you know if simplicity (see the thread's topic) means readability
and reusability for eveyone?

BTW, why is there 64 spaces?

There is not.

I have not changed the number of spaces (equals to 65) since the original
piece of code was posted very first time. If you want to know why it was
done that way originally, then I am a wrong guy to ask

Anyway, you know a piece of code is readable, if the next programmer can
modify it easily. It does not have be read by everybody, it has to be read
by anybody who may be asked to change it.


Well, how (in your opinion) I supposed to know all that before I even started
coding? Don't you think that it takes a bit of clairvoyance to figure who may
be asked to change my code later, and what (s)he can or can not do easily? People
can be very creative in inventing barriers which block them from understanding
each others. And life is full of surprises, y'know

For an example, lets say I want to reuse this code for a hand-held device
that can only accept 40 character per line.


In your opinion, is it practical to make provisions for reusability of any
piece of code you write? Do you realize that code reusability has it's price?
and that it not always is a value?

I would provide for reusability if I planned to reuse the code in a set of
specific ways. IMHO it's not practical to guess which way a pice of code
could be reused in the future, and fine tune your implementation for that
particular way. Unless you have an operable chrystal ball, of course

How long would it take to fix it? How many code lines need changing? Will
the JTest still work?


If you need to change more lines of code than the size of original piece,
then it's more practical to throw it away and rewrite it from scratch.
And this is one of the beauties of compact code
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Better than "readability" or "simplicity" I like the XP crowd's value of "expressiveness". Does the code say what it's doing? I really liked printStarAtPosition() above better than any of the print substring things because it says what it's doing. So for the long blanks I'd prefer

rightJustify("*", MAXPOSITION);

Cute tricks like a for loop that looks like it has a terminating condition and says it's incrementing an index but really fudges the index so it never terminates do not say what they're doing. Misusing control structures for terseness ain't simplicity.

Even the for loop with an empty terminating clause could be questioned. It took me a hard second look to spot it. Why not while(true) to make it even more explicit this thing runs forever? My favorite language REXX has a "do forever" that's even better than while(true).

We could argue those specific examples (ie, I could be wrong) but I stick with the concept: make the code say what you're doing. If you're ever tempted to put a comment on a tricky line a little bell should go off!
Sonny Pondrom
Ranch Hand

Joined: Jun 05, 2001
Posts: 128
Stan,
I like what you say. I think we all learned something from this thread. Thank you Dan for starting it.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

While Sonnys arguments are good - much better than the example, - the diversion was for shortness - so the whole discussion is off topic here - it's even contra-topic.

And I gave an - in my opinion good - reson for prefering a 'for'-loop: variable-declaration.

Of course you may write:

- use a scope without control-structure.
But of course this would confuse beginners much more than an 'for (; '.
I guess beginners don't like those special uses of control-statements, but we don't write for beginners.
If a programmer doesn't understand these uses, it's high time to get used to them!

'do forever' is misleading too - 'do while not interrupted' would be the minimum

in 'rightJustify("*", MAXPOSITION);' I see a constant and 'rightJustify' would not express the output, which is everything, but not rightJustified.

To Jim: We could comment out the sleep-value, to speed things up.
Else my calender would have the next entry in about 878 million years.
But that one million years shouldn't stop us.

Calculating (Long.MAX_VALUE-59)/60 shows, that Long.MAX_VALUE+1 isn't a multiple of 60. Mhhm. Saves a lot of energy in the next 878 million years...
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Calculating (Long.MAX_VALUE-59)/60 shows, that Long.MAX_VALUE+1 isn't a multiple of 60. Mhhm. Saves a lot of energy in the next 878 million years...

Alternately, setting the initial value of x to Long.MAX_VALUE - 20 gives output of

which seems to indicate an annoying bug in JDK 1.4.2_05 for the % operator appiled to very large values. Too bad. The correct (or at least, expected) behavior would be to have a discontinuity in the triangular wave pattern (since as you correctly note, Long.MAX_VALUE + 1 is not a multiple of 60) after which the pattern should resume as before. Apparently that's not the case. Which, to be fair, makes this a more interesting problem than I'd originally believed. Thank you.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

This bug is still present in jdk 1.5-beta:

x % 120 = -8
... -60 = -68
(int)abs...= 68

perhaps we should use 'unsigned long long' as they do in c++?

Or redefine the String to be:

Perhaps pointer arithmetic will make it's way to 1.5?
Dan Maples
Ranch Hand

Joined: Jun 21, 2004
Posts: 153
wow I didn't think such a little fun problem would have sparked such a huge debate. Thank you all for your input. I must admit that I am not a super-java-guru so seeing different opinions about readability and reusability was very interesting. I think Sonny had it right when he said
Stan,
I like what you say. I think we all learned something from this thread.

Thank you all for playing along feel free to continue the debate and cheers to all of you who gave it a shot.
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
I like Jim Yingst's first version best. The main() method is actually shorter than Dmitry's (because it doesn't have an x to initialize), and it's far clearer. Both have exactly one three line helper function (Dmitry's version needs the sleepit() function, so that needs to be added to make it work).
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Thanks, Warren. Your check is in the mail.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: who can get the simplest code?