|
A lot of code I have come across recently has
If (null == something)
whereas I tend to write
if (something == null)
which seems to me to be much more logical and easily readable -
So the question is, is there a good reason for using the former over the latter?
___________________________________________
.\\axxx
(That's an 'M')
|
|
|
|
|
Hi,
the origin is it protects you against a typo where you drop one equal sign; then constant=variable yields an error, whereas variable=constant may not (it wouldn't in C/C++, it most often would in C# unless the types are bool). (*)
However, I am with you, it does not look good, and any decent compiler would normally (there are exceptions conceivable) generate a WARNING message, either saying: "are you sure that is what you intend?" or "condition will always be true/false". However MS compilers seem not to do so.
[ADDED] (*) which is another good reason never to write things such as if (someBool==true)... , just write if (someBool) ... .
[/ADDED]
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
modified on Thursday, July 2, 2009 8:10 PM
|
|
|
|
|
The "null == something" form (or in general, "<literal> == <variable>" form), is simply not useful in C# or Java.
The purpose of this form was to avoid accidentally typing an assignment rather than a comparison in C/C++. C# and Java will not let you make this mistake since the condition must evaluate to a boolean and a simple assignment will not (unless you compare the assignment to something).
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB
|
|
|
|
|
_Maxxx_ wrote: is there a good reason for using the former over the latter?
There is, and AFAIK this reason comes from the good old C/C++ days. It was a very common error (the most common of all) to type if (reason = null) instead of if (reason == null) . This small little typo led to very strange program behaviour and was very hard to debug, because it's very easy to overread it when inspecting the code. The problem was that the C/C++ - Compiler didn't even issue a warning when you typed something like if (reason = null) , but it immediately complained about if (null = reason) .
So it's all about correctness and maintainability. I even saw companies with coding guidelines that dictated this style.
Regards
Thomas
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
Yeah, what they said. It only works when comparing an Rvalue to an Lvalue, which isn't necessarily all that frequently, and my argument against it is that if I can remember to write it that way, then I will probably not make that mistake anyway so it's fairly pointless.
I worked for a company with the former specified in the company coding standard (for writing C), but by the time I left, even the "guru" (who had written the standard) had admitted that it wasn't that important and wasn't enforcing it.
|
|
|
|
|
So in summary, people used to do this in some other programming language, so have continued to do it in a different language, even though it serves no purpose, and indeed makes the code harder to read?
Sack 'em,. I say, Sack 'em!
___________________________________________
.\\axxx
(That's an 'M')
|
|
|
|
|
_Maxxx_ wrote: Sack 'em,. I say, Sack 'em!
I am also in your side that this is not that readable comapring the other version of it (ie. something == null - which is more simpler and expected), but I dont agree with you that (null==something) is that much "unreadable" that you need to sack somebody. If you have hardship reading this style, according to your way of thought, you are also a good candidate to get sacked.
Moim Hossain
R&D Project Manager
BlueCielo ECM Solutions BV
|
|
|
|
|
_Maxxx_ wrote: Sack 'em,. I say, Sack 'em!
On a serious note, people who write code this way are generally people who've used something like C and have been caught out by assigning a value rather than doing a comparison. The fact that they've made a conscious effort to change their coding style to make sure that their code is less error prone is probably an indicator of a good developer, rather than anything else.
It definitely isn't definatley
|
|
|
|
|
Right, but even "modern" C compilers will issue a warning if the mistake is made:
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
aa.c:
Warning W8060 aa.c 25: Possibly incorrect assignment in function main
And HP C:
if ( result = 5 )
....^
%CC-I-CONTROLASSIGN, In this statement, the assignment expression "result=5" is used as the controlling expression of an if, while or for statement.
at line number 13 in file MY$ROOT:[000000]AA.C;1
The D language defines the mistake as an error.
|
|
|
|
|
If (null == something) and if (something == null) are nothing but the same.
But I/many people always prefer - If (null == something),
because if we use - if(something == null), sometimes it may happens that instead of == we use = and that change the value of 'something'. And if 'something' is used throuout the programe, you can imagine how wrong result it will create.
So using If (null == something) is always be safe.
Hope this will be helpful to you.
Thanks,
-Yogesh Patil.
|
|
|
|
|
Hi Ive been messing around with GDI+ and Ive managed to make a simple line graph by plotting the graph points and drawing a line between them .
However the result is rather ugly , Im trying to make a graph without points using a curved line but im not sure where to start .
Can anyone point me to an example or suggest a way to draw this type of graph/line ?
|
|
|
|
|
Hi,
look for Bezier curves or spline functions. They are everywhere on the internet, as well as in CP articles.
BTW: such curves will connect only a few points; for larger graphs, you need more curves, not polynomials of a higher order (they tend to oscillate too much).
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
Yeah the only examples i can find are based on 3-4 points i need an example that can handle 50 points .
Basically i have a bunch of values and im incrementing the x values x 10 each time and i need a function that can take plot all this in one go using loops not manuly inouted information
|
|
|
|
|
The trick when more than 3 or 4 points need a smooth curve, is to apply a bezier or spline on a subset of those points, then move on and make sure there is continuity (same tangent).
Sorry I don't keep a reference around.
[ADDED] Googled for "continuous cubic splines" and found lots of things including this[^].
[/ADDED]
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
modified on Thursday, July 2, 2009 8:21 PM
|
|
|
|
|
Isnt that what the robot voice says at the start of sim city 2000
Perfect thanks
|
|
|
|
|
Hello all,
I'm looking for a faster way to get a string representation of a bitmap image without looping through the coordinates in the getpixel(x,y) method. Can it be done more efficiently?
Thanks.
|
|
|
|
|
Yes, use LockBits [^] and unsafe code. (getpixel also does something like that internally, it calls a native method that performs some magic and probably locks the bitmap as well)
|
|
|
|
|
Perhaps this is what you're looking for:
MSDN[^]
|
|
|
|
|
Thanks to both of you for the quick replies, and right on the money with them too.
|
|
|
|
|
Hi everyone,
My datagrid is populating all data. Now i want to add tabs to my WINFORM so that the data can be grouped according to the status_type. Can you let me know how this can be done. I am using Linq to query the database.
Thanks,
|
|
|
|
|
What is status_type? What does Linq have to do with tabs? What will be the purpose of the tabs?
|
|
|
|
|
Status_type relates to my AD Hoc query. It has a list of employees with their current status as "Active" , "In-Active" , "Overseas".....etc. I want to define tabs so that a tab named ACTIVE can list all the employees with the status "Active" and similarly the rest.
Cheers!
|
|
|
|
|
And I suppose you don't want multiple datagrids? Otherwise there wouldn't be any problem at all..
|
|
|
|
|
Hi,
what you could do is this:
- add a TabControl to your Form
- give it any number of TabPages you want
- put one DataGrid(View) on each TabPage
- create a different query for each TabPage
- make sure the queries don't get all launched at once, maybe just get one of them launched by the TabIndexChanged event (plus the initially visible one right away if necessary).
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
hi thanks, can u show me an example for writing queries into tabcontrols
|
|
|
|