• Post Reply Bookmark Topic Watch Topic
  • New Topic

Change 2 if conditions into 1 if condition ?  RSS feed

 
Amandeep Singh
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Snapshot of the code:



I want to change this into 1 if condition. Also i want to make sure when ist if condition is true, then only i make a object using the new.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's simply not possible. One if means either both or neither get executed. What's wrong with that code you have right now?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In theory, this is actually possible... because the ECDutyCompCodeXSolution variable is only instantiated and used in one place -- and then goes out of scope -- you can get rid of that variable, and use the object directly. Once you do that, you can merge the two if together, like so...



Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with that code you have right now?



And I agree with Rob... What is wrong with the code that you have now?

Henry
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:In theory, this is actually possible... because the ECDutyCompCodeXSolution variable is only instantiated and used in one place -- and then goes out of scope -- you can get rid of that variable, and use the object directly. Once you do that, you can merge the two if together, like so...



Henry

You are 100.00% right of course. And if you need that object after the if statement you can do it as well:
objECDutyCompCodeXSoln will stay null if recalculateDuty is true because of the shortcircuit operator &&.

Still, I would prefer a nested if, if only to make the code more readable.


As a side note: is it perhaps not possible to make the CheckForMiniDutyCompCodeX method static? That way, you don't even need to create an instance:
Seeing as you're creating instances just to be able to call that method, it might be worth the little effort.
 
Amandeep Singh
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with that code you have right now?


it is nothing wrong with the code. the only reason i want to change it is because it is kind of perception in java programmers, to reduce the if conditions as much as possible.
Well i agree, it comes at the cost of readability.

So you still would prefer the two if conditions ?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be referring to a metric called cyclomatic complexity. Various tools can evaluate code and give you a score based on how many distinct paths through the code there are. The "less-readable" way here might actually fool the tool into giving a lower score. (Or it might not. I'm really not sure.) However, metrics are just guidelines. You are a better judge of readability than a tool is!

Using short-circuited compound conditionals can often improve readability, usually if both halves are both simple and clearly related (and don't do an assignment). For example



That saves one level of indentation and too many of those can get annoying. Also it expresses a single idea if that makes sense. In your case, though, I agree that the nested ifs are more readable. Going all zen ... don't split one idea into two pieces, but don't try to combine two ideas into one. Also, from discord find harmony, the simplest things are often the truest, and always separate darks and lights when you do laundry.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:What's wrong with that code you have right now?

Um, well, the complete lack of indentation comes to mind. Not to mention the horribly obfuscated identifiers for both variables and objects. Both of these seem much, much more important than any other issues I can imagine here.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:
Rob Prime wrote:What's wrong with that code you have right now?

Um, well, the complete lack of indentation comes to mind. Not to mention the horribly obfuscated identifiers for both variables and objects. Both of these seem much, much more important than any other issues I can imagine here.

You're right of course, but that's not what I meant
 
Amandeep Singh
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:
Rob Prime wrote:What's wrong with that code you have right now?

Um, well, the complete lack of indentation comes to mind. Not to mention the horribly obfuscated identifiers for both variables and objects. Both of these seem much, much more important than any other issues I can imagine here.


Mike these identifiers are based upon application, what it is doing currently.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Hungarian notation to identify objects seems somewhat redundant for almost all Java code.
 
Amandeep Singh
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Using Hungarian notation to identify objects seems somewhat redundant for almost all Java code.


what type of notation is this ?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://en.wikipedia.org/wiki/Hungarian_notation

In short, when the start of the name indicates the object type. In your case, objECDutyCompCodeXSoln.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!