Oddbean new post about | logout
 ``` C++
auto p = MyType();
... 20+ lines 
// what tf is p? even if it's named correctly it should be named for it's purpose, mutually exclusive to it's type. 
```

Same with C#
``` C#
var p = new MyType();
```

My preferred style in all C-ish languages

```C
void Foo(void)
{
  //Declaration, tells me and my compiler what memory will be in use throughout the function
  int myInt, myInt2;
  int* myPtr;

  //Assignment is it's own statment
  myInt = 0x01;
  myInt2 = 0x02;

  // easily cleanup or destroy memory if needed because I know at the top what's been created
}
```
 
 i remember the olden days of C... declare before use pattern, even was (is?) required for functions, in order to call them they must be further up the page

i've got so used to the Wurth style returns to the right of the function and receivers to the left though, and do you really need to explicitly state that a function has no parameters like that? 
 Yes C89 enforces declare before assign. They must be completely separate statements ';'. 

>and do you really need to explicitly state that a function has no parameters like that
For C89 and -pedantic, yes. Some compilers even in C89 mode don't care, GCC will complain though. I think the Windows cl doesn't care though.  
 i'm just quite content over here with my language with implicit things that are implicit 
 also with the file-level non-imperative declaration (though it does execute functions in var and init() in top to bottom order, that can be troublesome sometimes but it is what it is, and globals are bad mkay) 
 "Moisturized, in my lane" 
 yeah, i was having a bad afternoon when suddenly somehow it seemed like the event store wasn't keeping tags but no... it wasn't decoding tags... and it seems i have retained the memory leak bug fix as well so i'm feeling very moisturised 
 Any idea what the issue was? It's been a saga my friend 
 yeah, it wasn't growing the arrays to fit what was being put into them

still got another glitch in here somewhere, probably a regression... again a forever loop in the database query code lol

idk even how it's happening, probably me putting the wrong direction <> in somewhere 
 i think i found it... i switched to collecting the found events in a map (key/value hash table) and i was checking the limit size based on the result variable which isn't populated until after all the event keys are retrieved

i think this will fix it

it's that kind of forever loop where your termination condition is incorrect and it keeps looping because the condition doesn't update to reflect the state of execution 
 btw, the point of using the maps is it allows me to automatically deduplicate in case of clients sending filters with repeating elements that yield the same event again... which probably can happen, so best to mitigate it 
 Ok I see thanks. So, my security code review eyes would want to make a comment on this style and say that declaration should go with assignment.

Because what can happen is that the default assignment is sometimes used mistakenly and results in undefined behavior.

Obviously not an issue if you follow up with the assignments as you did, but just stating an observation. 
 True but I think even -Wall -Werror on all the compilers I've worked with whine about use before assignment. 

If you have a function with 40+ lines it's easy to miss where a variable may have been declared. I use unconditional jumps often, so I like to see where variables are declared incase I need to do something with them. 

If my variables are declared immediately, I always know they are accessible no matter where I jump to in the function. 

But I definitely get the UB of default values. I try to assign variables as the first statement's if possible, and manually assign default/failing values for this reason.  
 Yeah, -wall -werror fixes many problems. 
 Actually when using Visual Studio the intellisense even freaks out with accessing structure members after declaration (before assignment) which is proper behavior in C but I guess frowned upon in C++