haven't encountered anything that double checks the '=' under if statement... the closest flags with respect to our discussion, is the one I mentioned above, using /Zs.
My understanding about superfluous flags, or let me say unnecessary flags are appeared only at certain situation. eg. Compilers have default flags some of it are superfluous in Windows Operating Systems, BSD's, Linux and other distros and more...and We will disable/enable that manually.
anyway, It’s not difficult to examine compiler's flag, coz it is all categorized into different purpose like Optimization, Code Generation, Output Files, Linking, and etc...
Frankly, I discouraged the usage of ternary operators, there are times when you are a bit tired and exhausted and seeing this code makes you more confuse, determining which end is which. Aside from that, you cannot set some breakpoints, especially if you want to investigate only at specific clause.
The only help that ternary operator provide me is when I want conditional statement inside my WriteLine or cout function. the rest are all just plain if/else block.
if you discourage usage of ternary operators, why use it even in writeline??
like i said bro, these are not hard and fast rules.. ternary operators are also used outside of writeline or what-nots.. its up to you if you're comfortable with it or not..
however, personally, if a programmer is confused about ternary operator logic, then he's not really a programmer.. its expected na kahibalo na xa unsa gamit ana.. it makes for really compact code but still readable.. dre na ni mu-delineate sa real programmer or script-kiddie lng.. hehe.. so don't refrain from using ternary just because you fear that others might not be able to read your code properly, force them to learn the usage of ternary operators..
Writeline as invistagation purpose only... not in production code., when I want to output something and suddenly think of something conditionally, I code directly with ternary operator, instead of going back again placing if/else block, but this won't be shipped to production.
I'm not a real programmer I guess... coz it really confused me, going back after 2-3 months, feels debugging codes at hell, It makes a real compact code that you are not able to set some breakpoints.
Anway, If this is one of your reasons for you to differentiate real programmer or script-kiddie, I respect your opinion. At least I was able to share my truthful comments.
i apologize.. i don't mean any offense..
my logic would go around why ternary operators are even placed in standards.. for the record, i also had the same question as yours before.. libog man pag-una.. pero once i got used to it, its pretty much useful as one-line if-statements.. instead of putting curly braces, i convert my one-line if to ternary operators..
as for the unable to put breakpoints, its just one line of if, so, i don't see the need to rely too much on breakpoint and just evaluate the one line in my head..
besides, you can set a watch instead of breakpoints if you want to monitor evaluation of expressions..
Last edited by bluedes; 12-15-2009 at 02:23 PM.
In C# case:
"Watch" has limitations everybody knows that... and It doesn't help either way. Inorder for you to understand what I mean, try this:
Can you add watch on the return value? how about the line Math.Sin(x) ?
Can anyone distinguish what operation is this? And why there is no room for breakpoints?Code:return x != 0.0 ? Math.Sin(x) / x : 1.0;
In addition to that, here's some code where I do it with ternary operators.Code:byte x = (byte)(8 + ((myVar == 1) ? 4 : 2));
Obviously I already put some breakpoints/watches from the method I'd like to investigate. But this code does not belong to our production code; never would I allow anyone to use ternary operators in our modules.Code:MyClass.StaticMethod(Method1( ) ? Method2( ) : (Method3( ) ? Method4( ) : Method5( )));
In C++ case:
Of course there is no room for breakpoints/watches coz this is just a macro, but can anyone understand this right away? What’s the worst possible thing can happen here?Code:#define MyFunc(a, b) (((a)>(b)) ? (a) : (b))
There are still a lot more to discuss why ternary operators are ban in some corporate standards, but I’m not telling anyone here not to use it. What I explained here is just purely base in my experience, and from my previous posts![]()
Last edited by MarkCuering; 12-15-2009 at 03:55 PM.
yeah, as a matter of fact, you can evaluate the entire expression not including the return..
"x != 0.0 ? Math.Sin(x) / x : 1.0;"
or you can also put watches on individual members of the ternary operator, if you're curious for the evaluation of separate expressions:
"x != 0.0"
"Math.Sin(x) / x"
"1.0"
you can evaluate them just before you get to that return statement.. which is the previous line.. add a breakpoint there and just check ur watch expressions..
i don't know what limitation of watch you are talking about.. can u please specify?
that's your personal preference bro.. but i still don't see justification why you should ban ternary operators in some "corporate standards"..
again, ternary operators are a good substitute for one-liner ifs, that's why they are created in the standards.. but if your if statements comprises more than one-line, then ternary operators only make the code confusing.. especially if nested ifs or if-else-if..
a watch on: x != 0.0 ? Math.Sin(x) / x : 1.0
and: Math.Sin(x)
some sort of limitations or side effects...
the messages are:
1. This expression causes side effects and will not be evaluated as double
2. Math.Sin(x) The name 'x' does not exist in the current context.
I hope you figure it out why does it happened. The rest of those limitations is not relevant for this topic. like looking on float or double, type cast, virtual destructors. What IDE are you working on? So i could enumerate those limitations for you.
Any comments or solutions on the other issues I mentioned? like this one:
Code:byte x = (byte)(8 + ((myVar == 1) ? 4 : 2));
you mean as simple as this:Code:if (x!=y) doThis(); else doThat();
I was really thinking that you want to utilize the entire ternary operator base on my given examples… when all of my opinions are base from that…
sa imoha nang debugger problema bro.. coz wala jud problema sa ako dre..
what debugger are u using? i'm using GDB - GNU debugger..
i've tried those many times, evaluating even the most complicated expressions and multi-line expressions in my watch/expressions list, wala jud problema..![]()
Similar Threads |
|