• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Writing efficient Java

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SnapShot of the code




or




both are different ways of doing it. and it is guranteed this code will be executed only 1 time.
I am preferring 2nd because it is not using unnecessary boolean's.
whereas in 1st, it looks more clean.

can you let me know the final answer 1 or 2 ?

 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neither one looks particularly handsome.

My preference is always for what is going to be easiest to maintain in the long term. I'd prefer to lose a clock cycle, than lose a day trying to update or troubleshoot some incredibly efficient code. But in this case, option two doesn't look like a huge readability improvement over case 1. I'd stick with the second.

-Cameron McKenzie
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I vote for 1.

I think that readability of the code is more important than marginal optimization.
I am not an expert on JVM but most likely compiler or JVM will optimize performance anyway.

If code is hard to read/maintain it will lead to degrading performance in the long run....

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about a third choice:

 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the third choice is modular. i would prefer third choice if this code is used more than 2 times.
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cameron Wallace McKenzie wrote:Neither one looks particularly handsome.

-Cameron McKenzie



By the way, I wasn't saying it was bad code. It was more a comment on the visual aesthetics.

-Cameron McKenzie
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would go with something similar to Garrett's suggestion:




It's fine to use an extra method like that even in just this one place as long as helps in making the code more readable, testable, accurate, or maintainable. It does all four of those things!
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome Ernest.

i like your all points. less bugs. have to look at 1place only.

this thing should have come to my mind...


 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is another variant from Ernest's solution. It's because the column index doesn't have any meaning outside the array anifestColumns.

 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
making it logical..
 
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For those who missed it, the original index sequence was 0, 1, 3, not 0, 1, 2. So let's fix that while simplifying further:

I would also be strongly tempted to replace getTrimmedUCaseDBColumnValue(x, manifestColumns[ i ]) with a more meaningful (and hopefully shorter) name - but I have no idea what the significance of this method is. The name tells me how the value was obtained, more or less, but gives me no idea what it means. And I would be much more interested in the latter.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that of those two options mentioned in the first post, they do not work exactly the same, and the second one is potentially more efficient!

The && operator in Java is a short-circuit operator. That means that if you write A && B and A is true, then Java already knows that the answer to the expression A && B is true, so it is not going to evaluate B.

In your first example, the JVM is going to evaluate all three expressions first, and after that it combines them in the if-statement. Note that all three expressions are always evaluated, regardless of which ones are true or false.

In your second example, the JVM evaluates the first expression and stops if it is true (it doesn't need to evaluate the second and third expression).

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:The && operator in Java is a short-circuit operator. That means that if you write A && B and A is true, then Java already knows that the answer to the expression A && B is true, so it is not going to evaluate B.



Don't you mean 'if you write A && B and A is false, then Java already knows that the answer to the expression A && B is false' ?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amandeep Singh wrote:making it logical..



Nope. The reader is now wondering what "i" represents, and what the state of "i" at the end of this expression represents, and why we saved "i", and where we're going to use it again.

Mike Simmons wrote:So let's fix that while simplifying further:



My first thought was that this one is extremely nice and clean; then I realized that it's because you blew off several of the arguments. It's still not bad, but to be fair, with out more refactoring, it'll look like this, which doesn't have the same punch:

 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

EFH wrote:My first thought was that this one is extremely nice and clean; then I realized that it's because you blew off several of the arguments. It's still not bad, but to be fair, with out more refactoring, it'll look like this, which doesn't have the same punch:


True - I was figuring I'd probably move those to fields. (If they weren't already.) But it depends what scope they naturally have. Chances are good I'd create a class that matches their lifetime, if there wasn't already a class that did so.
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic