Click here to Skip to main content
14,360,959 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: do any of you others have little coding mantras that save your behind? Pin
Rick York12-Sep-19 5:50
mveRick York12-Sep-19 5:50 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
AnotherKen12-Sep-19 5:19
professionalAnotherKen12-Sep-19 5:19 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
honey the codewitch12-Sep-19 5:20
memberhoney the codewitch12-Sep-19 5:20 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
Member 798912212-Sep-19 5:20
memberMember 798912212-Sep-19 5:20 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
honey the codewitch12-Sep-19 5:23
memberhoney the codewitch12-Sep-19 5:23 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
Member 798912212-Sep-19 7:15
memberMember 798912212-Sep-19 7:15 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
honey the codewitch12-Sep-19 8:17
memberhoney the codewitch12-Sep-19 8:17 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
Member 798912212-Sep-19 10:37
memberMember 798912212-Sep-19 10:37 
Casting is not something you do very often - unless you continue to think of enums as just names for ints, so you continue to mix the two types up. So it shouldn't be essential (or even noticable) to system performance.

In many cases, the compiler can suppress the runtime check, e.g. for int literals, or when a simple flow analysis reveals that an int variable couldn't possibly be outside the enum range (or most certainly would be outside, in which case the compiler should barf).

For enum-to-int casts, there should be very little need for runtime checks - very few systems define more than 32K values for one enum type, and very little code nowadays use ints of less than 16 bits. Especially: In contexts where 8 bit ints are relevant, you very rarely see huge enum definitions with more than 128 alternatives (or 256 for uint8).

If you declare enums by forcing the internal representation to be given by the bit pattern of some int value, then you show that you do not recognize enums as a distinct type. Forcing the internal representation is as bad for enums as it would be to force a pointer or float to a specific bit pattern given by the representation of a specific integer literal. You shouldn't do that. Even assuming that enum values form a dense sequence from 0000 and upwards is on the edge - they are not ints, and you cannot assume any given similarity in int and enum implementation. Really, int/enum casts are as meaningless as int/pointer casts; we have casts only because lots of C programmers can't stop thinking of them as "just a little different ints".

Even for ints, the compiler should generate code that verifies that e.g. an int32 cast to an int16 is within the int16 range. Maybe the instruction set provides some harware support, creating an interrupt if not. Support may be available even for enum use: The last machine I programmed in assembly had a four operand instruction "LoadIndex register, value, min, max": If "value" was not in the range from min to max, an "illegal index" interrupt was generated. The Pascal compiler used this instruction for int-to-enum casts, specifying the first and last permitted enum value. (In Pascal, it is not given that "min" is zero; e.g. if an array is indexed from may to september.) I haven't spent time on learning the .net "instruction set", and don't know if it has something similar. But since it does index checks, I'd exepect it to.
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
honey the codewitch12-Sep-19 11:58
memberhoney the codewitch12-Sep-19 11:58 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
Member 798912212-Sep-19 14:37
memberMember 798912212-Sep-19 14:37 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
honey the codewitch12-Sep-19 14:40
memberhoney the codewitch12-Sep-19 14:40 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
Alister Morton12-Sep-19 5:42
memberAlister Morton12-Sep-19 5:42 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
TrinityRaven12-Sep-19 5:47
memberTrinityRaven12-Sep-19 5:47 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
honey the codewitch12-Sep-19 5:52
memberhoney the codewitch12-Sep-19 5:52 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
TrinityRaven12-Sep-19 6:23
memberTrinityRaven12-Sep-19 6:23 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
honey the codewitch12-Sep-19 6:54
memberhoney the codewitch12-Sep-19 6:54 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
Member 798912212-Sep-19 7:50
memberMember 798912212-Sep-19 7:50 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
TrinityRaven12-Sep-19 8:46
memberTrinityRaven12-Sep-19 8:46 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
Member 798912212-Sep-19 11:13
memberMember 798912212-Sep-19 11:13 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
Bruce Greene12-Sep-19 5:51
memberBruce Greene12-Sep-19 5:51 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
Ira Greenstein12-Sep-19 6:07
memberIra Greenstein12-Sep-19 6:07 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
Member 798912212-Sep-19 7:52
memberMember 798912212-Sep-19 7:52 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
Ira Greenstein12-Sep-19 9:11
memberIra Greenstein12-Sep-19 9:11 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
Roger House12-Sep-19 9:05
memberRoger House12-Sep-19 9:05 
GeneralRe: do any of you others have little coding mantras that save your behind? Pin
honey the codewitch12-Sep-19 9:07
memberhoney the codewitch12-Sep-19 9:07 

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.