Tag Info

New answers tagged

-2

Consistent style is important for code maintainability. Not doing the wrong thing is important for code correctness. Here are two suggestions as to how you can achieve what you want without alienating other team members: Have the appropriate tidy tool as a checkin hook Have the appropriate lint tool as part of the build toolchain. This way nobody's ...


1

Besides the excellent points made by Sparky (items 1 and 3) and Donscarletti, this post brings me to another one I answered a long time ago: How do I document my code?. Lots of people are or call themselves programmers, some are good, not many are excellent. Just like in many other walks of life. You can decide to judge those who appear less good than you ...


2

If I were presented code like this in a code review, I'd have two questions to pose: Why did we elect to write it this way? Since bit manipulation like this is used to circumvent some sort of performance bottleneck, one would presume that we have a bottleneck that is rectified if we employ this approach instead. An immediate follow-up question would be, ...


2

Replacing if-s with arithmetic/logic expression is sometimes necessary where long processor piping is required. That makes the code to always run the same instructions whatever the condition, making it more suitable for parallelization. That said, the provided sample is wrong, since the two samples are not equivalent: if (b) return 42; else return 7; ...


2

Something like the following would go a way toward making the INTENT of the code more apparent: manifoldPressureFloor = (b * 42) | (~(b - 1) * 7); return manifoldPressureFloor; manifoldPressureFloor is totally made up, of course I have no clue what the original code is actually about. But without some kind of explanation or justification for the ...


4

Odds are, someone writing (b * 42) | (~(b - 1) * 7) is either someone who knows very little about programming trying to pretend to be experienced/knowledgeable/etc, or is someone trying to sabotage a project (i.e. they're too experienced/knowledgeable/intelligent and want job security). The first type of person wants to show that they know how to use NOT, ...


37

The second code does not return 42 or 7. for b = 1: (1 * 42) | (~(1 - 1) * 7) 42 | (~(0) * 7) 42 | (-1 * 7) 42 | -7 -5 for b = 0: (0 * 42) | (~(0 - 1) * 7) 0 | (~(-1) * 7) 0 | (0 * 7) 0 | 0 0 And yet when you posted it, you thought it did, which is exactly why you should avoid convoluted code. However, take some "correct" code ...


3

If the branches in the if/then/else are a problem, then it's probably easiest to just switch to something like: static const int values[] = {6, 42}; return values[b!=0]; This actually works and although some may find it marginally less readable than the if/then/else, it certainly shouldn't be a noticeable obstruction to anybody who knows C or C++ at all. ...


82

There are several questions that you raise. 1) Is this a clear sign that the coder is not cut out for professional programming? No. Developers often go through stages where they learn about an idea and want to apply it. Do they always apply these ideas efficiently and/or effectively. No. Mistakes are made, and it is part of the learning process. If ...


103

The first rule of any professional software engineer is to write code that is comprehensible. The second example looks like an optimized example for an older, non-optimizing compiler or just someone who happens to want to express themselves with bitwise operators. It's pretty clear what's going on if we are familiar with bitwise operations but unless you're ...


-2

I think your understanding of the code is correct (or at least it maches mine). Print a character at a time, unless the last character was white space. But I think both versions suffer from not being clearly stated. I would prefer the below, which is shorter and more explicit. #include <stdio.h> main() { int c, lastC; while ((c = getchar()) != ...



Top 50 recent answers are included