Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assignment 1.1: literals

 
landon manning
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1449
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
paul wheaton
Trailboss
Pie
Posts: 21183
Firefox Browser IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".

 
landon manning
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21183
Firefox Browser IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 1449
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21183
Firefox Browser IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic