[C#] Is this incrementation ternary method sensible?

Started by
80 comments, last by _the_phantom_ 14 years, 2 months ago
Quote:Original post by phantom
Depends on your metric of 'better'; if I'm scanning code and debugging then all I need to know is that I got into that block because 'num' is greater than 5. I don't care HOW it got greater than 5 just that it is.

When scanning code you process a line at a time, the lower the information on that single line the faster you can process.

By introducing the '++n' into it you are forcing the reader to slow down and de-mangle the line in order to understand what is going on. It's needless noise in the line which would be better pulled out for clarity and easier refactoring later if you got beyond needing a trival '++' operation.


That certainly does make sense; debugging is much easier with simple lines.
Advertisement
Quote:Original post by Antheus
This is C test, focusing on embedded development, it is likely that mainstream popular languages would be skewed slightly to the left, but at the end of the day, it's not all bad.


Wow. Mean score of 61%? I guess there really are a lot of idiots out there o_O

I took the C Quiz and got 90% (didn't know that all compilers supported const volatile)... I don't even use C [grin]

Ok, I can see why you would want to simplify the statement. Still, though - given a tight group of good, experienced programmers, if (++num > 5) would be in no way ambiguous.
Quote:Original post by Antheus
Also - all the correct proposals in this thread describe idempotent changes to source code (aka they don't affect the result).

That's not quite true. The original code contains a subtle, unlikely-to-matter-in-real-life bug. Let's assume for a moment that the types of all the variables are int for the sake of a discussion. If frame_Finish happens to be equal to int.MaxValue, and frame_Start is not equal to int.MinValue, then the resulting value for frame_Index won't loop properly back to frame_Start when it hits the end of the frames. My version doesn't preserve this bug.
Quote:Original post by SiCrane
That's not quite true. The original code contains a subtle, unlikely-to-matter-in-real-life bug. Let's assume for a moment that the types of all the variables are int for the sake of a discussion. If frame_Finish happens to be equal to int.MaxValue, and frame_Start is not equal to int.MinValue, then the resulting value for frame_Index won't loop properly back to frame_Start when it hits the end of the frames. My version doesn't preserve this bug.


Hence the unit test.

In this case, the bug is obscure integer overflow. Perhaps that is the intended behavior. perhaps MaxValue is used as a special marker that allows to start playing animation from the middle, but then loops across all frames. Or something.

Readability doesn't help if one doesn't know the problem they are solving.

There are many real world scenarios where broken behavior becomes defacto standard, especially if end-users begin to rely on it.
Quote:
Wait what? I'm not sure what point you're trying to make? No offense or anything but what you just said sounds incredibly stupid.


I was perhaps not clear... Basically, I know a ton of stuff about C# as far as the language and its specification goes, but what rip-off says also applies to me:

Quote:Original post by rip-off
I have no idea if || or && has a higher precedence. I don't care. I've never seen anyone carelessly write code that depends on the priority at work. If I ever do I'll look it up and change the code to make it clearer. Call me stupid if you like [smile]



Quote:
(regarding assignment within the if)
No? That's a horrible example, that expression needs parenthesis without second thought, comparison comes before assignment.


And that's my point. In depth knowledge of operator precedence for that specific language is needed to maintain the code effectively. You've made the code more fragile for no benefit.

Quote:
(regarding un-braced if statement fallthrough)
That's an even worse example! It practically kills my eyes when I see something like that .


And it looks perfectly fine to python users, or the myriad of functional languages that allow indentation to denote the block for a conditional... even the compiler won't complain when you force the indentation over. It just won't work right. Again, slightly more fragile code for no good benefit.
Quote:
And that's my point. In depth knowledge of operator precedence for that specific language is needed to maintain the code effectively.

That's not in-depth at all. At all. It's completely basic. Comparison before assignment. Your examples resemble what someone in their first quarter of AP Computer Science in High School might write. None of us fit in that group.

Quote:
And it looks perfectly fine to python users, or the myriad of functional languages that allow indentation to denote the block for a conditional...

So what? If you're writing C++/C#/Java/etc. then you're not writing Python. As simple as that.

That's like saying "well an upside-down question mark looks perfectly fine to Spanish readers" when we're talking about English.
Quote:even the compiler won't complain when you force the indentation over. It just won't work right. Again, slightly more fragile code for no good benefit.

I don't even understand how that example relates to my original statement? I'm not pro-complex-code - like I said multiple times, breaking down complex statements is highly beneficial. I'm just going against your superficial argument that if (++num > 5) is somehow overly complex.
Quote:Original post by nullsquared
Quote:
And that's my point. In depth knowledge of operator precedence for that specific language is needed to maintain the code effectively.

That's not in-depth at all. At all. It's completely basic. Comparison before assignment. Your examples resemble what someone in their first quarter of AP Computer Science in High School might write. None of us fit in that group.


None of us, no. Most of the people I've interviewed for programming positions (pretty much all of them with 3-6 years of professional experience)? Yes.

Quote:
Quote:
And it looks perfectly fine to python users, or the myriad of functional languages that allow indentation to denote the block for a conditional...

So what? If you're writing C++/C#/Java/etc. then you're not writing Python. As simple as that.


It's not as simple as that. It's not common for a programmer to only use a single programming language during their work, forget any personal projects. Sure, that context switch is usually swift and easy, but programmers are human and it's an easy thing to slip up on.

Quote:
I'm just going against your superficial argument that if (++num > 5) is somehow overly complex.


It's overly complex because it's needlessly complex. You're adding fragility (for the reasons I said before) and harming readability (using phantom's argument) for no practical benefit.
Quote:Original post by nullsquared
I'm just going against your superficial argument that if (++num > 5) is somehow overly complex.


Of course it is, it requires so much knowledge... Here's a better version:
switch (currframe) {  case 0 : currframe = 1; break;  case 1 : currframe = 2; break;  case 2 : currframe = 3; break;  case 3 : currframe = 4; break;  case 4 : currframe = 5; break;  case 5 : currframe = 0; break;}


Plus, it is very easy to extend. And if you need different kind of increment, copy paste the above, and add extra cases. This is much better for reliability, since old version is not modified, so no bugs are introduced.

Then ask the architect to wrap all of the above into a nice FrameIncrementationServiceBeanFactory, and you're done.


PS: I can't remember if I mean the above as a joke, or I'm reliving a debate I once had where I was proven that switch version really is better. I suppose there is a reason why my memory is so vague.
Antheus, I should have known! [wow] My bad.
Quote:
The thing about operator precedence is, it is easy to see what precedence the compiler will apply, but what the code does not show is the precedence the programmer intended.

I make a point of using parantheses for all non-trivial expressions so that my intent is clear.


If you're worried about someone reading your code and missing the intent of you code, wouldn't it be less effort to just add some comments specifying your intent? Hell, I generally do that anyway. Or do these "terrible programmers" who might read your code not read comments? [grin]

This topic is closed to new replies.

Advertisement