Noob learns to code in 3 months

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 8th, 2017, 2:07 pm

hmm that makes me think I might've misunderstood him. Will try to message him and see if he can clarify

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Noob learns to code in 3 months

Post by albinopapa » September 8th, 2017, 4:44 pm

chili wrote:
Yumtard wrote:Teacher mentioned that it's preferred to pass char pointers than std::string references so I changed all the std::strings om my code to char*
This is actually kind of worrying... :?

https://stackoverflow.com/a/23383614/3773305
I had a debate on The Cherno's youtube channel a while back about this. I called him out for claiming to teak C++, but instead of using std::string he used const char*.
The Cherno wrote: I usually don't reply to comments that start out with something as stupid as "you didn't write exactly the code that I would have so I stopped watching halfway through the video", but since you took the time to write such a long comment I thought I'd reply. If you have your own ideas, great, you don't need to watch my video or other people's videos at all. However this C++ series is for people who actually want to learn how to write good code.

Yes, I could have used std::string. However why on earth would I go through the process of constructing an entire object and causing memory allocations for something as simple as providing std::cout with constant data that would simply be stored in the binary? This may seem like a petty optimization, but there's absolutely nothing wrong with using a const char ptr in C++ code. You should probably also know that most C library features are much faster than their C++ equivalents, which is why everyone in the AAA games industry relies mostly on them.

This series is not your typically "hey lets learn C++ by limiting ourselves only to the newest features". No. This series is about using all that C++ has to offer to write the fastest and "best" code possible. Of course the word "best" is subjective, but it's based on my personal experience in the industry and on the way that I like to do things.

So if you're willing to actually listen you might learn something new from this series, and that is my hope, however if you start disputing "stylistic" differences then that's not going to do anyone any good.
This is his only response to my initial comment, but others did reply one in favor of teach strictly C++ and the other defending the youtuber.
Courier wrote: You're right because std:string is a type that stores char-like elements with known size and capacity, while const char* is just a pointer to const char data that is expected to be null terminated. I think nowadays for C++ is highly recommended to stick to "modern C++" and leave low level details for later, just like one would do in any other language. Kate Gregory in “Stop Teaching C" at 9:45 talks exactly about this point
albinopapa wrote: Yeah the const char * thing doesn't necessarily mean a C string either. It could just be a memory pool for instance. Granted, I've seen others around the internet using std::string in a similar way, but for the most part, if your functions request a 'const std::string &' you know what data the function is requesting. This comes into play when you aren't the one creating the functions
eatplayhate wrote: Using a const char* as a null terminated string is an idiomatic approach and wouldn't confuse anyone, especially if you have the context of a function named "Log". The small-string optimization in std::string is not mandated by the standards committee and is not present in many STL implementations including libstdc++ (although yes, it is present in the MSVC++ runtime and libc++).

Speaking as a game developer, one of the issues I see with programmers who become a bit militant about "modern" C++ is that they are missing out on many of the things that make C++ such a useful language for high performance applications - it is a mixed paradigm language which allows you to express fairly complicated ideas via things like templates and lambdas, while also allowing you to seamlessly go down to a lower level (C-style) subset of the language when you need or want to.

In this particular instance, using a std::string would require changing the function signature to use a const reference (because passing strings by value is needlessly expensive), and because the small string optimization isn't guaranteed, it may very well cause a heap allocation just for the privilege of packaging a constant block of char data in an object which doesn't add anything to the equation as the function is simply forwarding it to another system which is quite happy with a null terminated char array. In addition, taking a const char* is perfectly functional for users who have std::strings, as there is a simple and effectively free conversion from std::string to const char* via the c_str() method.

On your other examples - I would be very surprised if when he gets to the point of needing a dynamically resized array that he would do anything other than using a std::vector, but bear in mind that it is just one option available to you - many games do not use the "normal" STL because they want either custom allocator behaviour or guaranteed performance characterists (see EASTL, which is now open sourced). I also regularly use manually created arrays, either via new() (often to allow me to pack several types of objects together) or even using alloca() when the size is bounded but the count is still variable.

Finally, I have no problems with people using or wanting to use things like std::make_unique or whatever, but often these come with a price which is rarely considered, and it is important to have all the tools available to you that will allow you to make the best choice.
The last sentence about std::make_unique having a price confuses me. I've read up on it and it's no different than calling std::unique_ptr<type>(new type) with added benefits of exception safety if I recall correctly.

I like the video that the first respone posted.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 8th, 2017, 10:16 pm

Interesting read!
Cherno def got triggered tho

User avatar
krautersuppe
Posts: 91
Joined: September 14th, 2015, 10:58 pm
Location: Istanbul

Re: Noob learns to code in 3 months

Post by krautersuppe » September 9th, 2017, 1:00 am

Yumtard wrote: Just wanna correct your last sentence. "That is what you're paying for" to "That is what you're getting paid for"
I live in Sweden so I get about a 350 dollar salary to study :P
You pay with your time and effort. For 350 dollars a month - at least where I live - I could survive in a shared apartment without electricity eating potatoes only(I guess in Sweden it is the same). Sleeping under a bridge might be an option but not in winter -haven't tried that yet though so maybe it's possible :)
albinopapa wrote:
I had a debate on The Cherno's youtube channel a while back about this. I called him out for claiming to teak C++, but instead of using std::string he used const char*.
The Cherno wrote: I usually don't reply to comments that start out with something as stupid as "you didn't write exactly the code that I would have so I stopped watching halfway through the video",[..] If you have your own ideas, great, you don't need to watch my video or other people's videos at all. However this C++ series is for people who actually want to learn how to write good code.
[..]
So if you're willing to actually listen you might learn something new from this series, and that is my hope, however if you start disputing "stylistic" differences then that's not going to do anyone any good.
:shock: Quite rude that cherno guy.
DSU
Discord: dsu1, GitHub: https://github.com/DSpUz

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 9th, 2017, 5:54 pm

Watching the linked list video twice. It's still a bit hard comprehending some of the stuff.
Trying to figure out a way to use a linked list as an inventory but it's not working out very well so far. instead of just storing an int val in the Element class I'd need to store a Weapon pointer which is causing issues.

krautersuppe wrote: You pay with your time and effort. For 350 dollars a month - at least where I live - I could survive in a shared apartment without electricity eating potatoes only(I guess in Sweden it is the same). Sleeping under a bridge might be an option but not in winter -haven't tried that yet though so maybe it's possible :)
This is ofc true :) And yeah I obv have to take student loans in order to pay the bills since I wont have the time/energy to make any money on the side.

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 9th, 2017, 9:23 pm

Thinking I could do something like this

Code: Select all

class Inventory
{
public:
	void AddGun(WeaponData* data_in)
	{
		if (Empty())
		{
			pFirst->pNext = new Gun(data_in);
			pLast = pFirst->pNext;
			pLast->pNext = nullptr;
		}
		else
		{
			pLast->pNext = new Gun(data_in);
			pLast = pLast->pNext;
			pLast->pNext = nullptr;
		}
	 
	}
	bool Empty();

private:
	
	Weapon* pFirst = nullptr;
	Weapon* pLast = nullptr;
};
but then Weapon* pNext in the Weapon class must be public :shock: or at least have both a getter and a setter

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Noob learns to code in 3 months

Post by albinopapa » September 10th, 2017, 2:00 am

Depending on how you handle cycling weapons, you might want to implement a doubly linked list with functions to increment or decrement a current weapon pointer and a GetCurrent() function. Linked lists (single or double) aren't random access, so if you plan on allowing random weapon choice and insist on linked list, you'll have to either track where you are in the list or start at the beginning of the list and traverse to the weapon you want to be active depending on which type of linked list you are going to use.

In regards to the Cherno thing, I didn't take offense and he probably felt personally attacked. More than likely he's had this same argument with others a few times. Later videos of his he explains why he chooses const char* so at least I feel like I got my point across. I apologized to him in that same video and thanked him for taking the time to respond.

I ended up looking up the EASTL ( Electronic Arts STL ) and their string class for I think is more versatile for different character sets ( different languages ). I think they also have a short vector, where if you only need a few items, it uses the stack but once it grows beyond the stack allocated size, it allocates on the heap making smaller vectors faster and still allowing growth and versatility. You'd have to check it out yourselves to see what else there is.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Noob learns to code in 3 months

Post by chili » September 10th, 2017, 6:21 am

albinopapa wrote: I think they also have a short vector, where if you only need a few items, it uses the stack but once it grows beyond the stack allocated size, it allocates on the heap making smaller vectors faster and still allowing growth and versatility.
This optimization is present in msvc and gcc's libstd as well afaik. Generally called short string optimization (SSO).
Chili

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 10th, 2017, 3:18 pm

Yeah I did came across doubly linked lists when I read about them and started working on one for an inventory. I'm not really doing it because I feel it suits my program but just to get some practice with linked lists.

I had a horrible weekend so not much studying done. Got a fever and also had to go to the vet with my dog who seems to have hurt his neck

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Noob learns to code in 3 months

Post by albinopapa » September 10th, 2017, 11:57 pm

no bueno
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply