General > General Technical Chat
How many people code in C these days, and if so, why?
IDEngineer:
And despite how I typed my examples above (which I did for clarity), I actually always type conditionals which contain a fixed value like this:
if (true == boolean)
if (5.3 == fVar)
Why? Because if I accidentally leave out one of the equal signs, the compiler will conveniently report it as an error. Typed the "traditional" way, the compiler will simply (and silently) execute the assignment and then resolve the conditional based on the zero vs. nonzero (false vs. true) numeric value of the assignment. This works in Java and other languages too. It "reads" a bit weird but is perfectly logical and lets the compiler check my work in an extra way for free.
I teach others to type this way and (especially for newbies who haven't internalized the whole "double equal sign" thing) it catches a LOT of mistakes.
Siwastaja:
--- Quote from: nctnico on May 10, 2020, 02:57:34 pm ---The problem is that in C 'if' can be used with any type.
--- End quote ---
IMHO, you should have a naming convention (explicit, or more subtle) so that you know the types, because the problem isn't limited to the if clause and comparisons.
Once you fix that, you get rid of needing the comparisons to other values just to demonstrate the type!
if(isDog) // any isSomething is bool
printf("%d", numDogs); // num anything is int
or explicitly,
if(b_dog_exists)
printf("%d", i32_n_dogs);
whatever your coding style or naming convention is, I don't think writing excess comparisons just to "demonstrate" the type is a good solution. This information should be available in some other way.
Siwastaja:
--- Quote from: SiliconWizard on May 10, 2020, 03:17:28 pm ---But it's not particularly ugly. Some people may find it quirky, but that's largely questionable.
--- End quote ---
I agree, there's nothing ugly. We don't need to hide all low-level details in near-neurotic way; only abstract things when there is a clear benefit and good reason. Just use a simple comment to document dual-use as a flag and counter (I do that all the time, not only for performance, but because it makes sense.)
Similarly, I accept, and even prefer, to have my plumbing visible. It does not distract me, it has a purpose, it's needed, I use it, and when visible, I can see if it leaks. Some people prefer to hide plumbing inside concrete though because it's "ugly". Then replacing it is a huge project (it's known here as a personal tragedy, called "plumbing renovation", requiring evacuation for half a year and almost doubling the estate price.)
SiliconWizard:
--- Quote from: IDEngineer on May 10, 2020, 03:53:19 pm ---And despite how I typed my examples above (which I did for clarity), I actually always type conditionals which contain a fixed value like this:
if (true == boolean)
if (5.3 == fVar)
Why? Because if I accidentally leave out one of the equal signs, the compiler will conveniently report it as an error. Typed the "traditional" way, the compiler will simply (and silently) execute the assignment and then resolve the conditional based on the zero vs. nonzero (false vs. true) numeric value of the assignment. This works in Java and other languages too. It "reads" a bit weird but is perfectly logical and lets the compiler check my work in an extra way for free.
I teach others to type this way and (especially for newbies who haven't internalized the whole "double equal sign" thing) it catches a LOT of mistakes.
--- End quote ---
Yes, I don't like this style, but it's a pretty common one to avoid errors.
Note that many decent/recent compilers will issue a warning if you write 'if (a = 5)' (and you shouldn't ignore warnings!), and otherwise static analyzers will also spot that. But if you don't use any, then yes I agree.
SiliconWizard:
--- Quote from: Siwastaja on May 10, 2020, 04:58:05 pm ---
--- Quote from: SiliconWizard on May 10, 2020, 03:17:28 pm ---But it's not particularly ugly. Some people may find it quirky, but that's largely questionable.
--- End quote ---
I agree, there's nothing ugly. We don't need to hide all low-level details in near-neurotic way; only abstract things when there is a clear benefit and good reason. Just use a simple comment to document dual-use as a flag and counter (I do that all the time, not only for performance, but because it makes sense.)
--- End quote ---
Agreed, but I don't quite agree with the low-level nature of this particular point.
If anything, I consider the two states of original C "booleans", zero and non-zero, to be a little more abstract than just 0 and 1. Weirdly enough, many people seem to have a problem with that. Guess we're all different.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version