|
The Laplace Demon posted:Yeah, really don't get why people scoff at that. You, the compiler and I all know it's equal to 1. Sometimes I put sizeof(char) to show the units are "bytes" or for symmetry with other statements. It doesn't make my code any less correct or efficient. If anything I'd prefer sizeof(char) to be in there for clarity in most cases. It's not like it's going to cost anything when compiled, and it helps someone casually reading/reviewing your code to understand what you're getting at (hell it might even help flag a bug). Also this: Subjunctive posted:Similarly spelling out 4 * 1024 * 1024 for 4MB, or 60 * 60 * 24 for seconds-in-a-day. Express the significance of something, not just the resulting value.
|
# ? Feb 11, 2015 14:00 |
|
|
# ? Jun 6, 2024 11:02 |
|
TZer0 posted:Anonymized code: That snippet is strange for other reasons like casting std::vector.size() to an int and apparently distrusting the result of std::vector.empty().
|
# ? Feb 11, 2015 15:26 |
|
I know a great feature for a STL vector class: prefill new instances with All you need to do is check whether your new vector is empty before using it, and prefill it yourself if the compiler didn't do it for you.
|
# ? Feb 11, 2015 15:49 |
|
Karate Bastard posted:I know a great feature for a STL vector class: prefill new instances with I would like all String instances to come pre-filled with the text of John Galt's speech from Atlas Shrugged, please.
|
# ? Feb 11, 2015 16:39 |
|
The Laplace Demon posted:Yeah, really don't get why people scoff at that. You, the compiler and I all know it's equal to 1. Sometimes I put sizeof(char) to show the units are "bytes" or for symmetry with other statements. It doesn't make my code any less correct or efficient. Voted Worst Mom posted:I think what he meant was using the literal 1 is a magic number in comparison to using sizeof(char) even if you know they are the same thing. I think some bits of context would be helpful: 1: the context of my statement, which is that it's generally preferable to use identifier expressions as operands to sizeof rather than types. 2: the context in which sizeof(char) most often appears (at least in my experience), which is usually not to manipulate single bytes. code:
If the intent is to have a specified number of bytes: code:
If the intent is to allocate enough for a certain number of elements, which happen to be char, but could maybe possibly in theory change at some point (perhaps to wchar_t to hold larger characters, or short or int to hold larger numbers, you probably want: code:
Steve French fucked around with this message at 17:13 on Feb 11, 2015 |
# ? Feb 11, 2015 17:11 |
|
I would write malloc(count * sizeof char) where I was going to use the resulting buffer as a char *, for symmetry with malloc(count * sizeof double) or similar. I wouldn't bounce something in code review for being less explicit, though.
|
# ? Feb 11, 2015 17:47 |
|
Steve French posted:If the intent is to allocate enough for a certain number of elements, which happen to be char, but could maybe possibly in theory change at some point (perhaps to wchar_t to hold larger characters, or short or int to hold larger numbers, you probably want: I don't do very much C (though I review a TON of it), but I DO tend to prefer the sizeof(char) when talking about a c string. The reason I prefer it is the potential bug in the code quoted above! If you write sizeof(char), it signifies you mean to use this as a cstring, and likely forgot a +1 to account for the null terminator. If you just do malloc(length), I have to deeper dive to determine whether you mean for s to point to a general byte array or a string.
|
# ? Feb 11, 2015 18:02 |
|
Voted Worst Mom posted:That snippet is strange for other reasons like casting std::vector.size() to an int tl;dr: This is mostly fine: it's avoiding a warning in a very slightly dumb way. 0 has type int (which is signed), and size() returns a size_t (which is unsigned). There's a very common warning, spelled -Wsign-compare in gcc and clang, which warns about comparing values of different signedness. That warning has an important exception when the operand that's changing signedness for the comparison is obviously non-negative; this is why -Wsign-compare doesn't warn about comparisons like someUnsignedNumber > 0. But in the gtest framework, ASSERT_EQ is a macro which invokes a template function, and that template function is where the comparison occurs, and it has no idea about the value of its arguments statically (in fact, the same template function will be shared for all invocations with an int and a size_t as operands, and some of those really might be problematic). So the extra abstraction causes this warning, and the test is avoiding it by making sure the template sees the same type on both sides. It would be slightly better to instead make the expected value unsigned instead of signed by writing it as 0U or casting it to size_t; this would avoid the (completely theoretical in this case, still basically unimaginable elsewhere) possibility that size() could return an exact multiple of 2^32 which, when truncated to int, would yield 0. But I can understand the instinct to add the syntactic burden to the more complex operand and leave the expected value a nice, clean literal.
|
# ? Feb 11, 2015 18:16 |
|
TheFreshmanWIT posted:I don't do very much C (though I review a TON of it), but I DO tend to prefer the sizeof(char) when talking about a c string. The reason I prefer it is the potential bug in the code quoted above! If you write sizeof(char), it signifies you mean to use this as a cstring, and likely forgot a +1 to account for the null terminator. If you just do malloc(length), I have to deeper dive to determine whether you mean for s to point to a general byte array or a string. I was not advocating malloc(length) in the case of a buffer meant to hold a string with a particular number of characters, but rather in the case of a buffer with a particular number of bytes. It will hopefully be clear in context whether s is meant to point to a string (perhaps a nearby call to a string function, or well-named variables). But let's assume for a moment that it's not: using sizeof(char) is a good indicator that you're not meaning to allocate a buffer with a particular number of bytes, but so is sizeof(*s), and neither makes it clear that you're allocating a buffer to hold a cstring rather than an array of 1-byte signed integers. Humor me for a moment, and show me exactly how you personally would allocate a cstring str to hold length characters?
|
# ? Feb 11, 2015 18:52 |
|
Using sizeof(char) is like splitting an infinitive. It's perfectly legitimate, and there are times when it makes your intention clearer, but you should keep in mind that a lot of people will judge you for it.
|
# ? Feb 11, 2015 21:26 |
|
I bet people are worried about the weirdo machines where a char is bigger than 8 bits, but they don't realize that sizeof(char) is required to return 1 no matter what.
|
# ? Feb 11, 2015 22:57 |
|
Kilson posted:I bet people are worried about the weirdo machines where a char is bigger than 8 bits, but they don't realize that sizeof(char) is required to return 1 no matter what. They need to check CHAR_BIT for weird architectures like that.
|
# ? Feb 11, 2015 23:32 |
|
This is one of mine from a year ago or so:C# code:
|
# ? Feb 12, 2015 00:49 |
|
One of mine, recently:code:
|
# ? Feb 12, 2015 04:44 |
|
Knyteguy posted:This is one of mine from a year ago or so: Forgive me for being an idiot, but what's the horror here? Is it just that you're setting the flag before you actually do something?
|
# ? Feb 12, 2015 08:48 |
|
What does the @ do there?
|
# ? Feb 12, 2015 14:21 |
|
Looks like Razor, so it just starts a C# (or VB.Net) code block.
|
# ? Feb 12, 2015 14:48 |
|
VostokProgram posted:Forgive me for being an idiot, but what's the horror here? Is it just that you're setting the flag before you actually do something? The real crime is comparing a boolean to false.
|
# ? Feb 12, 2015 15:21 |
But readability!!!
|
|
# ? Feb 12, 2015 16:46 |
|
Bognar posted:The real crime is comparing a boolean to false. I do it all the time... Because I'm forced to used languages without strong typing at work.
|
# ? Feb 12, 2015 16:55 |
|
the horror is doing business logic in your views
|
# ? Feb 12, 2015 17:07 |
|
There's arguments for comparing to false instead of using !, but yea there's definitely some horrors in there. The comment sucks and doesn't mean anything, the boolean variable name isn't descriptive at all (it could have been bool filtersAreSet or something), the view has business logic in it, and there's more I'm sure. The whole view could use some refactoring, because it's a mess. Luckily there's a bug in there (surprise) and I'll get the opportunity to do so soon. Also the controller that creates the view: https://nopcommerce.codeplex.com/So...ogController.cs Knyteguy fucked around with this message at 17:29 on Feb 12, 2015 |
# ? Feb 12, 2015 17:24 |
|
code:
|
# ? Feb 12, 2015 17:46 |
That's pretty good, I might have to steal it.
|
|
# ? Feb 13, 2015 00:44 |
|
Fix the spelling of "nickel" first
|
# ? Feb 13, 2015 00:52 |
|
GrumpyDoctor posted:Fix the spelling of "nickel" first
|
# ? Feb 13, 2015 01:32 |
|
Bognar posted:The real crime is comparing a boolean to false. I do a double-take in Swift every time I compare an optional boolean to true or false. It's necessary but it still feels weird.
|
# ? Feb 13, 2015 03:23 |
|
Spatial posted:
|
# ? Feb 13, 2015 04:57 |
|
quote:[The] O(n) is log n E: code:
Also, still a bug by reallocating the struct instead of the backing array, but I have to give full credit for that because the instructor's solutions make the same error. And his loop to shift the elements doesn't do what he thinks it does. To be slightly fair, this is a midterm so it's effectively whiteboard coding. Linear Zoetrope fucked around with this message at 20:13 on Feb 14, 2015 |
# ? Feb 14, 2015 20:00 |
|
Jsor posted:whiteboard coding. Any of you whippersnappers actually got to do this?
|
# ? Feb 15, 2015 00:28 |
|
Karate Bastard posted:Any of you whippersnappers actually got to do this? It takes practice like anything else, but there's nothing fundamentally hard about it.
|
# ? Feb 15, 2015 02:53 |
|
KernelSlanders posted:It takes practice like anything else, but there's nothing fundamentally hard about it. Specifically practice choosing an appropriate size so that your code doesn't look like the board got into a head-on collision with something.
|
# ? Feb 15, 2015 04:15 |
|
var hypotenuse = sqrt(opposite * opposite + adjacent * adjacent)
|
# ? Feb 15, 2015 04:40 |
|
The most important part of software engineering is designing the crumple zones.
|
# ? Feb 15, 2015 04:55 |
|
Jsor posted:The most important part of software engineering is designing the crumple zones. Now you see the hidden genius of Java.
|
# ? Feb 15, 2015 04:56 |
|
Steve French posted:I was not advocating malloc(length) in the case of a buffer meant to hold a string with a particular number of characters, but rather in the case of a buffer with a particular number of bytes. It will hopefully be clear in context whether s is meant to point to a string (perhaps a nearby call to a string function, or well-named variables). But let's assume for a moment that it's not: using sizeof(char) is a good indicator that you're not meaning to allocate a buffer with a particular number of bytes, but so is sizeof(*s), and neither makes it clear that you're allocating a buffer to hold a cstring rather than an array of 1-byte signed integers. I tend to stick to C++ these days, but I do a bunch of code reviews on it. This is the pattern that makes reviewing it most clearly: code:
|
# ? Feb 15, 2015 05:30 |
|
Karate Bastard posted:Any of you whippersnappers actually got to do this? Yes, and I stressed out so hard about it that I all but forgot how to talk for the soft questions.
|
# ? Feb 15, 2015 07:40 |
|
TheFreshmanWIT posted:I tend to stick to C++ these days, but I do a bunch of code reviews on it. This is the pattern that makes reviewing it most clearly: If only programming languages had some way to assign names to patterns like this, so you could have something like str = malloc_str(length) and it would be explicit and simple and you wouldn't have to worry about using redundant multiplications by obfuscated constants to convey your intent
|
# ? Feb 15, 2015 14:04 |
|
I dunno, Soricidus- that sounds like overhead talk. My PDP-7 only has 4 kilowords of memory to work with and an indulgent 10 character identifier will take quite a toll on the symbol table.
|
# ? Feb 15, 2015 14:52 |
|
|
# ? Jun 6, 2024 11:02 |
|
Internet Janitor posted:I dunno, Soricidus- that sounds like overhead talk. My PDP-7 only has 4 kilowords of memory to work with and an indulgent 10 character identifier will take quite a toll on the symbol table.
|
# ? Feb 15, 2015 17:09 |