Click here to Skip to main content
15,890,185 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I hate recent C# versions! PinPopular
Richard Deeming6-Sep-22 1:18
mveRichard Deeming6-Sep-22 1:18 
GeneralRe: I hate recent C# versions! Pin
Kate-X2576-Sep-22 22:08
Kate-X2576-Sep-22 22:08 
GeneralRe: I hate recent C# versions! Pin
Mateusz Jakub6-Sep-22 23:47
Mateusz Jakub6-Sep-22 23:47 
GeneralRe: I hate recent C# versions! Pin
Chris Maunder7-Sep-22 3:14
cofounderChris Maunder7-Sep-22 3:14 
GeneralRe: I hate recent C# versions! Pin
Richard Deeming7-Sep-22 3:36
mveRichard Deeming7-Sep-22 3:36 
GeneralRe: I hate recent C# versions! Pin
Chris Maunder7-Sep-22 4:20
cofounderChris Maunder7-Sep-22 4:20 
GeneralRe: I hate recent C# versions! Pin
William Rummler7-Sep-22 5:40
William Rummler7-Sep-22 5:40 
GeneralRe: I hate recent C# versions! Pin
Philippe Verdy8-Sep-22 12:01
Philippe Verdy8-Sep-22 12:01 
The purpose of the "is" contruct with multiple conditions is to avoid repeating the first operand (imagine it is a function or method, or attribute whose evaluation or dereference would have a side effect).
Here it allows writing it, without having to declare and initialize the value of an additional local temporary variable (that temporary variable or register will be allocated/defined intrisicly by the compiler, and only where it needs it). You get a shorter and more readable condition with "is" than with such additional explicit declaration.

Then when testing "c is" with constants or ranges, this becomes very clear, and the compiler can choose how to represent the ranges or individual values (notably when they are constant) and generate the best code (yes it means that it can then optimize "c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z')" itself into "(uint)((c | 0x20) - 'a') <= 'z' - 'a'", but without using ugly expressions with unsafe/unchecked typecasts.

So in my opinion this short notation is a good idea, and it remains very readable while being safe (and allowing easier optimization and inlining if needed by the compiler).

Doing the same thing in C++ is a real nightmare (you have to play with very ugly "move" semantics that are very hard to debug when there are also multiple overriden candidates for operators/convertors/constructors and so many implicit typecasts to play with). Doing that in C is simpler, but hard to manage as the compiler has no knowledge or control of the semantics, so it cannot make so many optimizations: you need to use explicit local "register" declarations, put multiple statements within braces, possibly surrounded by the "do{}while(0)" trick when using ugly macros... And still the C compiler is unable to perform optimizations you'd expect (and there's no warranty that it works when you also have to play with "volatile" semantics), plus the effect of "restricted" state concurrency used by modern processors using multiple cores and modern architectures using caches over shared buses/networks, where loads/stores may be reordered.

Cosidering all this, the "is" construct is very safe and the compiler will generate the correct code while preserving all the needed semantics, and without pollutiong the source code with ugly temp varaibles and extra local scopes.
GeneralRe: I hate recent C# versions! PinPopular
Kornfeld Eliyahu Peter6-Sep-22 1:26
professionalKornfeld Eliyahu Peter6-Sep-22 1:26 
GeneralRe: I hate recent C# versions! Pin
Gerry Schmitz6-Sep-22 5:47
mveGerry Schmitz6-Sep-22 5:47 
GeneralRe: I hate recent C# versions! PinPopular
BryanFazekas7-Sep-22 1:10
BryanFazekas7-Sep-22 1:10 
GeneralRe: I hate recent C# versions! PinPopular
OriginalGriff6-Sep-22 1:39
mveOriginalGriff6-Sep-22 1:39 
GeneralRe: I hate recent C# versions! Pin
PIEBALDconsult6-Sep-22 4:54
mvePIEBALDconsult6-Sep-22 4:54 
GeneralRe: I hate recent C# versions! Pin
harold aptroot6-Sep-22 5:04
harold aptroot6-Sep-22 5:04 
GeneralRe: I hate recent C# versions! Pin
PIEBALDconsult6-Sep-22 5:07
mvePIEBALDconsult6-Sep-22 5:07 
GeneralRe: I hate recent C# versions! Pin
harold aptroot6-Sep-22 5:10
harold aptroot6-Sep-22 5:10 
GeneralRe: I hate recent C# versions! Pin
PIEBALDconsult6-Sep-22 5:26
mvePIEBALDconsult6-Sep-22 5:26 
GeneralRe: I hate recent C# versions! Pin
harold aptroot6-Sep-22 5:38
harold aptroot6-Sep-22 5:38 
GeneralRe: I hate recent C# versions! Pin
PIEBALDconsult6-Sep-22 5:51
mvePIEBALDconsult6-Sep-22 5:51 
GeneralRe: I hate recent C# versions! Pin
NiL^6-Sep-22 21:43
NiL^6-Sep-22 21:43 
GeneralRe: I hate recent C# versions! Pin
PIEBALDconsult7-Sep-22 2:31
mvePIEBALDconsult7-Sep-22 2:31 
GeneralRe: I hate recent C# versions! Pin
NiL^7-Sep-22 4:30
NiL^7-Sep-22 4:30 
GeneralRe: I hate recent C# versions! Pin
PIEBALDconsult7-Sep-22 6:55
mvePIEBALDconsult7-Sep-22 6:55 
GeneralRe: I hate recent C# versions! Pin
OriginalGriff6-Sep-22 6:16
mveOriginalGriff6-Sep-22 6:16 
GeneralRe: I hate recent C# versions! Pin
PIEBALDconsult6-Sep-22 10:12
mvePIEBALDconsult6-Sep-22 10:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.