Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
12bitfloat939420dNot wrong lol
I mean most of these are so unnecessarily hard to type for zero benefit -
typosaurus1218720dThat's haskell style? Saw it in php once. I did use allman style for a while. Now K&R. I use an rc file for formatter for all projects. It's quite basic with custom line length
-
donkulator298120dNot convinced about K&R either.
The opening brace isn't part of the while (...).
The block with its opening and closing braces is one thing, and the while (...) is another thing.
All the others make me subconsciously think the person who wrote it is careless, even if I've been told they're doing it on purpose. -
cafecortado776120dWhen I was a noob I used the Ratliff style, because it made more sense to me. But then I realised I was probably the only person on earth using it, so had to back down to K&R.
-
12bitfloat939420d@Lensflare The semicolons are a monoid in the category of endofunctors, you wouldn't get it
-
Demolishun3467920dThe mental disorder is all of these is the fucking whitespace in between elements!
while(condition < 1){ // good
while (condition < 1) { // mental disorder! -
typosaurus1218720d@Demolishun I've no idea from my head which one I use. Gets autoformatted anyway
-
Demolishun3467920d@retoor I think the most bizarre one I have seen is this:
while ( condition < 1 ) { // just why?
One of my pet peeves is this:
int *var;
The datatype is int*, not int. It should be:
int* var;
But my autoformatters all want to do the first one. I know its adjustable, but it really isn't that important. -
typosaurus1218720d@Demolishun before I started c I thought that there was a difference based on position of the asterisk. Spaces around expression like ( <expr> ) is insane indeed. I think my clang formatter does it your preferred way regarding asterisk usage btw. Myself did it in the middle. int * var
-
12bitfloat939420d@Demolishun I'm in your camp too, but I think int *foo; is actually more correct
As far as I know, C and C++ consider foo a pointer variable of type int, instead of a variable of type int pointer
There are only a few cases where the distinction shows itself in syntax though (can't remember them) -
12bitfloat939420d@Demolishun Ah right now I remember, here's an example: `int* a, b;` only declares the first variable as a pointer, the second is just an int
https://godbolt.org/z/vdvW41cnh
That has tripped me up before lol -
Demolishun3467920d@12bitfloat oh that is just stupid. Okay, the language is retarded. Is this why I avoid putting variables on same line? Maybe.
-
Lensflare1727020d@Demolishun I don‘t want to brag or something but how is it possible that I knew about this thing in C++ and you didn‘t?
I think your main language is C++? 😂 -
12bitfloat939420d@Lensflare I don't think you can ever be a point where you can't learn something new about c++ lol
-
typosaurus1218720d@Demolishun I also do every line, never inline
So,you have to declare int *a, *b instead of int *a, b? Damn, keep learning -
Lensflare1727020d@retoor there is a reason why none of the languages who were inspired by and came after C/C++, haven’t adopted this multiple-declarations-in-one-line syntax. :)
But I wouldn’t be surprised if something like php had this. -
Demolishun3467920d@Lensflare I dunno, maybe I just don't care. I don't put multiple variables on a line.
-
Lensflare1727020d@Demolishun I just realized that I might have learned it when I was doing code golf 😂.
I‘d never use this in normal code either. -
Demolishun3467920d@Lensflare one thing I found about C++. There is so much shit you don't know until you have a wtf moment. This causes you to dig into the language to search for the reason. When I found that there is no order for compilation units to execute constructors in any specific order I was kind of shocked. (this is code that executes before main) But I had to dig into cppreference to find that detail. Even after I explained this to people I work with they just tried blaming the OS itself. It has nothing to do with the OS at all. lol
-
Demolishun3467920d@Lensflare in before ostream:
"how is it possible that I knew about this thing in C++ and you didn‘t?"
Something, something German master race... ;-) -
devRancid65020dYou can count the Allman style as a mental disorder as well
Along with tab indentations -
CoreFusionX344620d@Demolishun
Constructors only run before main for global or static objects, and their order of execution is order of statements.
Did you mean static constructors? -
Lensflare1727020d@Demolishun this "no order for compilation units" kind of makes sense for me intuitively 😂.
Maybe I‘m more suited to be a c++ coder than I thought 😅
I fucking hate c++. But I also love it! -
Demolishun3467920d@CoreFusionX different compilation units. For simplicity: files like .cpp. Have no defined order in which they run. Sure within the unit it is defined. But the order in which each unit is run is not and can change depending upon the compiler and possibly even compiler settings.
We ran into this issue because we had one file defining an array of structs. Different compilation units would modify this struct at defined indexes. This is fine, but there was a constructor that zeroed out the array in one unit. This caused the structure to be wiped out if another compilation unit updated the array before the constructor ran.
I also found that globals are initialized by the OS. So it was unnecessary to have the constructor zero out the array. The solution was to remove the constructor that zeroed the array in one of the compilation units.
To me the code smells really bad to begin with. I have not convinced anyone to address this issue yet. -
Demolishun3467920d@CoreFusionX
file1.cpp:
struct GlobalStruct1 {
int stuff[10];
GlobalStruct1(){
// init stuff to zero here
}
} gloablstruct1;
file2.cpp:
extern GlobalStruct1 globalstruct1;
// update an entry in struct in some global classes constructor
The order in which these are run in the different units is not guaranteed. -
Naturally typing out Kernighan is easy.
foo() {}
And then you just stick your crap in between, and hit newline where necessary.
Allman is does the newline in a different order but still not as bad.
My editor does most of the work though, so I don't think about it. -
CoreFusionX344620d@Demolishun
Aha, yes, the good ol' static initialization order fiasco.
Which is, in short, undefined behaviour, so if you are relying on it, you're walking on the edge.
It's a very big code smell and you should rework it ASAP.
Also, the way you declare your classes (in CPP struct just means public class) screams C coder left and right XD. -
CoreFusionX344620dYou can, by the way, solve this, by just encasing all these modifications you make of the global into a function each in its own translation unit, then have another function that calls these in whatever order you need. Call it first thing into main() or in a static constructor if you will, and there you go.
For reference, it's called the construct on first use idiom. -
Mental disorder is also enforcing particular code style by a compiler itself, instead of using linter. I understand if there's no other way because there's no brackets like in python (but it's interpreter so idk).
-
@Demolishun
struct Point {
int x, y;
} p, *pPtr;
I only knew about this from this syntax, but at the same time didn't correlate with regular variable syntax of pointers. -
@Pogromist I know this syntax and I always wondered who would use this. I mean you are declaring a name for the type anyway, so there is no point in doing it inline.
It would make more sense for anonymous types. -
@Lensflare
when it's regular struct declaration, the names before ';' or after '}' are variable declarations of that struct
struct Point { int x, y; } p, *pPtr;
struct Point { int x, y; } p = { 5, 5 }; // you can even initialize it there
struct { int x, y; } p, *pPtr; // variables of that unnamed struct could be declared only once
When it's typedef it makes alias for "struct Point"
typedef struct Point { int x, y; } Point, *PointPtr;
So you can use "Point p = { 5, 5 };" instead of "struct Point p = { 5, 5 };"
When it's anonymous typedef
typedef struct { int x, y; } Point, *PointPtr;
you can only use "Point p" and not "struct Point p"
And it's useful when the type name shows that it's a pointer
PointPtr p = (PointPtr)malloc(sizeof(Point)); -
@Pogromist yeah, as I said, the anonymous type thing makes sense and is quite nice actually, but naming it (Point) and still using it inline without a typedef is just weird.
So true
joke/meme
code styles