File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Cattle Drive and the fly likes Assignment 1.1: literals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Assignment 1.1: literals" Watch "Assignment 1.1: literals" New topic
Author

Assignment 1.1: literals

landon manning
Ranch Hand

Joined: Nov 20, 2000
Posts: 47
Note: In my assignmnet I have the following:
<code>
public class Hundred
{
private static final int number = 100;
private static final int maxLineLength = 79;
</code>
Reviewers comment:
"You declare these here, then only use each one time. Wouldn't it be simpler in this case to just use the integral literals directly in the lines below?"
There was a mixture of reasons for makeing these 'constants' and putting them up top.
1. To avoid the "magic number" syndrome. Literals in code are bad. They give no indication as to what they represent. In large projects, this is really bad. I once spent two days tracking down a bug on some code that had a singe literal in an inconspicuous place causing the problem.
2. I made them as 'constants' because they represented numbers that wouldn't change through-out runtime. When I first started coding the class, I put the hundred down first as it represented the classes number. I didn't know if I would have to use it multiple times or not. Also, after codeing was complete, I left it there (even though I had only used it once) because in the future, when someone might modify the code (an assumption that all should make about all code they write) they would have an easier time figuring out what was going on (readability).
3. Also, putting them up there makes it easy to change them withoug diving into the code (think about my way of doing this as the code bases grows larger). One of my mistakes in the program was that my maxLineLength was 80 as opposed to 79. It took me all of two seconds to modify the code (again, think about having to do that in large code bases).
Granted, this was a very small piece of code, but I perfer to practice good programming techiniques whenever I code. That way, when I do code larger projects, the good programming techiniques are second nature and the code turns out better.
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
I did the same thing. You also provide some good reasons to do this in general, which I think everyone would agree with. But it isn't the simplest thing that could have been done for this specific assignment so I think that is why it received a comment.
John


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
You've brought this subject up before (I'd forgotten it was you). This is not a major thing, and certainly if you were writing a large project (actually in most instances), I would whole-heartedly agree with you.

John is correct in the reasoning behind the comment. We're looking for the simplest and most readable way to satisfy the requirements of this short assignment. I think that 80 is much more readable than 'maxLineLength'. But, like I said, it's not a big deal.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20574
    ∞

You make some good points, but help me to understand how the identifier "number" adds readability.
There are many times when such constants do add a great deal of readability, even if they are used only once. For example, in assignment 1.4, rather that using 1000000000, I set a constant at the top of the class called aBillion.
I think you can make a debatable case for "maxLineLength" (and we'll let that go), but not for "number".


permaculture Wood Burning Stoves 2.0 - 4-DVD set
landon manning
Ranch Hand

Joined: Nov 20, 2000
Posts: 47
Ok, so maybe 'number' was a bit overly pendantic. Granted, assignment 1.1 was a very procedural assignment, but I tried to put in as much OO design as possible (mainly to practice OO design).
Reasoning:
1a. Class Hundred represented a number, 100. Like I said above, when I first started coding this class, I wasn't sure how many times that I would use the number 100.
1b. I was preparing for any changes that might be made in the requirements (aka, say Hundred got changed to HundredAndOne). Granted, the chances of this assignment are rather slim, however, most projects do change, so I was following a general pattern.
Now this really doesn't do much for readability, however it does help with flexibility. And perhaps the case that I was trying to make with readability stemmed from the fact that using number as opposed to a literal, if there were any changes in the requirements I wouldn't have to read through the code to find out where all I used the literal.
2. Avoiding "majic number" syndrom (see above). Perhaps number should have been classNumber or something to that effect.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20574
    ∞

1b) My experience has been that my crystal ball is defective. Stuff rarely grows in the direction I wrote all this extra code for future growth. In the mean time, all this extra code just makes the whole thing less readable.
The new XP stuff makes some strong cases for not using a crystal ball.
2) There are times when a hard coded number makes the code more readable. This is one of those times.
landon manning
Ranch Hand

Joined: Nov 20, 2000
Posts: 47
"The new XP stuff makes some strong cases for not using a crystal ball."
Could you expand on this, or point me to some information pertaining to this?
Thanks

[This message has been edited by landon manning (edited January 22, 2001).]
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
Head out to extremeprogramming.org or xprogramming.com for overviews of XP and why XP advocates doing the simplest thing that could possibly work.
John
[This message has been edited by John Wetherbie (edited January 22, 2001).]
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20574
    ∞

In the first XP book (I don't have it in front of me) I remember there was a guy that was supposed to write some code to do a pop-up. Nowhere else in the program was there need for a pop-up. The developer said "we should make a generic pop-up routine that we can re-use." And the manager said, "but we don't need any other pop-ups. Just the one." "But we probably will have more pop-ups later. So this will save time in the long run". He spent two extra days making a generic pop up. In the end, they didn't make any more pop-ups. It was a wasted of two days.
With good OO techniques, when the second pop-up was needed, the generic pop-up could have been created and it would have taken just as long as when the generic pop-up was created.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assignment 1.1: literals