Handmade Hero » Forums » What are the downsides to using namespaces?
Berend
6 posts
#10019 What are the downsides to using namespaces?
2 years, 9 months ago

Hi there,

Newbie question!
I'm only at day 30 and until now almost all functions are prefixed with 'Game' or 'Win32' and types likewise. I've looked at Caseys latest commit and it seems that he stopped prefixing the Game code after a while.

What is the reason not to structure code into namespaces? Is it searchability? Or because it is trivial because you can just name a function whatever you want?
Dont you benefit from knowing about the context of a function and allowing them to use the same name if they want to?

Something like this?

Game::GetState();
Game::Entity::GetState();
Game::Sound::AudioSource::GetState();

Thanks!
Kelimion
Jeroen van Rijn
256 posts / 3 projects

A big ball of Wibbly-Wobbly, Timey-Wimey _stuff_

#10021 What are the downsides to using namespaces?
2 years, 9 months ago

Hi Berend.

Not an answer to your question. I'm just informing you I'll be deleting the duplicate thread. There's no value in having two threads with the exact same question, so at a guess I'm going to say you duplicated the thread by mistake.

- Jeroen.
BillDStrong
Bill Strong
52 posts
#10022 What are the downsides to using namespaces?
2 years, 9 months ago

Berend
Hi there,

Newbie question!
I'm only at day 30 and until now almost all functions are prefixed with 'Game' or 'Win32' and types likewise. I've looked at Caseys latest commit and it seems that he stopped prefixing the Game code after a while.

What is the reason not to structure code into namespaces? Is it searchability? Or because it is trivial because you can just name a function whatever you want?
Dont you benefit from knowing about the context of a function and allowing them to use the same name if they want to?

Something like this?

Game::GetState();
Game::Entity::GetState();
Game::Sound::AudioSource::GetState();

Thanks!


Do you need to be reminded you are building a game?

Namespaces are meant to avoid name collisions. Not to organize your code into a hierarchy. In C, code isn't organized into hierarchies, id ir organized into files.

Your examples hint that you are thinking in OOP, so I will direct you to the many fine examples on the site of why that is bad according to Casey.

Also, we are using function overloading, which means we know what a function is doing both by its name and the data it is working on.
mmozeiko
Mārtiņš Možeiko
1971 posts / 1 project
#10023 What are the downsides to using namespaces?
2 years, 9 months ago Edited by Mārtiņš Možeiko on Dec. 19, 2016, 8:47 p.m.

Berend
What is the reason not to structure code into namespaces?

Because its longer to type.
Compare "Game::GetState" vs "GameGetState" or "GetGameState" - 14 vs 12 characters. Why complicate life and make everything longer to type and take more horizontal space on screen?
Berend
6 posts
#10027 What are the downsides to using namespaces?
2 years, 9 months ago

I see I see, very valid arguments.

I guess that I'm still thinking too much in terms of OOP and you're right about that Mārtiņš.

Thanks for you answers :)

@Jeroen Yes I thought that I posted it in the wrong thread ;) Thanks