• Post Reply Bookmark Topic Watch Topic
  • New Topic

Where's the logic?  RSS feed

 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi my favourite crowd!

this time I've got question regarding the use of logical operations in conditional statements. Actually I'm interested in one particular operator - ! .
So, let's say I've got some program where I've got declared earlier method in a class... Let's say Dedication() method that returns true if dedication is found in a given object.
Now I want to use it in different method in such a way that it would call method Dedication() and if there isn't any would assign value '0' to a field 'result'.

I might be crap in it so please forgive me, but is the following code correct? :


Or should it be:

?

I just want to know if such conditional statement would make sense to the compiler?

Thanks a thousand!
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either statement would be fine as far as the not (!) operator is concerned, but prefer the second. The only change I'd make is to lowercase the "D". Methods should be lowercase by convention.
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a follow-up. Why calling this.dedication() is less elegant? Do I not need to pass whatever object I'm 'testing' as an argument to this method? Or just invoking dedication() have the same effect? Or maybe more universal? Did I got lost maybe?? :/

Or it just simply depends on how the calling is made within a method?
Or maybe it's a question for a different topic all together...
 
Liutauras Vilda
Sheriff
Posts: 4925
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I'd change
to
The way clearer, right?
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, better that way. Thanks!
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IDEs often have an option to prepend this. left right and centre, but I am neutral about redundant this.
I agree about the code being spread out; both single‑line versions are difficult to read.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer this, because it's easier on the eyes. And a little easier on the brain. Some will hate it...



 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree

== false is an abomination.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Agree

== false is an abomination.


LOL. I think it comes from this - (where 0 is false and any other number is true).

From K & R (C reference - NOT to be confused with Java):

"The unary negation operator ! converts a non-zero operand into 0, and a zero operand in 1. A common use of ! is in
constructions like
if (!valid)
rather than
if (valid == 0)
It’s hard to generalize about which form is better. Constructions like !valid read nicely (‘‘if not valid’’), but more
complicated ones can be hard to understand.
"
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know. I mean to me it's simply like negation in logic therefore easier for the eye and neater for the brain. I like it this way 'if(!dedication)'.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think somebody had a falling out with their cousin Dennis.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to use that, you need that other abomination necessary in C
if (NULL == x) ...
The == operator is very error‑prone, and we have a perfectly good NOT operator.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I miss integer "booleans" from C

 
Lucian Whiteman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamila Bertran wrote:Hi my favourite crowd!

I might be crap in it so please forgive me, but is the following code correct? :


Or should it be:

?

I just want to know if such conditional statement would make sense to the compiler?

Thanks a thousand!


1) methods should start with lower letter
2) methods should start with a verb
3) code should read like well written prose
4) use the optional this keyword whenever it makes code easier to read

So my take on it is:



Though I am by no means an expert.

Also the result = 0 thing is useful only when you need to initialize the result, if you know that in case of this.isDedicated() == true you must return 0, then just return 0 and do not update that result variable.
Having said that, in case your code just initializes the result variable, I would very much use this construct: int result = !this.isDedicated() ? 0 : someOtherValue;

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!