• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Refactoring Exercise: An example

 
Ranch Foreman
Posts: 266
12
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

D.J. Quavern wrote:My hardware teacher called my code, (brace for it), "not a capital sin, but quite close", and "à criminal waste of memory" 😎.


Who cares about memory anymore these days?!! This is veeery old-school thinking, and not in a good way. The bulk of the cost of ownership in software these days is in maintenance. There's about a 1:10 to 1:50 ratio of good code to bad code out in the world today and even that might be too generous. There's way more bad code than there is good code out there. One big reason: there are way more bad programmers out there than there are good programmers.

Good programmers make sure they test and refactor their code. Good programmers make sure their code can be read and understood by others. Good programmers realize there is no "my code" any more. There's just code that needs to be maintained and enhanced. The only code that is "your code" is the code you're working on at the moment. Once you commit that code, it becomes "OUR code," where "OUR" refers to everyone in the organization that is paying for the code to be written.

These days, "criminal waste" is when you write code that is difficult to read and difficult to change. I can go to the local drugstore and get more memory any time of the day. Yeah, it's that cheap and ubiquitous. And we haven't even talked about cloud-based platforms. Basically, hardly anybody cares much about memory these days; except maybe hardware teachers stuck in the year 1966.    



This is not this code that was so highly acclaimed by critics. I was talking about my hardware teacher, not the competitive programming.
We train with small microprocessors and some registers are only 16bits, so memory is an issue.
 
D.J. Quavern
Ranch Foreman
Posts: 266
12
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

D.J. Quavern wrote: . . "à criminal waste of memory" 😎. . . .

If it is that criminal, please tell me where you live, so I can go there and shoot you.


That's extreme!     Are you... an American? Is it why you need this second (first? fourth?) amendment of yours, to effectively manage (memory leaks)   ?

Campbell Ritchie wrote: Alternatively, write if statements with and without {} and inspect the bytecode with javap −c MyClass. If you can actually see any difference, I shall have to kill you. If you can't see any difference, where is that waste of memory?



What is MyClass and what does it do?

Campbell Ritchie wrote:As you will see here, when I was young, 4kB of memory cost about the same as every house on the street (we have some short streets here). I can go onto eBay and buy 10⁶× that amount of memory for less than I could spend on two pints of beer. So the cost of the memory you are wasting can be calculated as less than the cost of 1 microlitre of beer. And that is criminal, isn't it.



This comment was directed to a hardware code where memory was a constraint , not in Java
 
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you're saying but it sounds to me like your comment about your hardware teacher was about some other code you wrote. If so, why say that in the context of this thread where it could easily be taken out of context?

On the other hand, even small ARM-based computers like the Raspberry Pi have enough memory to not have to worry that much about penny pinching bits and bytes to eke out every last bit of available memory space.

Sure there are still contexts where memory is a premium but for the most part, Donald Knuth's admonition still should be heeded.
 
Marshal
Posts: 65814
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

D.J. Quavern wrote:. . . . Yes, . . .

Whenever I try timings, I can find times varying by 25% in successive runs. So I can find that sort of difference, run the program again, and find the difference the other way. That is why I didn't think it is significant.

. . . to minimize the confusion (ironically), and because InputReader throws an Exception in main. . . .

Yes, that is very ironic. Please find out why you are suffering that exception. Also please move the reading code out of the main() method. I am sure we have referred you to Winston's FAQ many time already.
 
Campbell Ritchie
Marshal
Posts: 65814
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

D.J. Quavern wrote:. . . small microprocessors . . . so memory is an issue.

Sorry, I didn't know that before replying. I missed several posts because I didn't realise we were on page 3 of this thre‍ad. I won't let you know where I live in case you come and shoot me.

The 4th amendment is about managing blood leaks.
 
Campbell Ritchie
Marshal
Posts: 65814
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

D.J. Quavern wrote:. . . What is MyClass and what does it do? . . .

I should have written Foo instead, shouldn't I It is something to display my class with:-Now correct lines 12‑13 by indenting them correctly and adding the appropriate {} and see whether there is any change in the bytecode at all.
 
D.J. Quavern
Ranch Foreman
Posts: 266
12
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Not sure what you're saying but it sounds to me like your comment about your hardware teacher was about some other code you wrote. If so, why say that in the context of this thread where it could easily be taken out of context?

On the other hand, even small ARM-based computers like the Raspberry Pi have enough memory to not have to worry that much about penny pinching bits and bytes to eke out every last bit of available memory space.

Sure there are still contexts where memory is a premium but for the most part, Donald Knuth's admonition still should be heeded.



Well, it's not like this thread has not wandered out dozen times. Seriously this thread is FILE-d with random comments and probably RANKing highest in the out of context remarks   .

I wrote "my hardware teacher". This piece of code is not hardware.




 
D.J. Quavern
Ranch Foreman
Posts: 266
12
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

D.J. Quavern wrote:. . . small microprocessors . . . so memory is an issue.

Sorry, I didn't know that before replying. I missed several posts because I didn't realise we were on page 3 of this thre‍ad. I won't let you know where I live in case you come and shoot me.

The 4th amendment is about managing blood leaks.



We have excellent gun control laws in Sweden, do not worry.
Besides, I like you.
 
D.J. Quavern
Ranch Foreman
Posts: 266
12
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

D.J. Quavern wrote:. . . . Yes, . . .

Whenever I try timings, I can find times varying by 25% in successive runs. So I can find that sort of difference, run the program again, and find the difference the other way. That is why I didn't think it is significant.

. . . to minimize the confusion (ironically), and because InputReader throws an Exception in main. . . .

Yes, that is very ironic. Please find out why you are suffering that exception. Also please move the reading code out of the main() method. I am sure we have referred you to Winston's FAQ many time already.



I did not write the class, just borrowed it online.

Junilu is doing all the removing right now.

I never saw this MyClass manipulation before!
I don't even know what javap means...
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This latest digression is starting to wander too far off range...

Please rein it back in to the main topic or I will be obliged to start cutting this section out and moving it to Meaningless Drivel. Thanks for everyone's cooperation.
 
Marshal
Posts: 7181
492
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted to thank for this thread, Junilu.

I haven't study it much as been away for few days, but will familiarize with it soon. I am sure I will learn a lot from that.

Suggesting to study it to all members.
Staff note (Junilu Lacar):

I learn a lot of things just by putting my thoughts into writing like this, so it's as much for me as it is for others. But still, you're most welcome. I'm glad to help anyone willing to dredge through the thread learn something.

 
Campbell Ritchie
Marshal
Posts: 65814
250
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

D.J. Quavern wrote:. . . javap . . .

At risk of Junilu shooting me (): this link lists all the development tools. You will find javap one line above the first horizontal dividing line. Click the link for your OS on the right.
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just curious, has anyone else tried their hand at refactoring the original code to make it more testable and more expressive? There's not one right way to refactor code, you know, and I'd be very curious to see how others might approach the problem we've been discussing.

FWIW, I have continued with the exercise and started moving code out to proper objects and instance methods so that I can start getting rid of the static methods. This has been quite an interesting exercise for me and I'm actually learning a lot from it. I'd love to see other approaches. It would be great to see something where I can say, "You know, that's an interesting approach. It might even be better than what I tried."

Any takers?
 
D.J. Quavern
Ranch Foreman
Posts: 266
12
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to disgress, but I think it's cool.
When Junilu (and maybe someone else?) be done I will make a blog post of this Kattis problem, and the refactoring that followed.
 
Saloon Keeper
Posts: 10669
228
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu, was there a particular part you wanted to see refactored?
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not in particular. I was just interested in seeing other approaches, like maybe starting with moving to objects right away. I may get around to doing it myself if nobody else does.
 
Stephan van Hulst
Saloon Keeper
Posts: 10669
228
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tend to start with the basic building blocks, so I have a strong fundament to write the rest of the application on top of. After I wrote the testcases earlier in this thread, I made them pass until I had a fully functional Position class. Here it is:

Using this Position class, I got my Bishop to solve the simplest test cases:

All that's left to do is implement the findHopTo(Position) method, which finds a position that is on a diagonal with the bishop's current position and on another diagonal with the specified position.
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Stephan: can you please remind me again what are you using to be able to write API contract DSL like lines 13 and 14:

I vaguely remember seeing this sort of code from you before but I have since forgotten about it. Looks useful and I'd like to at least have it in my toolbox.
 
Stephan van Hulst
Saloon Keeper
Posts: 10669
228
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a library I wrote myself and is heavily based on Hamcrest. If you give me a few days I'll prepare a release for you. If you haven't heard from me, remind me at the end of next week.
 
D.J. Quavern
Ranch Foreman
Posts: 266
12
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will definitely make a blog post about it (when my electronics is done...), so I hope one of you volunteers to donate some good code . Junilu I'm waiting on you     .
 
Ranch Hand
Posts: 43
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why is that a not (!) ? I'd have thought we'd want the positive case there.
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because if they're on the same diagonal there's no need to find a hopTo position (the step that gets on the same Diagonal as the other)

The idea is that if the start and end positions are not on the same Diagonal, you go from start -> hopTo -> end
 
Bartender
Posts: 3519
150
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This topic is geared towards the standard chess game. But if you take, for instance, the european checkers version, played on a 10x10 board, you can have the exact same question, using kings.

Second: do not call a square 'Position'. Position is used to denote the situation of the game, like in: 'blacks position is hopeless'.

As an alternative, I used Pythagoras to solve this question. Not shorter, but also a lot of fun.
 
D.J. Quavern
Ranch Foreman
Posts: 266
12
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:This topic is geared towards the standard chess game. But if you take, for instance, the european checkers version, played on a 10x10 board, you can have the exact same question, using kings.

Second: do not call a square 'Position'. Position is used to denote the situation of the game, like in: 'blacks position is hopeless'.

As an alternative, I used Pythagoras to solve this question. Not shorter, but also a lot of fun.



Is it the same approach as me, but with a triangle instead of 2 lines intersections? Can I see it     ?
 
Piet Souris
Bartender
Posts: 3519
150
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops... of course you can see it, but I've written it in my 'bad habits' mode, so gonna have to refactor... and do you want tests going with it (I've used zero tests   )?
 
D.J. Quavern
Ranch Foreman
Posts: 266
12
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Oops... of course you can see it, but I've written it in my 'bad habits' mode, so gonna have to refactor... and do you want tests going with it (I've used zero tests   )?



's okey! I don't have "bad code allergy" 😀! I can see it as it is.
I wanted ton solve the problem with Pythagoras /right triangles but I did not know how to handle side length (which were not integers) in relation to the length of the bishop's path (if you understand what I mean!). For right triangles, I did not know how to work with 90 degrees angles.
So I would really love to see your solution.
 
Piet Souris
Bartender
Posts: 3519
150
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi DJ,

when the heck does that Souris come up with that code, I hear you ask. Well, this morning my beautiful hair was godlike blond, but now, after working on this code, hair is fully gray, that is, what is left of it    

When I was refactoring, and although Junilu makes that look easy it is not, I discovered a nasty bug that took me quite some effort to repair, and while doing some real tests, I discivered a fatal flaw in my reasoning, that made a useles (but funny) method totally worthless.

The starting point was to avoid the use of any diagonal related knowledge (which I made use of anyway, but in a disguised way), and thus the method is like going from Stocholm to Kopenhagen, not via the bridge, but via the SouthPole.

I have it almost ready, and either I post it here, for you (or anyone) to witness my stupidity, or I can put it in my GitHub account. Still interested, by yhe way?
 
D.J. Quavern
Ranch Foreman
Posts: 266
12
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:hi DJ,

when the heck does that Souris come up with that code, I hear you ask. Well, this morning my beautiful hair was godlike blond, but now, after working on this code, hair is fully gray, that is, what is left of it    

When I was refactoring, and although Junilu makes that look easy it is not, I discovered a nasty bug that took me quite some effort to repair, and while doing some real tests, I discivered a fatal flaw in my reasoning, that made a useles (but funny) method totally worthless.

The starting point was to avoid the use of any diagonal related knowledge (which I made use of anyway, but in a disguised way), and thus the method is like going from Stocholm to Kopenhagen, not via the bridge, but via the SouthPole.

I have it almost ready, and either I post it here, for you (or anyone) to witness my stupidity, or I can put it in my GitHub account. Still interested, by yhe way?



Yes, I want to see all of it, including all mistakes 😀.

Haha I know the issue. I pull att my hair in frustration when I have a code problem. So it's standing as greasy chopsticks after à kattis session. And not black anymore!
 
Piet Souris
Bartender
Posts: 3519
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi DJ,

it took a little longer. I added support for checkers (international version) and that was tougher than I expected. You probably know that game. Only the dark squares are used, and on a 10x10 board the 50 squares are named "1" up to "50". Unfortunately, square "1" is the rightmost dark square on blacks first row. So that meant I had to incorporate a Board parameter to my interface. And implementing the checkers notation was a nightmare.

Well, I refactored it a little, as I promised, and I leave it up to you, as a training, to refactor it further and write JUnit tests. But no obligation of course, I would not be surprised if you had better things to do than trying to improve a dead horse.

Challenge: find the fatal flaw.

PS: I've learned my lesson, so if you, in your upcoming movie (please ?) you call the king "Shirly", I won't object.

 
D.J. Quavern
Ranch Foreman
Posts: 266
12
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Piet!

Will you be disappointed if I promise to come back to you a bit later, when I have a little personal time? I am very stressed about an assignment that I have no idea how to solve .
 
Campbell Ritchie
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

D.J. Quavern wrote:. . . come back to you a bit later . . . an assignment  . . .

I am a bit busy myself, because I opened my big mouth and promised to go and stand on stage and sing. Giorgio in the Gondoliers by W S Gilbert and A S Sullivan. Please show us as much as you are allowed of your assignment.
 
D.J. Quavern
Ranch Foreman
Posts: 266
12
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

D.J. Quavern wrote:. . . come back to you a bit later . . . an assignment  . . .

I am a bit busy myself, because I opened my big mouth and promised to go and stand on stage and sing. Giorgio in the Gondoliers by W S Gilbert and A S Sullivan. Please show us as much as you are allowed of your assignment.



This is a bit what happened to us too   .

Sorry Junilu, this is not to spoil your thread, but just some background information:

We have a class assignment that includes programming hardware (the program must run on a PIC32 chipkit).
I opened my big mouth (aka wrote an abstract where I promised to deliver...) and said we would be doing an obstacle avoiding robot, that would additionally issue angry beeping when faced with an obstacle.
I have no idea how to write code for a moving robot in C with the PIC32, let alone assemble a robot. I have never done Technical Lego before, let alone Arduino!

So a lot of time has gone in research so far, without even knowing if it will work. If it's ok with you Ranchers, and if someone knows PIC32 I can post about the project in the appropriate thread.
 
See ya later boys, I think I'm in love. Oh wait, she's just a 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!