views:

206

answers:

6

I'm a beginner and I was reading http://stackoverflow.com/questions/237241/what-coding-mistakes-are-a-telltale-giveaway-of-an-inexperienced-programmer. I didn't get few things.

1.Why writing something like this is frowned up on?

if (IsAManMan == true) { 
   Console.WriteLine("It's a MAN, man!");
}

2.what about this?

if (test) {
  return true;
}
else {
  return false;
}

I don't write code like above. Should it be written this way: return test? or for readability I sometimes write it like return test?true:false or return (test==true)? true:false

A: 

These codes do same things. In my opinion, its just readability.

Serkan Hekimoglu
+8  A: 

It's just a tautology. If it rains and If it's true that it rains is exactly the same and therefore, you can (or should) leave out superfluous comparisons, checks and returns.

Dario
+12  A: 

In this example:

if (IsAManMan == true) { 
   Console.WriteLine("It's a MAN, man!");
}

In most languages, if conditions always evaluate a Boolean expression, which means IsAManMan can only either be true or false. Comparing to true or false is thus unnecessary because it's implied. So we just write this instead:

if (IsAManMan) {
   Console.WriteLine("It's a MAN, man!");
}

And in this example:

if (test) {
  return true;
}
else {
  return false;
}

Following the above example, this means test can only either be true or false, which is like saying:

if (true) {
  return true;
}
else { // If it's not true, it's false
  return false;
}

That's again unnecessarily verbose code, so we just

return test;

instead.

In the end, it's still a matter of preference and readability. For example, if you think if (IsAManMan == true) is easier to interpret than if (IsAManMan), you can still write it that way and the compiler won't mind.

BoltClock
the compiler doesn't mind a lot of things that drive ME bananas. If `if (IsAManMan)` is unclear, it means you need to learn more about the language you are using.
Matt Briggs
+2  A: 

In both cases you're testing the value of a boolean variable. Because your value is already a boolean, there's no need to explicitly test against true because it's implied in the if statement (and similar constructs that contain a boolean test, like loops)

For example

if(isAManMan == true){ 
    //...
}

could evaluate to

if(true == true){ //answer: true
     //...
}

or

if(false == true){ //answer: false
     //...
}

which is the same as just writing

if(isAManMan){ 
    //...
}

In your 2nd example, you're examining the value of another boolean value before deciding what boolean to return.

Again,

if (test) {
  return true;
}else {
  return false;
}

could evaluate to

if (true == true) { //returns true
  return true;
}else {
  return false;
}

or

if (false == true) { //returns false
  return true;
}else {
  return false;
}

The if statement is redundant, because the variable already holds the value you want to return, hence this is equivalent, and preferred:

return test;

It's important to note though, that your examples are completely valid, however a good compiler will optimise them into the improved versions I gave, so ultimately it comes down to readability and convention. I believe the versions I gave are more readable and closer to the generally accepted conventions.

chrisbunney
A: 

Due to this being tagged language-agnostic:

There are languages where keywords can be used for variable names. In FORTRAN, this is a valid statement: IF IF THEN THEN ELSE ELSE.

If true is defined to be a boolean variable with value false, the proposed shorthand solutions are not the same as the more verbose ones. Neither if test, IsAMan, true, and false are not boolean values in the first place.

A real world example in Python 2.0

>>>True = False
>>>True
False

Tongue in cheek.

Ralph Rickenbach
A: 

Say you want to return a boolean, and not the actual value of test for the 2nd case.

You could do

return !!test 

in most languages, or something like

return bool(test)

which is more explicit and readable.

Alex JL