Code smell - READIBILITY

In my opinion readability is the most important attribute of code.

Good code should be easy to read.
That makes code easy to understand, maintain and REUSE.

 

Ignore the meaning of the code but look at the layout... What would you prefer?

  if (myTrueValue) myFirstTempVariable = 1;
  if (!myTestStatement) mySecondTempVariable = 0;
  if (mySecondTestStatement) myThirdTempVariable = 0;
  if (!myFourthTestStatement) mySecondTempVariable = 1;
Hard to read

 

  if (myTrueValue == true) myFirstTempVariable = 1;
  if (myTestStatement == false) mySecondTempVariable = 0;
  if (mySecondTestStatement == true) myThirdTempVariable = 0;
  if (myFourthTestStatement == false) mySecondTempVariable = 1;
GOOD: Already much easier to read

 

  if (true == myTrueValue) myFirstTempVariable = 1;
  if (false == myTestStatement) mySecondTempVariable = 0;
  if (true == mySecondTestStatement) myThirdTempVariable = 0;
  if (false == myFourthTestStatement) mySecondTempVariable = 1;
GOOD: What about this? Already much easier to read??

7 comments:

Juri Strumpflohner said...

Readability is most important, I fully agree. But still, for boolean values I find it more intuitive to directly write "if(myTrueValue)" instead of explicitly writing "if(myTrueValue == true)". But I guess this very much depends personal preferences. The main importance is that everyone in the team uses the same style. :)

Are you already back to downunder?

Anonymous said...

I agree with you : readability is the most important.
It is for that .. I prefer VB.NET.

Dominique.

Peter Gfader said...

>> Are you already back to downunder?

Not yet, starting this Saturday. Monday I am in Bolzano. I will visit SIAG I think...

Peter Gfader said...

Hi Dominique
Would you do the same in VB.NET like in the last example in C#?

Peter Gfader said...

Hi Juriy

I agree: I would also write "if(myTrueValue)" instead of explicitly writing "if(myTrueValue == true)". But in this case maybe it would make sense.

Anonymous said...

Hi Peter
Sorry for the long delay to answer.

in vb, we can write :
If myTrueValue = True Then myFirstTempVariable = 1
If (myTestStatement = False) Then mySecondTempVariable = 0
If (True = myTrueValue) Then myFirstTempVariable = 1 ' I don't like that

differences with c# :
1) ( ) or not
2) = instead of ==
3) there is no ;
4) there is the word "Then" which separs the two members : comparison and action. It is easy to read.
5) there is no ! to indicate not. It is better.

For all these reasons (and others) I prefer VB.
And now VB and C# do the same, Microsoft said ;-)

Dominique

Peter Gfader said...

Hi Dominique
I agree with 4) and 5) and I like that in C# we must use parenthesis 1)

What do you mean by "And now VB and C# do the same"

cu

Post a Comment

Latest Posts

Popular Posts