|
Of course you could use your form:
if (value <= 0)
But if values greater than zero are OK, and anything else should give an exception, then why not write it in the original form:
if (!(value > 0))
{
throw new ArgumentOutOfRangeException("....");
}
This could be considered a more direct translation of the requirement, and therefore more understandable.
Of course, it may just have been a lack of thought, but I'd argue there are times when it would be beneficial not to simplify expressions, particularly when you have a compiler that will reduce it to the same form for you so there is no performance penalty.
|
|
|
|
|
I can top it off!
static void Main(string[] args)
{
var b = true;
if (!!!!!!!!b != false)
Console.WriteLine("hu?");
else
Console.WriteLine("ha!");
}
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
"hu?" is printed.
|
|
|
|
|
Indeed!
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
I use less ! on my messenger
-
Bits and Bytes Rules!
10(jk)
|
|
|
|
|
I wouldn't really call that a "gem".
|
|
|
|
|
Another alternative that I didn't see mentioned in the replies. Was this code hand written or produced by a generator?
I've been playing with the CodeDOM and there are some structures that you can define that would likely generate exactly that code.
|
|
|
|
|
|
It doesn't make much sense for an int , but it might make sense for an int? (Nullable<Int32> ).
The ordering operators (<, <=, >, >=) on Nullable<T> will always return false if either operand is null , so !(value > 0) would be equivalent to value == null || value <= 0 .
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
It's an int, nothing but the int, so help me glod.
|
|
|
|
|
In Visual C++ MFC, there's a CString. And to check if a CString has values, it's "if (!str.IsEmpty()).
So the QA person, who had a degree from Stevens Institute of Technology, sat there and argued with me for 2 HOURS, and brought it all the way up to the VP, because it wasn't the "obvious way to do the logical operation." Didn't matter at all that is was the way Microsoft implemented it, and we had to use it.
|
|
|
|
|
Hopefully you'll be gratified to learn I just used this as an example to one of my students, introducing him to refactoring poor code and [specifically] bad if statements.
|
|
|
|
|
If it stops anybody else making the same mistake, then I'm glad to help.
|
|
|
|
|
Ahhh, teaching him a lifetime skill huh? I hope most of us, who read bad code go:
Is it working correctly?
if no, write a bug and hope someone assigns you ownership so you can fix it.
Hope it's approved, and whoever is assigned the bug takes the time to fix it.
else: Is this egregiously bad, causing performance issues or something else that might cause severe system problems?
if yes, write a bug etc.
else: Do you work in a best practice shop and this violates a best p?
if yes, write a bug etc.
else: Do you work in a best practice shop
if yes, suggest a new best practice reffering to your source and hope it is adopted and the source is addressed.
else: ask yourself if it is worth suggesting to your manager one more thing that could be improved.
if yes, wow, you have a manager who listens to you and acts on your suggestions, or you are working for a new manager, or you are a perrenial optimist.
else: Join the regular ranks, shake your head about the code you see, and go on with your life.
Maybe I have a gift for seeing bad logic.
It took me three days to convince someone this was a bug:
if (current_thread_count <= maximum_thread_count + thread_count_to_add)
add_new_threads(thread_count_to_add);
It took asking for the specification document because I was told this met specifications. I agreed that this exactly met specifications, the only problem is, that specifications aren't asking for what is intended. Huh, what? It took going to the lap, finding out what values they were configured for (maximum_thread_count=120, thread_count_to_add=25) and showing what that would do. (Say your current count is 119, if you add 25 more, the current count would go to 144. You don't want that to happen, right? "Right." Well, 119 is less than 145... "How did you come up with 145" Well, the first value is 120, the second value is 25, added together they are 145. Well, the light dawns, but we continue the exercise proving that the count would go up to 169 because the loop is immediate when if the statement was true and the request is relatively immediate. In theory the count could go to 170.
Anyway, if there is a heavy enough load the service machines should blow up with a thread allocation error. The lab guy who gave us the numbers, pipes up "Oh, yea, we have that problem all the time!"
I'm thinking it would have been nice to know that the problem existed instead of just reading C# code to get up to speed on what the group was doing while waiting for my next assignment. Also, how can the specs get through code review. The person creating the code doesn't see it. I have so much trouble convincing someone there is a problem. I'm passively reading it and it's so bad it almost hurts my eyes.
I heard the problem was significantly reduced, but still occurs. I was on an assignment so it can wait. (Just like everyone else in the group while I was there.)
|
|
|
|
|
Your alternate solution is just as valid. To me the original is perfectly readable, but I'd prefer:
if (value > 0) {//do something useful}
else (throw ...);
Since you have CDO, this is probably too much reading to do before you get to the else statement.
|
|
|
|
|
It is accurate, but inelegant!
------------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
CCC League Table Link
CCC Link[ ^]
|
|
|
|
|
Firstly, with most languages both examples will produce the same code, so there is no need to prefer one from a performance standpoint. So the choice comes down to clarity and depending on the specific situation (and perhaps the individual) either one could be preferable. The first example has the advantage that it more directly reflects the comment. I don't really regard this as a coding horror.
Steve
|
|
|
|
|
Found this in production code. Variable names changed to protect the innocent:
For i As Integer = 0 To 0
Dim lb As LinkButton = GetLinkButton(i)
Next
Future proofing the code?
|
|
|
|
|
Scalablility
|
|
|
|
|
Optimization... reduce the number of iterations until you achieve reasonable performantcy*.
* I just made that up.
|
|
|
|
|
Boss said to the newby, "take this code and make it run faster."
Gary
|
|
|
|
|
Warning: The Following is a Rant.
From time to time I get into the religious 'To Stored Proc or not To Stored Proc' argument.
Let me confess right now. As much as I appreciate all the pro's of Stored Procs, I still prefer to not use them when it can be avoided.
It may be a technically inferior solution but I still prefer to create functions and subs in my VB or C# code that perform the equivalent task.
I have finally decided that the reason for my bias is that the greatest Code Horrors I deal with now are badly written Packages and Stored Procs etc.
It's a bloody nightmare.
And to top it all, I regularly have to deal with the fact that the features needed to debug aren't installed, or they're running some cut down version of a DBMS.
So I'm debugging this rats maze of code using techniques that I haven't used since I wrote Basic on my Sinclair Spectrum. Plug in Values. Run. Check Log File. Nothing Happened. Plug in Values. Run. Check Log File. Something unexpected happened. repeat to fade.
Right now I'm trawling through the PL/SQL code of a very large globally known company. Honestly I've decided that what I'm looking at is a cast off from that team of monkeys that are working on the complete works of Shakespeare.
I also regularly have to deal with Databases that contain hundreds of functions, procs and views where nobody knows if they are needed anymore but everybody is terrified to modify or remove any of them.
I see companies who spawn a new copy every time a proc needs to be changed (just in case) and use that.
I see companies who have no Gold version of their DB. When they need a DB either for test or for a new Production site, they just copy an existing production site.
As for version control. It seems DB Objects live in some abstract zone, like International waters that aren't covered by the treaties that cover version control.
This is no way to live.
Thanks for letting me get that off my chest.
Now back to the rats maze.
-Richard
Hit any user to continue.
|
|
|
|
|
Richard A. Dalton wrote: I have finally decided that the reason for my bias is that the greatest Code Horrors I deal with now are badly written Packages and Stored Procs etc.
But THIS is the problem, not really the 'concepts' of stored procedures or packages. The same can be said about regular code and writing shared libraries and using objects.
Poorly written anything is junk.
You can’t blame the tool or the concept really.
Blame the user of the concept/tool = YES
Blame the reviewer of the designer = YES
Blame the reviewer of the implementation = YES
You can use the best tools on the best platform using the best agreed upon methodologies and still write code that is ugly.
|
|
|
|
|
Ray Cassick wrote: You can use the best tools on the best platform using the best agreed upon methodologies and still write code that is ugly.
|
|
|
|
|
I agree 100%, there is too many drawbacks on using adhock queries in the client application that most of the times prevents scaling an application. what if you have a table holding customer records, and you want to move the address columns to a new table to introduce multiple addresses for a customer.
how are you going to due this when you have a web app, thick client, analysis cubes, reporting that will all require change and how do you manage deploying this change out?
by preventing adhock or table direct queries all one would have to due is modify a few views and procs.
secondly how can you ensure that all the adhock queries in all applications that are consuming this database are all using indexes, common business logic and formulas. and how would you trace down a poorly written adhock query that is table scanning, how do you go about fixing it and deploying it without impacting other user applications.
my developers may only select from a view or call a proc from any application period....
look on the bright side, you have an opportunity to catalog all objects and send out impact statements to understand who is using what and also have an opportunity to find synergies between your development teams to come to a common process
|
|
|
|