Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Forward Referencing Doubt

 
Joe Harry
Ranch Hand
Posts: 10083
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

Consider the following code,



Is this an illegal forward referencing as we violate the declare before read rule at Line 3? Can anyone explain this please?

Regards,
Jothi Shankar Kumar. S


(removed shout in title)
[ October 15, 2006: Message edited by: Barry Gaunt ]
 
Joe Harry
Ranch Hand
Posts: 10083
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

Sorry....In the above code please consider the return noOfRooms * occupancyPerRoom in the public int initMaxGuests() method and also create an object in the main method.

Regards,
Jothi Shankar Kumar. S
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code won't compile as is since you forgot to return an int from your initMainGuests method. hen you do return an int from that method the code will compile and run, but the result in my opinion is a bit surprising. Try running your code slightly modified so that it compiles and I changed a few of the printlns:



What would you expect the result to be?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best thing to do is to edit your initial post using the icon that looks like a piece of paper with pencil.

No, the use of occupancyPerRoom in the method does not violate the declare before read rule. But using the method where you do will not print the results that you may expect. The value of occupancyPerRoom printed and used in the calculation will be 0 and not 2. (You will have to give the method something to return to get it to compile).
 
Joe Harry
Ranch Hand
Posts: 10083
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Above,

I read that the declaration before read rule (DBRR) should not be violated. So as per that, the declaration can happen in the left hand side but not on the right hand side (RHS). But in the above code the method is on the RHS which very well violated the DBRR. Please anyone?

Regards,
Jothi Shankar Kumar. S
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"But in the above code the method is on the RHS which very well violated the DBRR"

The method is where?
 
Joe Harry
Ranch Hand
Posts: 10083
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry,

private int maxNoOfRooms = initMaxGuests(); //Line 3

In the above line the initMaxGuests() will return return occupancyPerRoom * noOfRooms; which is more or less like,

private int maxNoOfRooms = occupancyPerRoom * noOfRooms;...

So the occupancyPerRoom is read before declared???please can you explain on this.

Regards,
Jothi Shankar Kumar. S
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just a call of the method. The compiler does not see the call to the method and then go to see what the method does.

The code referencing the instance variables is in the definition of the method. At the time the method body is compiled all the instance variables have been defined (even if not fully initialized).
[ October 14, 2006: Message edited by: Barry Gaunt ]
 
Joe Harry
Ranch Hand
Posts: 10083
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry,

I did not quite understand it. Please can yopu elaborate it furthur?

Regards,
Jothi Shankar Kumar. S
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jothi, did you try to run the code I posted? If not, try it... you'll see how the value of "occupancyPerRoom" is not what you might expect when it is first used in your method call.
 
Joe Harry
Ranch Hand
Posts: 10083
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rick,

I tried that same piece of code even before you posted. I waned to know what forward referencing is with respect to method returns.

Regards,
Jothi Shankar Kumar. S
 
yogesh sood
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jothi,

Let me give a try here to summarize what Barry meant.

Reason Code compile without any error as Barry mentioned
"That's just a call of the method. The compiler does not see the call to the method and then go to see what the method does."

So when you write initMaxGuests() then compiler does not go and check body of method to verify what variables it is referring to.

Now, when you run this code, initMaxGuests() will return 0 reason again comes from Barry's statement below.

"At the time the method body is compiled all the instance variables have been defined (even if not fully initialized)."

Therefore value of occupancyPerRoom variable will be 0 rather than 2.Hence value that will be assigned to maxNoOfRooms will be zero.

I hope it helps !!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jothi, have you read Corey's Tipline article on Forward Referencing?
 
Joe Harry
Ranch Hand
Posts: 10083
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry,

Regards,
Jothi Shankar Kumar. S
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic