Spotted an old post on web about using C# in AutoCAD .NET programming. It solely focused on a C# coding style there regarding condition checking in the 'if' statement.
It mentioned a few different coding styles about 'if' this or 'if' that, just as people discussed and favored else where, such as on the SO. It is not a bad idea at all to raise the concern and discuss about it, since in fact it's only a personal preference and nothing really correct or wrong, but the bad thing was that the author obviously laughed at those people who coded or will keep coding this way, or strongly against it:
if( sth.IsChecked == true)
Anything wrong with this? Is it really much worse that the following way that the guy looked favored so?
if( sth.IsChecked )
Compiling faster? Running faster? Less typing? More concise? More consistent? More readable maybe?
Even if all so, not that important, isn't it? Needless to say, not quite so!
If he ever had coded in WPF and the 'sth' were a WPF CheckBox, he would have known which one was the only right way to go. He seemed to have some C/C++ coding experience though, as he mentioned another coding style:
if( true == sth.IsChecked )
That might be so good a skill to be learned hard, it's not really something worth of mentioning from those people's humble point of view who coded with so many different languages many years.
Let's focus on the first two coding styles then. Once again, nothing wrong with either. The first one is becoming more and more adaptable nowadays, however, at least in the WPF world or somewhere else the nullable boolean is widely used. In addition, it would be easier for code migrations from C# to VB.NET for example. If that is a bit more concern than saving a few letter typing, the following might just look better:
if( sth.IsChecked.ToString().Equals(Boolean.TrueString))
Many people like the author there may just laugh at the above code, but the point is many looking weird things have their exact reasons (known or not to some people) to be there!
Think about it another way. Which one is better or all good it depends, more portable, or more consistent with the true comparison in the following false comparison sentences, or is the right way to go in as many as possible circumstances?
if( !sth.IsChecked )
if (sth.IsChecked == false)
if (false == sth.IsChecked)
Recent Comments