Having an asterisk both be the type indicator and the dereference operator is one of the great programming language design blunders of our time, along with allowing nulls for any type in so many languages.
I also sometimes wish that the syntax in
if
statements was inverted, where()
was optional and{}
was required.Rust makes this choice and it is way better.
Can you give me an example? I’m not sure I follow. Might be language specific?
The code in the image is C or C++ or similar. In those languages and languages derived from them, curly braces are optional but the parentheses are required. It should be the other way around to avoid logic errors like this:
if (some expression) doSomething() else if (some other expression) printf(“some debugging code that’s only here temporarily”); doSomethingElse();
Based on the indentation you’d think that
doSomethingElse
was only meant to run if theelse if
condition was true, but because of the lack of braces and theprintf
it actually happens regardless of either of theif
conditions. This can sometimes lead to logic errors and it doesn’t hold up to a principle of durability under edit — that is, inserting some code into theif
statement changes the outcome entirely because it changes the code path entirely, so the code is in a sense fragile to edits. If the curly braces were required instead of optional, this wouldn’t happen.I have all of my linters set up to flag a lack of curly braces in these languages as an error because of this. It’s a topic that sometimes causes some debate, ‘cause some people will vociferously defend their right to not have the braces there for one liners and more compact code, but I have found that in general having them be required consistently has led to fewer issues than having arguments about their absence, but to each their own. I know many big projects that have the opposite stance or have other guidelines, but I just make ‘em required on my own projects or projects that I’m in charge of and be done with it.
if(condition) statement; Is valid in typical C-style syntax.
if condition { … }
Is invalid in typical C-style syntax
Having assignments return a value is right up there as well.
Because of the possibility of accidentally performing an assignment in a conditional expression?
If yes, I agree that it’s not great.
There is an other.
int * p;
Here is an otter
:)
i am in this picture and i do like it
The fact it’s a pointer is part of the type, not part of the variable name. So
int* p
is the way.You would think so, but
int* a, b
is actually eqivalent toint* a; int b
, so the asterisk actually does go with the name. Writingint* a, *b
is inconsistent, soint *a, *b
is the way to go.Yeah, and I’d say that’s a design flaw of the language as it is unintuitive behaviour.
While technically true, that’s also one of the worst ‘features’ of the language and I personally consider it a bug in the language. Use two lines and make it clear and correct.
Don’t declare more than 1 pointer per line. This resolves that, badly.
When people say “pointers are hard”, they mean “I have no idea where the star goes and now an ampersand is also implicated”.
That’s the part where you give up and randomly shove/unshove symbols in until the code works.
I’ve definitely never been guilty of this. /s
Alright, I’ll never, ever write something this way now. Good to know.
int* i, j
The C syntax is just messed up.
It’s part of the type yet it’s also a unique identifier. That’s the whole thing with east or west
const
.const int *
is aimmutablemutable pointer that points tomutableimmutable memory.int *const
is amutableimmutable pointer that points toimmutablememory.int const *
is the same type as the first example, aimmutablemutable pointer that points tomutableimmutable memory.Same stuff applies to references which makes it easier to think of the variable owning the
*
or&
as if you want that pointer or reference to beconst
it has to go after.Edit:I am a moron who managed to get it exactly backwards :|
Found the guy that can probably do function pointers!
I wrote a couple unholy lines of C++ the other day using the ternary conditional operator to select a class member function to be called with a fixed argument.
I think my teammates were too scared to call me out on it.
Wait until I tell y’all about const.
Int *p is unreadable, unreasonable, and bad programming.
Why so? It’s what it actually is
But C syntax clearly hints to
int *p
being the expected format.Otherwise you would only need to do
int* p, q
to declare two pointers… however doing that only declaresp
as pointer. You are actually required to type*
in front of each variable name intended to hold a pointer in the declaration:int *p, *q;
That’s because C was designed by a fool.
The fools who created Unix
And C!
What, could you have done better in 70-whatever?
std::shared_ptr<int> p;
I’m just a c# dev wishing to fuck we had something visual to indicate reference types so my coworkers could stop misusing them
oh thank you! I use jetbrains but I wonder if I can implement the same thing