• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

To "RMNWSC" or not to "RMNWSC"

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a constant 42 appearing in my procedural coding assignment code. It appears 3 times in the main method.
Now there is a Refactoring called Replace Magic Number with Symbolic Constant (RMNWSC) which will take this constant, let me give it a name, and make it into a private static final int member field of my top-level class.
Can I take it as a general principle for Cattle Drive assignments that I should perform RMNWSC? That is, should I follow the XP principle Three Strikes And You Refactor ?
-Barry
[ January 09, 2003: Message edited by: Barry Gaunt ]
The original said 4 times, the fourth being in a helper method. But the constant in the helper was not the same as the one in the main. But the question remains, should we apply the principles or not?
[ January 09, 2003: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With this thread I would like to start establishing some helpful principles to enable greenhorns, like me, get their Cattle Drive assignments through the nitpick process a little more efficiently. A sort of add-on to the worshipful Style Guide if you like.
-Barry
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm partly (only partly) puzzled as to what assignment you could be working on.
I never repeated 42 that many times. I could see, maybe 2 or 3 times in a main method, but 4 times? Sheesh!
And I'm not sure 42 has the same gravity as, say, 9.81.
I mean, if you have a program that loops through some arrays I don't think anyone's suggesting that if you have more than 3 of them you do
private static final int javaArrayInitialIndex = 0 ;
...
for ( int i = javaArrayInitialIndex ; i < ...
Oops! Did I give away the fact that when Barry says "42" he's talking about "0"?
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
With this thread I would like to start establishing some helpful principles to enable greenhorns, like me, get their Cattle Drive assignments through the nitpick process a little more efficiently. A sort of add-on to the worshipful Style Guide if you like.
-Barry


But isn't that in part what the first half dozen or so assignments are about -- learning some of the ground rules, so to speak, so you don't torture the nitpickers too badly on the later assignments?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike said:

But isn't that in part what the first half dozen or so assignments are about -- learning some of the ground rules, so to speak, so you don't torture the nitpickers too badly on the later assignments?


The shift between thinking about the program and thinking about how one programs certainly takes place. But for the likes of me "I bin doin' it fer years naa", old ingrained habits die hard, let alone I never been YAGNIed before.
As for the 4 times up there, once was in a helper method, 3 times in the main. Hang on! I just heard something CLANG. The 42 doesn't mean the same thing all over. It's a different 42 in the helper, than the three occurances in the main. Gotta go, fire up eclipse again, PDQ... Amazing!
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, fixed it... that should now read "The constant 42 appears 3 times in the main method"!
I agree, that during the early assignments one gets to think about, and learn a few simple principles.
But, to be honest, my first responses to some nitpicks (other than spacing/tabbing) has been "Are they nuts or something?". Then after a while it sinks in a little, and gets a little more solid and unambiguous. After a few iterations, frustrations, readin' an' researchin' I get my working 68 line program converging down to the 28 line solution.
Now, after the assignment, I can say I have learned about "YouArntGoingToNeedIt", "OnceAndOnlyOnce", "ThreeStrikesAndRefactor", "ReplaceMagicNumberWithSymbolicConstant".
But, if I had read/known about these principles first then I would have applied them (really? I'm not so sure) and produced the "model" solution more efficiently and quickly.
Dunno, just need to talk I suppose (people are so dammed clammed up on these topics where I work).
-Barry
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm intrigued that your "main" method is big enough to fit in three of anything. My main methods are typically only one or two lines long, and I'm having a hard time imagining how you could fit three similar constants in a short bit of code like that.
Are any of the code fragments which use this constant similar to any of the others - could you extract some common behaviour rather than just the constant?
Is it just because they are used in a static method that you want to jump to making a static final constant? If all the occurences are in the same method, I'd probably just move them to a named local variable as a first step. The trick with refactoring is to move in very tiny steps, and check at each step to see if it makes sense in the context of the whole system. Maybe your number really wants to be a method parameter or a member variable set in the constructor - I don't know yet.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Frank, the constant value I have is the same constant value every where in the method. My very first attempt at the assignment did exactly what you proposed. int aMeaningfulName = 42; I had to use that identifier in 3 places. The nitpick was


int aMeaningfulName = 42 ;
Looks like the value of this variable doesn't vary...
String aNotherOne = "XX";
...nor this one - I'm wondering if they're necessary?


I was using these "constant" variables in several places. From the nitpick I jumped to the conclusion that I must inline these constants back into the places where I actually used them. My original logic was that by having these two constants I would be able to make my program more pliable, that is if my boss came up and said: "Hey that's great: but can you do it for 64 instead of 42?", then all I had to do was change the value of those two constants in exactly in those two places. Then Michael pointed me at YAGNI, which got me to say to myself, it's "only" a Cattle Drive Assignment, so let's do what the nitpicker wants, and I inlined the constants again. The necessity of the second of those constants has gone away now, but the first remains.
My original concept was to use 3 tiny helper methods plus a main method to solve this problem.
Problem is: I can't show you the code. Tell you what: you can do Java-5 yourself and send your version to me at nitpick@bfgaunt.com. Leave it a few days though so I can get the Java-5 CD seal of approval. Will yours, now, be the same as the required solution?
By the way, as you said, I no longer need a static member constant if I only use it in the main method now.
Hey, where are all the other Cattle Drive greenhorns ( those still fumbling on the Java-* assignments ) What are you guys thinking? Bazza's bonkers or what?
And Nitpickers you are doing a great job! I'm just an old Bazza of little brain.
-Bazza
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll probably be back, later, but for now, the first thing that comes to my mind is:
42 - isn't that THE ANSWER TO ALL QUESTIONS
AND LIFE IN GENERAL?

Juliane
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right, provided you got your towel with you.
Come to think about it, the effect of a nitpick has about the same effect as an Intergalactic Bulldozer on my assignment code.
Anyway 5's away. Wot's this? The David Mason has needed 3 attempts on Java 6 (Groans) so far? And he ain't no Daisy Dumbell either!
 
juliane gross
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry wrote:


Hey, where are all the other Cattle Drive greenhorns ( those still fumbling on the Java-* assignments ) What are you guys thinking? Bazza's bonkers or what?


First time in my life it seems to me rather good luck that I started learning Java rather late, and as my first programming language, because I don't have to "unlearn" things..
It took me one incredible attempt (my record book says so, but I may be wrong, can't really believe this myself) to loop myself through Times. Deprived of knowing it better, I generally approach the assignments whith just the little tool box I collected so far in doing the previous assignments. First I write code piece-wise and often procedural as far as I can get. Then I observe repetitions. I foster a written list with nitpicker's comments ("Isn't there any room for optimization?", "what did you gain by introducing this identifier?" and so forth ).
I go through my code mumbling the nitpicker's mantra and optimize, optimize, optimize..
In the end I often am surprised about how small my spaghetti code could get. Unfortunately, this doesn't mean a guarantee not to have to rewrite the whole thing completely, because my code full of ingenious inventions could be replaced by something even much much simpler.
With awesome feelings I follow those philosophical discussions about the finer points of Java programming in the Java Ranch forums which makes me wanting it (42 ! in Java !) even badlier.
Big Kotow to the nitpickers, I am a living example of this experiment called Cattle Drive, that it is possible indeed to close in on 42 (multilooped identifier or not).
Well, I better go and have some Java now (speak sorting some names).
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps the variable wasn't recognised as a constant because you didn't declare it "final". Or perhaps your "meaningfulName" wasn't meaningful enough.

Since you are building a "square" table and are using an inner loop, it seems that you would have two of something.

As an aside about "42", once we were playing a very difficult piece in an orchestra performance. Some of the musicians got lost (i.e. not together with the rest of the group). The conductor, trying to get everyone back together, said, "42", meaning we were currently at measure number 42. Some musicians in the of the orchestra thought he said something different, "f* you". We all had a good laugh at the next rehearsal.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Big Kotow to the nitpickers, I am a living example of this experiment called Cattle Drive, that it is possible indeed to close in on 42 (multilooped identifier or not).


Couldn't agree with Juliane Gross more! The Cattle Drive is incredable. Don't know if I'll ever make it all the way through JDBC, or even to it, but I sure hope so because I'm certainly enjoying it. Hang on to the lasso Barry, even though sometimes it feels like the cattle has pulled 'ya off your horse and is dragging 'ya around in the corral.
[ January 11, 2003: Message edited by: Ken Cobbs ]
 
Ranch Hand
Posts: 56
Android Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

.. needed 3 attempts on Java 6


It seems that we have similar unlearning difficulties. In one assignment, what seemed reasonable for a Triumph (Trophy) caused a sports car to chug. [ System.out.print( rightJustify( String.valueOf( row ) ) ) ; ]. In another, I used the header block that I have used for years, which doesn't of course equate to Style Guide ss 4. And, I ask you, how can writing your name on the screen take four attempts - we used to do that as kids in WHS/Tandy/Radio Shack on ZX81's & Spectrums - albeit without CRLF considerations!
It seems to me that we should let the compiler take care of optimisation & adopt the KISS approach.
BTW I've just put in the fourth amendment - [& no they can't search my computer ! ]
Gosh, I'm a Ranch Hand.
[ January 11, 2003: Message edited by: David Mason ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David said:

And, I ask you, how can writing your name on the screen take four attempts?


Only? Man, I was into optimizing screen real estate, running the thing on UNIX not just the other what's its name thing, and buffering output to reduce I/Os, AND allowing for switching to 132 line terminals,... Must have driven Marilyn nuts. (That's our only defence it seems ).
David again:

we should let the compiler take care of optimisation


Problem is we cannot rely on it to do any optimization. Somewhere I started muttering about that topic here.
[ January 12, 2003: Message edited by: Barry Gaunt ]
 
David Mason
Ranch Hand
Posts: 56
Android Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry said ..

Problem is we cannot rely on it to do any optimization


I tried to come up with an approach to optimisation .. Maybe this'd work ?
Take the KISS approach when we deal with all the problems that are still stopping our program from working.
Then test it, and it's speed.
If it isn't fast enough, then make sure we've hit all the go faster buttons on the compiler.
If it still isn't fast enough, refactor or optimise by doing some clever stuff (and increase the comment manyfold).
BTW do you check the bytecode for any reduction in instructions ?
I think I'm saying that optimisation isn't my primary concern; I'm not having to work on a PDP-11/83 any more, I'm done with overlays in 2MB of memory, and it all runs in terrabytes of memory now anyway.
[ January 12, 2003: Message edited by: David Mason ]
 
Trailboss
Posts: 23400
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is whether magic numbers should be replaced by a meaningful constant identifier.
My answer is: it depends.
Oh sure, you want some answer etched in stone to make your life easy dontchya!
Well, for these assignments, a magic number is often something like 10. Or maybe 100. And they are used once or twice. In a lean, little method, I think that this is no great crime.
Further, I have seen code with "private static final int innerLoopStart = 0 ;" - so this sort of thing can be taken too far.
My general rule is that if a value is used more than once and the value is not just a coincidence, I'll usually make a common identifier to hold the value. But if the value is used just once, I'll usually use a magic number. Sometimes, however, I think that introducing an identifier might add some clarity, so I'll introduce it!
 
Ranch Hand
Posts: 898
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by juliane gross:
Big Kotow to the nitpickers, I am a living example of this experiment called Cattle Drive, that it is possible indeed to close in on 42 (multilooped identifier or not).
Well, I better go and have some Java now (speak sorting some names).


Who is Big Kotow?
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kotow
 
Guennadiy VANIN
Ranch Hand
Posts: 898
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn, thanks,
it was capitas a name and there is such Russian name... also very common and the sense was quite opposite...
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guennadiy, maybe it's time to try to fix your display name. It's making me dizzy.
If you have any problems let me know and I will be happy to help.
 
Guennadiy VANIN
Ranch Hand
Posts: 898
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thomas,
I started to like it. It is not against Naming Policy to write in Arabic way.
Cindy expressed me his anguish about G (as well as you also could not distinguish, sometimes, user name/abbreviation from name) many-many times, even adding that it is OK.
I do not have time for it right now, but if it is, I shall fix it my next session.
It is by 22 of Jan.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic