Coding Blocks - Data Structures – Arrays and Array-ish
Episode Date: December 3, 2018We continue our deep dive into data structures, this time focusing in on arrays and array-like types as Allen gives Shania Twain some singing competition, Joe is going to owe us another tattoo, and wa...it ... when does Michael think C++ was invented?
Transcript
Discussion (0)
You're listening to Coding Blocks, episode 95.
Subscribe to us and leave us a review on iTunes, Stitcher, and more using your favorite podcast
app.
And wait for me to scroll up to the top of the page so I can see that I say visit us
at codingblocks.net where you can find show notes, example discussion, and a lot more.
Rookie mistake.
Send your feedback, questions, and rants to comments at codingblocks.net, follow us on
Twitter at Coding Blocks, or head to www.CodingBlocks.net
and find all our social links there at the top of the page.
And with that, I am the different sounding Alan Underwood.
Froggy.
And I'm Joe Zach.
And I'm Michael Outlaw.
And I'm glad that I wasn't the only one caught off guard
and had to scroll up really fast to the top of the page. Excellent. This show is sponsored by Datadog. Now,
you've heard us tell you about Datadog before. So you know that they're a software as a service
monitoring platform that provides developer and operation teams with a unified view of
their infrastructure, apps, and logs. But did you know about these features?
Like Watchdog. Watchdog
automatically detects performance problems in your application without any manual setup or
configuration. By continuously examining application performance data, it identifies anomalies like a
sudden spike in hit rate that could otherwise have remained invisible. Once an anomaly is detected,
Watchdog provides you with all the relevant information you need to get to the root cause faster,
such as stack traces, error messages,
and related issues from the same time frame.
Or what about trace search and analytics?
Trace search and analytics allows you to explore,
graph, and correlate application performance data using high cardinality attributes.
You can search and filter request traces using key business and application attributes, such as user IDs, host names, product SKUs,
so you can quickly pinpoint where performance issues are originating and who's being affected. Tight integration with data from logs and infrastructure metrics also lets you
correlate these specific trace events to the performance of the underlying infrastructure
so you can resolve the problems quickly. And let's not forget about logging without limits.
Logging without limits lets you cost effectively process and archive all of your logs and decide
on the fly which logs to index, visualize, and retain for Analytics and
Datadog. Now you can collect every single log produced by your application and infrastructure
without having to decide ahead of time which logs will be the most valuable for monitoring
analytics and troubleshooting. And Datadog is offering our listeners a 14 day free trial with no credit card required
and as an added bonus for signing up and creating a dashboard they will send you a data dog t-shirt
so go over to datadog.com slash coding blocks to sign up today all right and And like we always like to do, it's time to say thank you to the people who left us a review. So from iTunes, we have to install itunes so we appreciate those as well as well so big thanks to uh canan and weird
flex but okay uh that's excellent um nice job on reading those there outlaw i was impressed with
that i studied yeah hey so so sean from our slight group and also our community talks that we've done
on occasion he had a nice little question that came up on slight that i thought was worth
hitting up here and it's how do you get other people within a company to get on board with
writing better code and i i want to get you guys thoughts, or maybe I'll go first. My thoughts are,
you kind of have to create the excitement yourself a little bit, right? Like,
being somebody that's listening to a podcast like this, or being somebody that goes out and
picks up books, and you're constantly trying to get better and perfect your art, like,
not everybody does that, right? Like not everybody is spending
their off time trying to figure out how to get better. And that's why people that do try to
constantly improve and get better at what they're doing. You kind of got to lead by example a little
bit, right? And, and I feel like if you start, if you start making motions in that direction,
people will start seeing the benefits of it.
And then you start to get sort of a group buy-in on it, right?
Like, oh, I see how easy that was.
I think that's kind of how it has to go.
And also try and make it like, hey, look, guys, check this out and create the excitement, right?
If you try and force something down somebody's throat, then they're going to have a hard time, you know, taking it. But if you can try and create a little bit of excitement, like, yo, check out this pattern, right?
And look at what it allowed me to do over here, then I think that's how you go about doing it.
That's my two cents.
Yeah, and kind of along the same lines, I thought, like, Lunch and Learn is always a really great way of doing that sort of thing. Because if you can kind of pitch the idea, like, hey hey uh you know next friday let's get lunch catered and if you can get somebody signed off
on it otherwise you know just you know encourage people bring their lunch or something and uh give
them some meeting time there but on the calendar and try to show something cool like even if it's
just something small like um i guarantee you stuff like that tends to go way over so even if you come
up with like something that you think is going to take like 10 minutes to show, like some cool plugin or some neat way,
your novel way,
you solved a problem and just kind of show that people will get excited and
hopefully you can get them kind of to,
uh,
become invested in and want to share too.
And as long as you,
you know,
show something kind of cool or novel and try to make it fun.
I think that's,
um,
the best advice I can really give.
But I do want to mention that,
um,
long time ago, once I heard that
someone said, I don't know
if this is an official quote or if
the person I was talking to just made it up, but
they said basically they can teach someone JavaScript or
C Sharp or how to code,
but they can't teach them to care.
Right.
So ultimately, if the reason that people are
not getting on board with learning
better code is just because they don't care, then I think you're fighting an uphill battle.
But I think what you'll find is not necessarily that people don't want to write better code.
It's just that it maybe competes with their other interests or they disagree with you on what better code means or something like that.
And so if you can get those lunch and learns going and you can get people involved and invested, then I think that's the way to go.
So I'm going to call a little bit of BS on both of your answers.
But you said a lot of words that were like things that I liked.
There were keywords that you said that I liked.
So I'll hit on those real quick.
I liked the lunch and learn idea.
I liked the lead by example idea.
I think those kind of fit hand in hand.
The lead by example is just showing them like, Hey, I've created this
and you'll look how you can do this.
The reason why I say it's all BS though, is until you can prove to them like, Hey, this
thing is going to make your life better than their care.
As you said it, their incentive is the, they're not going to have any care or incentive about
it until they can figure out like until you can express to them why this is going to make their lives easier.
Right.
That's when they're going to be on board.
I agree with that.
And that's why I think the, the example, even the lunch and learns, I liked them, but I've,
I've found in the past, a lot of people will tune out if it's not something they're hyper
interested in.
Right.
You know?
Yep.
I totally agree.
But I will say that there's even been
times that I've done talks like when we did domain driven design, you guys remember we talked about
the specification pattern and it was something that I really liked a lot because it was,
how do you, how do you validate things? Whether you have two separate pieces of data coming in
that don't live in the same class, right? Like you've got user authentication or user authorization, and then you have,
you know, orders. How do you validate that stuff? And I'll never forget, like I put together an
entire presentation on it. I talked about it and literally everybody was like, oh, cool. Okay.
And it was gone. But what was awesome was I want to say it was several months
later, some changes needed to be added. Some additions needed to be added to that one section
of code where I use a specification pattern and the way that things had typically been done,
it was really ugly. They would have been changing client side code. They would have been changing
server side code all the way through. Right. And it would have been done in a very one-off way. Well, because I had that pattern in place, I was like, look,
dude, just do this right here. And he was like, oh man, that was so easy. Like he thought it was
going to take him four to five hours to do what he was going to do. He got it done in 10 minutes
because after he understood the pattern, then it was like, Hey, it's there. And that automatically got buy-in, right? Like at that point, they're like, I get it. I totally get why you'd use this now,
because I can centralize my, my business logic and my validation in one place. Right. So
yeah, I think if you can create the excitement, it's going to be hard to sell it without showing
them, you know, Hey, check this out.
This is what we can do now.
Right.
Yeah.
I don't know if I've written C sharp since then.
Oh man, that's sad.
It's been a while.
So yeah, I thought that was a great question.
Hopefully, you know, maybe, maybe you can take that back and share it.
And, and I do think that just, you know, showing some people and getting people excited about it is probably the key.
One thing I want to mention, too, is you kind of got to know your audience, too, a little bit.
So if the reason that people are maybe having a hard time or maybe not kind of moving them that's kind of like, you know, newfangled complexity they don't need.
So they may be more interested in something like how a garbage collection works.
Maybe you can kind of, you know, stoke that fire a little bit, get those embers kindled or whatever.
Get that fire started by kind of speaking to those desires rather than maybe what you think is cool, which it stinks.
So, like, ultimately, like, everything we talked about, it kind of involves, like, work on your part, which, you know, kind of stinks.
Like, you got enough work to do, right?
But hopefully it pays off.
That is so true.
So, basically – I'm sorry.
I thought you were done.
No, go.
Go ahead.
Yeah.
So, basically, in a nutshell here, not only do you have to be like you know a good
developer but you also have to become a good motivational speaker and a good salesman so
yeah and you have to put in the extra work right and that's the thing that really stinks is usually
you've already got enough work to do but if you want to see change you got to be willing to try
and put forth the effort to make that change start happening. So, uh, yeah, that stinks.
It really does.
Um,
I,
and so the next thing,
uh,
BMO over on Slack,
who is also the infrared,
the infrared dev,
um,
really cool site.
What do you remember the URL Joe?
Uh,
no,
is it?
And for,
I will,
we'll get that here in a second,
but he pointed out in,
and I always do this
when we're talking about ssd um performance because everything's measured in megabits per
second like when you talk about internet speeds and all that i always forget about when you're
going over to drives it's actually megabytes per second so the order of speed of those ssds that
we were talking about that we're doing on
episode 93 of the shopping spree,
it was actually megabytes per second,
which when you start thinking about that is insane.
So it's like 3.5 gigabyte per second reads on some of those drives.
Oh man.
Anyways,
you know,
I forgot since you brought up that episode too,
you know, I forgot since you brought up that episode, too.
You know, there's kind of like a little elephant in the room since we've had that that conversation, because, you know, we recorded that prior to Apple's announcement. And my big plan was I was going to take, you know, four or five hundred dollars, put it in some Bitcoin and hope that by the time Apple did have the announcement that it would have like, you know, obviously it would, you know, quintuple or triple or, you know, whatever,
quadruple, so that I could get an iPad.
That didn't work out so well, did it?
Oh my God, Apple.
The highest end iPad?
iPad is like $1,900.
So, yeah, no way is my Bitcoin going to even get me close to that because obviously it's not.
That market is apparently not going that route so far.
Yeah.
And on top of that, you still need the keyboard and the pen, right?
Or pencil.
Well, only if you wanted to use it like a Surface Book.
Yeah, because it's now a computer replacement, whatever.
There's rumors.
There was talk of what life would be like with a 15-inch iPad.
Never mind.
Hey, I will say it's pretty cool, though.
They're saying that you can use the ipad as a monitor for the
ipad mini so that's kind of interesting wait for the ipad mini for the ipad or not ipad you mean
the mac mini mac mini i'm sorry yes so yeah it's like two thousand dollar 15 inch monitor that's
sweet what no it's it's more like a 13 inch yeah it's, it's a 13-inch. That's right.
Yeah, 15.
I would be upset about 15-inch.
It seems like most laptops aren't even 15-inches anymore.
Yeah.
These 13-inch laptops, they can go away.
Which there's another elephant in the room.
There's a lot of elephants in this room.
There are.
Well, it's a zoo.
Because we've been recording this show now for like five years, we all have max we all love our max but you know there's a lot of episodes from five years
ago where we like loved them more and the stuff that was being made we loved more and you know
we i think have been pretty consistent like yeah i don't think i like that touch bar you know i'm not i'm
not sold on that one i think the three of us have been kind of consistent that maybe not maybe i'm
wrong oh yeah we're pretty consistent yeah you know every we get people who are always listening
to the back catalog and like man how can you guys recommend a macbook and i'm like well we're not
saying we would do it today right yeah yeah totally i i i would
have a hard time recommending a new one with the butterfly key using it nah get out of here i give
me give me some real hardware although of course i say that now i just remembered though that that
was like what joe started with with like wasn't that how you're you're like a couple episodes ago
with the shopping spree thing didn't you start with like well if i was just going to buy everything
one shot i'd just get the macbook i think uh you know i don't remember that might
have been past year but i still want one now i think that's the sad part it's just i want those
two things to change so i still think if i was to get a laptop and budget were not a concern like i
still would be going for the macbook pro it's just i really don't like those two things about it i
don't think i would i if for the same price i probably get just I really don't like those two things about it. I don't think I would.
For the same price, I'd probably get the Surface Book 2, I think.
But the problem is they haven't updated the processors,
which is why it's not on my list.
So maybe the elephant is halfway in the room.
It's halfway in the room.
And by the way, the website for BMO on Slack, it's theinfradev.pro.
So theinfradev, I-N-F-R-A, dev. pro. So the infrared dev,
I N F R a dev dot pro pretty cool stuff.
So,
uh,
you go ahead.
No,
go finish your thought.
I was going to say,
he's also on Twitter at the infrared dev.
So go check them out.
And I'm telling you,
you gotta love the confidence there at the dot pro.
Like that's just,
that's putting it out there.
He's not,
he's not a consumer grade.
He's not amateur grade.
This is dot pro level stuff. So I love it we gonna are we gonna buy the dot mediocre uh top
level domain yeah wouldn't that be awesome no we should get dot me coding dot me
that's amazing so that's that's it for the for the news that we have here.
Yeah, so this episode we're going to be talking about some basic data structures.
Remember last time we talked about some primitives,
and we were kind of leading up to talking about data structures.
And here we are.
We have arrived.
Yeah, and so you're going to think that this show is not going to be all that interesting
because I know data structures.
And we all thought we knew data structures until we had pages of notes for every little thing that we were looking at.
Right.
So I thought I knew floats.
I can't wait to see where this stuff is going.
Oh, man.
So we're going to kick this off.
And I landed with a raise.
And I'm actually going to do something a little bit different we'll get into here in a second.
So first, what is it?
What is an array?
It's simply, like, just summarizing, it's a way to store a collection of items.
That's, in a nutshell, that's it.
Now, some people are probably going to go crazy right now, and they're going to be like, no, it's not just items.
They have to be same type items. But we'll get into that in a minute um now the interesting
curve i took on this is i wanted to get to the heart of where arrays came from and how they're
used in various different languages and i thought the best way to do that would be go back to c
right because it's probably the lowest level one that you can work
with on a daily basis, unless you're getting down into a different type of programming.
And so I thought I'd start there. So the first thing to know is an array in C,
yeah, it's a collection of things, but it's stored in a contiguous block of memory.
So if you have a 20 item array, then it's going to start at a memory location. And then all the
rest of the data in that array is in the very next register of memory all the way down until,
until you're out of, of that array room.
Which is why they're able to be fast.
That's why they're able to be fast because typically you're iterating through the,
and it's called pointer arithmetic,
but as you're going through the thing,
it's basically pointing to the next block of memory
where the next item is.
And as you go through it,
you're just going to the next memory location,
which is right next to where the other one was.
So super fast to iterate through them.
It's also really good for random access. So if you need to go to the 14th element, we
know that it's basically the size of the items that are in your element
times 14, right? Starting with the original
index and memory. And another thing I was going to add to that, though,
is because it's just
the uh that pointer arithmetic that you refer to and as joe put it like you know 14 times the size
of it it's a o of one operation for random lookups all the access is is constant time it's it's right
there so here's the thing so So that all sounds great, right?
Like you have this thing.
It's super fast.
It's all in one block of memory.
But here's the thing that kind of stinks about it is you either have to declare this thing with a type and a size or you have to initialize it with the elements that you want at the time that you want to create an array that's going to have 100 elements in it, then you can you can do that thing up with my array.
And then, you know, brackets 100.
And you have this nice brand new array with 100 memory slots in it.
And it already allocated the memory for you, starting wherever it was going to start and then saving the space for those 100 elements, knowing the size of an integer.
Right.
So that's great.
Go ahead.
It's good.
So,
you know,
you ever have programs like in like,
um,
like assignments like computer science one-on-one and like let the user enter
a number and then press enter and then keep entering until they enter some
number of numbers and then they add them all up and tell them the sum.
And like,
I remember doing stuff like,
okay,
uh,
well erase this fixed size. So I'll do a like, okay, well, a race is fixed size,
so I'll do 100 because there's no way anybody's going to type 100 here.
I would just kind of pick a number because I didn't know any other good way of doing it.
Realistically, the whole point of the assignment was probably to teach me
that that's not a smart thing to do and to really talk about better ways to do that.
But I just thought it was kind of funny.
I remember doing stuff like that, just picking max sizes
because I didn't really know how much i needed but probably not a hundred right
right nobody's gonna enter a hundred no how much ram have you got let me just take all of it
available for my array right and so so the other way that you could do this is you can say at my
array and just do the open and close square brackets there. And then you can initialize it with a list, right? So equals open curly one comma two comma three comma four, et cetera, right?
And then that will basically create your array, populate it with the size that you need in memory
and you're good. You can also in C, you can mix the two and you can say int my array and then
bracket 10 equal and then do an initializer and you could
just put three elements in it and so basically you'd have items in there for one two and three
and then your other seven would not be defined at that point in time or they wouldn't have been
filled in with whatever you wanted but your size of the memory block would still be, you know, 10 integers. So that's all pretty interesting. The next thing
that I thought was really cool, and I'm not used to because it's been so long since I've done a
low level programming like this, is there's no indexing, index checking in C. So in C sharp,
if you say, hey, I'm creating an array of size 10, and then you go to reference
index 11, it's going to throw an out of bounds error. Oh yeah. That's a mild friend. Yes.
You'll see it. And it happens to all of us because we'll forget, Oh, it's zero index base. I forgot
to do less than this minus one or something. Right? So you'll get those things. He'll get it
in C. What are you getting? See garbage. You'll get something that was stored in that
memory register, but it's going to have no legitimate
meaning towards what you were trying to use that array for. So
you got to be careful. Debugging stinks. It's like error.
What? It's like, is not
an int or whatever. Well like well yeah i know but why is it not yeah
so i get frustrated enough with array index out of bounds like that's why why we why did you do
that stupid computer you're broken oh that's awesome oh so yeah that i thought that was
really interesting i mean i guess i've gotten so spoiled by at least those good errors in, you know, the newer languages that that didn't even occur to me that that's how things work back or still in that language.
And C is not a dead language, right?
Like people still write performant applications in C or cross compatible applications in C.
I mean,
web assembly is written in C.
So,
you know,
interesting things there.
And that's a big security hole too,
right?
Because if you end up going out of balance and you're going to read memory
that you shouldn't have been able to read and theoretically,
if you can change stuff in a way you can write to memory that you shouldn't
have been able to write to.
Yep.
That's true.
And here's another one. That's kind of interesting.
It also won't blow up if you initialize it with more data than what was in the
defined size.
So if you mix those two where you're saying,
Hey,
I want something that size 10,
but then you put 20 in it and it just,
it,
it's going to be fine.
It's just going to keep going.
It's not going to store that stuff where you wanted it to,
but it's not going to yell at you either.
Uh, C C plus plus c sharp java all those they will yell at you at compile time right that hey you did something really stupid go back and fix this so um this this was something that was
interesting i actually spent a lot of time reading this because it's been a while since I've worked with pointers. So in languages like C, people get confused between an array and a pointer.
And here's what they had to say about it. It's true that the array starts at a spot in memory
and then contain the contiguous memory space, right? But it's not the same thing as having a
pointer that's the first memory location.
And the way that the,
the way that you can validate this,
like if you don't believe it,
right?
Like,
because you can output array or you can output,
you know,
the,
the memory location there and you'll get that value out of it,
but you can do a size of either the pointer or the array.
And you'll see that they are actually two different things.
Assuming,
assuming that your pointer and your array are not'll see that they are actually two different things assuming assuming
that your pointer and your array are not just one right but or of size one but you'll actually see
that there's a difference there that's the point i was uh thinking like javascript for example
there's a dot length property so you can call on an array and like there's nothing special about
this kind of low level data structure we're talking about that has a magical dot length property.
So there's something that's special about that type.
Yep.
JavaScript's a whole other ball of wax that we're going to get to after we get away from this granddaddy of programming.
But you said granddaddy.
Like I know you mean it from a good place, but I still consider like C is like the it's still like the the backbone
of everything you want something fast you just you do it and see it's still like linux is in c
right like that that's what you're going to use i was actually thinking about this the other day too
is that you ever wonder why like how we keep going back to c for things like operating systems for example
or games or like things that we need fast like python is written down in the c like is it is it
because like all of the classes and inheritance and everything like it adds too much slowness on
top of it that i mean yeah i don't know i don't think that's what it is um and i know where you're
going i don't think it has anything to do with the object inheritance i think it has to do with
the fact that you actually uh and i'll get into it a little bit here more in depth but like for
an array there's this thing called dynamic um dynamic creation of an array. And basically all that means is you're actually telling it, you're calling a memalloc, I think is what it is.
Memory allocate.
Yeah, malloc.
Memory allocate this for me.
You have access to the underlying memory, right?
And you don't necessarily have that in these other languages. So like Joe said, you can shoot yourself in the foot and you have all these security holes when you can access these memory locations that you weren't supposed to have access to.
But the fact that you've got access to that low level stuff, like I think that's why YC is more close to the metal, right?
That's why you get that speed. Whereas,
you know,
something that's like C sharp,
but C plus plus is probably closer to C in terms of raw speed.
Cause I think you can do similar things,
but,
but I think that's what it is.
I may be wrong.
I don't know that it has anything to do with inheritance or anything as much
as it's just,
they give you basically here's everything.
Go do what you want and try not to screw it up.
Right.
Fast portable stuff is still in C it is so yeah i don't know if you were to graph like popularity and uh age for program languages i think c is at the peak of that like algol is older but it's not
as popular like c is like a far outlier liar there and c++ is close to i think but um yeah i mean
c and c++ are still like really hanging in there for as old as they are like when did when do you
think c wasn't invented oh golly i don't 70 something maybe or was it further back than that
c++ was 80s i thought this was pre was in the late 90s.
1972, you said?
Yep.
Okay. I mean, you're talking about these in terms of their popularity, though, but I'm looking at the Tybee index for them.
C and C++ are number two and number three.
That's crazy.
And popularity, what's one java java wow so i mean
like i'm kind of coming to the defense of c when we say when you say a grandfather because i'm like
well i mean yeah it might be the ones like get off my lawn kid like you know but it's still
awesome though it reminds me of like an old far side where there's like two guys
walking by one's wearing a mac t-shirt and the other one's wearing a windows t-shirt and then
there's this other bearded guy who's walking by and he has like a unix shirt on and he flips a
quarter to them he's like here's a quarter kid get a real operating system that's the way like
c fills compared to like other languages like Like, yeah, I got to imagine like current C developers are just laughing.
They're like, oh, my God, you guys.
Yeah.
No, no.
I mean, in all reality, when I say the granddaddy, it is because it's the oldest.
But it's also you remember, you know, that time that you realize that grandpa is really strong.
I don't know how that happened.
So, you know, it's that it can be that analogy as well.
So here's one of the reasons why the confusion happens between the arrays and the pointers is
because when you pass an array as an argument, you're passing a pointer. It's not like in C
sharp or something where you pass by reference the actual object. It doesn't happen like that.
And then you're accessing those values in there using the pointer arithmetic, which is nothing more than a particular syntax that you're doing.
Wait a minute.
Reference is a pointer.
So in C Sharp, for example, you are passing the pointer.
You just don't have access to the pointer.
You're like, you don't, it's not copying an array to pass it off to another function.
It is passing the pointer.
That's what the reference is.
Pass by reference or pass by value.
But you're not using special syntax to get to those values.
There's no other alternative is the is the, is the difference there.
Whereas in C.
Yeah.
Okay.
Yeah.
Yeah.
In C you're actually having to do pointer syntax to get to the items in that
array.
Once you get it in,
whereas in C sharp,
you pass it in my array.
And then on the other side,
whatever that parameter name was,
you say in my array,
you know,
bracket one,
give me the first value in it or second.
And that, in that regard, um, off by one uh so so this was another interesting thing c arrays can contain
anything except void or functions thought that was kind of interesting never really thought about it
but yeah the um the, it was really weird.
Like you could have a,
I don't know.
I was just like, can you have a function that returns either void or null?
But I mean,
void or Inton,
I know you either returns one or the other,
right?
Yeah.
Well,
you don't return void,
right?
Yeah.
That's like the,
that's the absence of returning a value.
Right.
Right.
So, so yeah,
I'd never really thought about it, but apparently you can't do crazy stuff like just pass functions around or anything like that. Like you can do in a lot of languages nowadays. Um,
you pass a pointer to a function. Yeah. Uh, yeah, yeah. You can, you could totally do that. I mean,
that's what you did with the rays, right? I mean, basically.
I should learn C.
It's funny.
I actually ended up, while I was doing all this, I was on, it's ide.geekforgeeks.com.
And it's basically just similar to our.NET Fiddle stuff that we've done in the past where you can go in there and there's other Fiddles, they have like an IDE up there that you can do C Sharp, C++, C, Java, all kinds of things.
And so I was definitely in there tinkering around with these things, trying to make sure that I wasn't screwing up anything that I was about to tell you guys.
I want to go back to C because we could just compile it down we could still keep our day job still you know write web applications but just compile it down to like web assembly
but write it in c yeah how awesome would that be down with that yeah what do that
yeah i don't want to do that you don't want to use structs for everything like forget all these
classes man forget all that nonsense well how do you import modules pure enums i don't even remember you just do an include pound include baby is it like c install
no way that's chocolatey no chocolatey uh so check this out this this was something that i thought
was interesting so arrays can be initiated in any of three memory
segments, the data segment, the heap and the stack. I don't think I'd ever heard of the data
segment and I didn't dig into it that much, but those are there. Now, this is what I thought was
pretty cool. So if we talk about C sharp,
actually,
I have a question further.
Let's go ahead and ask that.
Where do I have it?
Did I have a pop quiz somewhere?
I could have sworn I did.
Okay.
Where,
where do C sharp arrays go?
C sharp arrays.
Let's say an array of integers.
Let's say an array of integers. would that go it goes in the heap
outlaw
I say that as if I know
it's been a long time
I'm going to say it depends on how you initialized
it right because if you
initialized it
where you were
able to pre, at compile time, able to allocate the size, then that's going to happen on the stack.
So like in your example where you said like int myArray bracket 10, right? Or where you like gave
it, or your example where you didn't say bracket, you just
said bracket, empty bracket, but you like initialize it to a specific size. In that case,
at compile time, it knows the size it can go on the stack. Otherwise you're going to get a pointer
that'll point that would eventually point to some location on the heap.
So that's what I thought. Joe's actually correct. Oh, really? Yeah. I was thinking
the same thing. I was like, man, if it's using value types and it's an array of value types,
surely it's just in the stack, right? Because there's no boxing, right? There's none. It goes
straight to the heap and you get a pointer to the heap. Every time. Okay. Every time. Yep. And C
sharp. I failed. I mean, admittedly, I did too. Like I just, I kind of made that assumption.
And so when I was reading this about C, here's the interesting thing.
It depends on where you create it, right?
Like if you're creating an integer array of size 10 or whatever inside of function, it's going on the stack in C by default, which is kind of interesting to me.
I just kind of assumed that carried over into C sharp. Um, what doesn't go on the stack is if you dynamically allocate that thing using Malik,
right? It will then allocate a chunk of space on the heap using however much you said you needed,
which by the way, you actually have to tell it when you call Malik, you're basically saying,
uh, what's the size of an integer times 100?
Because I want 100 of these things, right?
Allocate that space.
Yeah, and that's your array, and that's your space.
And that's why, again, C is so much faster because you're actually telling it almost at the hardware level, yo, I need this memory chunk right here.
Give it to me.
That's silly, though, right?
It's like, hey, I allocate a bunch of memory.
It's like, you know, here's an arbitrary number.
You know, it's not arbitrary,
but here's a number I derived that much.
And I'm just going to use it like an array
and you're just going to do it.
See, you're going to do it, computer,
because I told you to.
That's right.
I mean, it's complete control, right?
With ultimate power comes ultimate responsibility.
So, yeah, it's different than the way it works in C Sharp, which I thought was really interesting.
Static and global arrays get allocated to the data segment, which, again, I'm not that familiar with it.
I'm sure there's people that are listening that know way more about this than I do.
But that was kind of cool to me is just knowing that there's three ways that those things happen, just basically, basically either the type or how it's being declared.
Um, and I already said the local arrays get put on the stack. All right. So we started with
ultimately probably the lowest level one that I would feel comfortable talking about. Cause I'm
not ever going to talk about assembly cause I don't care. So, you know, that was kind of the heavy hitter. So now I thought it'd be interesting to
go to the complete opposite end of the spectrum, which is JavaScript, right? Which is where there
are no rules to anything. It's just a big open playground. You do what you want. So I think,
I think Joe Zach actually put this comment in here because i kind of built on
some of the notes he had things are loosely enforced at best and you'll find out as we get
into this a little bit that that is incredibly true so in c or in javascript everything is loose
yeah everything is loose right and this is where the TypeScript lovers are going to come in and be like, not if you use this.
Right.
So you can do something like this in JavaScript.
If you're using ECMAScript 2015 or above, you can say let my array equal new array.
And that will create an array.
And new space capital A array with open close print.
That will actually create you a new array.
You can also do the same thing much easier with just let my array equal open and close square braces.
And that will do the same thing for you.
That's what I do all the time.
It's the easiest.
It's the prettiest.
It's the most concise.
It's what you'll see in just about every library on the planet if you ever dig into the source code well if you do the other the
previous option new array some idees will actually like give you a little squiggle and be like no no
no no just do empty brackets man don't be silly like it'll seriously be a suggestion that's that's
ridiculous everyone's gonna make fun of you to do this yeah right we will yeah don't do it the way
that i said it first.
And then you can also initialize the array.
So you can say let my array equal square brackets,
one comma two comma three comma four, right?
And then that will create your array with the size of the elements that you put in there.
All right, so now here's some of the interesting things
about it, right?
We talked about C and the way you do it
is you say int my array and then you put 10 in brackets that's that's the size of 10 in javascript
you don't define the type or the length there is no did we say and see that everything has to be
of the same type i don't remember that i actually called that out so if you have an energy arrays
but you actually declare when you declare the way array normally that this is like a car array or character array.
This is the integer array.
And that's because in order for that math to add up, it all needs to be the same size, which means that if it's a value type like an int, those need to all be the size of ints.
And if it's a pointer, then it needs to all be the size of pointers or whatever.
Exactly.
And it doesn't matter that you could have types that would be the same size, like a car and a truck.
Those might be the same size in terms of memory space, but you still can't do it.
You have an array of type car or you have an array of type truck.
You can't mix the two within the array.
Now, to make things really confusing.
You could probably show it in there.
Good.
That's not recommended.
Well, I was going to say, to make things really confusing, though, technically what you're saying is 100% accurate.
The array, the size of the array would have to be, like as you said, of all the same thing.
But that's not to say that the things that are in there have to all be the same type.
Which sounds really weird. But what I mean by that is, and Joe kind of hinted on this, is that if your array was
just pointers, then the array is based on size of a pointer, but the things that those pointers
point to could be different. But if you do that, you're probably going to get runtime errors,
right? Well, you're definitely setting yourself up for you need to be careful.
Yeah. It's not what you want to do.
But you could.
Yeah, so arrays are supposed to be a single type in C.
In JavaScript, doesn't matter.
You don't have a type.
You can put whatever you want in that array.
Anything.
Like anything.
Null.
Undefined.
A class or a prototypical class or a string or a number, whatever.
Yeah, you know, it's funny.
Like I said, the example of declaring with like, you know, like var array equals 1, 2, 3, 4.
But what happens if you do array equals brackets 1, 2, 3, negative 1.
Works fine.
Does it? I'm trying that right now.
No, it totally does.
What Mike said, just kind of joking a second ago,
is you can add functions to it.
You can absolutely create an array
of nothing but functions.
Yeah, that was a bad example, sorry.
You could even do
var myArray equals empty brackets and then myArray.push1 myArray.push hello world.
Yeah.
Yeah.
So, yeah, bad example on my part.
It's not what I meant to say.
What I meant to say is like, yeah, you can do like bracket 1, 2, 3, comma string, the state of California or coding blocks or whatever.
It'll do it.
But what also you can do is you can do array at index zero equals this.
You can do array at index 14 equals this.
You could do array at negative three equals this.
Yeah.
So we're going to get into some of those edge cases.
Don't steal.
Don't steal what I'm going to do here.
All right. I didn't want to'm going to do here. All right.
I didn't want to like – well, okay.
Go ahead.
Well, because there was like one interesting thing kind of about the memory parts.
Because I think – I thought we had kind of hit on this one time before about like the different areas of the memory.
But I'll include a Wikipedia article because there are the different sections, the stack, the heap, the data, and the text portions of the memory, but I'll include a Wikipedia article because there are the different sections
of stack, the heap, the data, and the text portions of the memory.
Because when we talked about that before.
I don't recall.
I didn't remember the data one.
And stack and heap are very familiar, but that's the one.
Yeah, that's the one everyone talks about, right?
Yeah.
So here's something interesting about the array in JavaScript.
Technically,
it's nothing more than a special object behind the scenes that uses numerical
keys for sorting purposes.
That's all it is.
It's actually an object and you can prove this.
If you do a type of,
and then you just do an empty array,
it'll come back and tell you object like not making those print of this there's
only seven types that are built in and so object is one of them yep and so one of the other things
that happens because it is an object they're always copied by reference so if you're doing
something with an array and you're passing around the functions or, or you go to change something, you thought that, you know, Hey, var my array equals one, two, three, four, five. But then you
say, Hey, my array two equals my array one. It didn't make a copy of it. You have a reference
to that original one. So anything you change in that's going to be changed in both variables,
right? So that's important to know if now this is where things get kind of interesting and you just kind of have to know these things or know to go look for these things.
If you're using the array without breaking the rules, and we'll get into those in a minute, there are some built in optimizations like storing data and contiguous memory spaces.
So I want to caveat this by the articles that I was reading that I read for
getting some of this information. They didn't talk about the underlying engines that they were
using when making these statements, right? I think we mentioned it before. There's the Google V8
engine, i.e. had its own engines. The newest versions of Edge has its own. Mozilla probably uses their own.
Like there's lots of JavaScript engines out there.
So there might be some assumptions baked in here in what some of these statements are.
So, you know, take it with a grain of salt, some of this.
But it does try and do something similar to what C does to where if you just if you put things in sequentially, it's going to try and allocate a memory block so that it can quickly get to those things.
And you're going to get those optimizations by using that. So what's it mean to break the rules?
If you go in there and manually set a property on array using dot notation,
then you broke the rule. So if you set up your new array, my array equals one, two, three, four, five.
And then now you say my array dot first name equal Allen.
You just converted that thing to an object.
It's going to trash all its array optimizations because they go out the window.
It's no longer a sequenced set of data.
You've now turned it into an object.
So you broke the rule.
Yeah, I've read that the JavaScript engines,
at least the couple that I looked at,
will actually change the type underneath.
So it's like it starts out as array.
As soon as you break the rules, it's like, all right,
well, it's not an array anymore.
You don't get that.
Yep, it's gone.
It trashes it.
Another one, and I think Joe's mentioned this on previous episodes when we were talking about something.
If you leave a major gap
in between indices, so if you have an array and you set the first 10 items, but then you decide
that, hey, I just want to set item 1000 and you didn't have anything in between, it's no longer
sequenced properly because it's not going to allocate that much memory with a bunch of empty
items in between. And so you now lose that sequencing optimization on that as well.
Well, that's the example that Joe just said too, though, right?
Yeah.
Same kind of thing where it's, it's basically as soon as it starts out, like trying to do
like a dense array, which is kind of like the standard, like keep it on, you know, it'll
allocate some amount of space.
And then if you exceed that space, it'll grow, but it tries to treat it like a normal
array with continuous memory. But if you start cheating, you do the dot notation. Also, it's
like, all right, fine. You get some other data structure. You're no longer an array. Sucks to
be you. And I think they do the same thing like you're mentioning there where I read about dense
arrays and sparse arrays, which are basically the same kind of deal where it's like it starts out
doing the dense thing. If you start doing wacky stuff like putting indexes way far away, it's
like, oh, well, you exceeded the bounds of the little array that i hear that i allocated for you and uh there's nothing in
between so you you're not in the right anymore you're you're goofy yeah so the difference though
mike in what he was saying originally uh versus what i was mentioning is you create your array
with 10 with 10 things in there you don't dot notate the the thousandth you actually say my
array bracket 1000 equal something else.
Even though you kept in the world of arrays, you created too much of a memory gap.
And so now it's no longer they're going to be like, we can't we can't treat this like an array anymore.
Well, I guess where I was referring to, there was the example that Joe gave where he said, like, my array equals empty bracket.
My my array bracket one equals one.
My array bracket 14 equals negative one right like
like what's too much too many don't do much gap right so i mean i realized an example that he
said it was only like 1 to 14 but that's where i was thinking like yeah i read a little bit about
it um and what i suspect happens is like um when they first kind of a lot of times a lot of languages like C Sharp or higher level languages, when you declare an array and you don't give a fixed size or like C Sharp like the list objects, it will go ahead and say, all right, fine.
We don't know the size.
Let's just pick one, 1,000.
You get an array of size 1,000.
And then if you use more than that, it will say, okay, fine.
Let's go ahead and copy that array to a new array with size 2000
and if you exceed that i'll go ahead and and uh do that just keep doubling down 4000 and every
time it does that it doesn't change the size of the original array it has to create a new one
then deallocate the old and that's kind of a trick that um some of the languages use to have
really fast speeds like we have in arrays but not force you to pick a size up front so that's a
little trick that they use there but i think in javascript my suspicion not force you to pick a size up front. So that's a little trick that they use there, but I think
in JavaScript, my suspicion is that
they'll pick a size, say 1,000.
And if you exceed the size of that
initial array with
a big gap,
I don't know what the number is for the big gap,
just like you said, they'll say, well, you know what?
I don't want to allocate all this
giant memory again.
Okay, so 1 to 14 is probably not big enough is what we're saying.
Right, yeah, definitely.
Don't know what the number is.
I actually didn't see that.
I looked for it in a few places.
I mean, without digging into the source code of the V8 engine, I didn't find anything.
I guess a funny way to do it would be like,
if you exceed the size of the array that I would be growing to, then you're not an array anymore.
Sorry, you lost your array privileges.
Yeah.
Like, I'm not growing this thing like four times just to fit your stupid thing in there.
Well, without having data every step along the way, right?
And that's the key part because you can have a million item array if you wanted and as long as it was sequentially added
there, then you should still get the benefits of it being
a dense array. Yeah, so I mean it's like so if your
original array starts out at size 1000 and you say, let me go ahead and set an item
index 2001. It's like, you know what? Growing this thing once isn't
big enough, which means that over
half of this thing is empty i know that just by simple math so i know over half of your array is
empty without having to do any further analysis like you don't get into it anymore and by the
way compilers they're perspicity they're they're they take pride in shutting you down like this so
if you give them any opportunity to shut you down on something like this
and take away your array privileges, they will take it.
That's awesome.
Nothing better to do.
Yeah, well, you got to do something.
The next one is kind of interesting.
If you reverse add items to an array,
that will also cause it not to be treated like a regular array.
So if you have 100 items and you start at index 100 that will also cause it not to be treated like a regular race.
So if you have a hundred items and you start at index 100 and add them,
you know, reverse order, it doesn't like that.
Just keep adding them in order and you'll be fine.
So here's something that's kind of interesting.
So push and pop are array functions, right?
They work very fast. When you push something to an array, it gets added onto the end of it. When you pop it, it takes it off the end, right? Shift and unshift are extremely costly. So
if you're ever doing anything with those and you think it's a good idea to take things off the
front of it, you're probably taking some major performance hits because it's basically reallocating the thing every time you do it.
So super, super duper expensive thing to do there.
Something that's interesting that I don't think I've seen that much of, maybe outside of like the spread operator and the newmoscript stuff is using for of in an array.
You ever done it?
Either of you?
No.
Is that a way of looping without having to deal with the numbers?
You just go through each one?
It's an iterator.
It's an iterator for an array.
I didn't know that this existed for some reason.
We've all done the for loops for I equals zero.
I is less than array.length and et cetera, right?
They actually have an iterator in it that's called for of, and it's only for arrays.
If you use this on an array, then you'll get a fast iterator through it.
Technically, because an array is an object, you can use the for in but the problem is it's not a good
idea for a couple reasons one it's 10 to 100 times slower oh wow that's quite a bit slower
and two you can actually get items that you didn't expect because it is an object when you do a for
in you're looping through the keys of an object right and so you might get internal
properties of the object that weren't actually part of your array so you know the speed's enough
to stay away from it the other one's a good reason just not do it at all unless you want to shoot
yourself in the foot um so yeah next time you go to do a loop through an array check out four of
or maybe don't because it sounds like you're going to see stuff you didn't
mean to see.
Well,
no,
four of you get four of you get what you want for in.
Oh,
sorry.
Misunderstood.
Yeah.
Yeah.
Um,
so this,
this to me was crazy.
I don't think I'd ever thought about it.
Never considered it.
Length is not really a length.
It's nothing more than the largest numbered index plus one soak that in for a second that's ridiculous
what yeah so if you loop like four i equals one or i equals zero to uh length then potentially
you're gonna you're going to hit errors
because there's a whole bunch of empty stuff in there, right?
If you say my array 1000 equals 1, then your length is 1001.
That sounds like a good reason to use for of rather than for i equals 0,
i less than length.
It could be a very good reason, yeah.
But, yeah, that kind of blew my
mind right there is it's it's not like it's keeping track of things that you added to it
doesn't care just whatever the largest indices is plus one that's it um so yeah did that there
length is writable all right so in in most cases like you're not going to go into C and be like my array, you know, int 10 and then say, no, no, no, no.
My array.length is three.
That doesn't work.
It doesn't.
It won't work.
In JavaScript, you can do that.
And if you increase it, nothing really happens.
Like if you had an array of length five and you say, no, no, no, my array.length is 100, it'll do it.
I don't know if it's going to buy you, but it'll do it.
On the flip side, if you had an array of size 10 and you said, hey, my array.length is five, it's actually going to truncate those last five items on the array.
So a really quick way to actually empty
out array is my array dot length equals zero. It'll actually kill the entire array. So, so yeah,
length is writable. Again, don't know that I ever really thought about it.
You know, it's funny. I don't remember seeing that tip in JavaScript, the good parts.
We didn't write it.
Yeah. I don't really remember seeing any of this funky array stuff
in javascript the good parts i wonder if they intentionally skipped it because it's crazy
man there's so much nuts with with javascript that it's just really hard to
to even go into it i mean i i kind of glossed over it because we're going to be getting into
later but the javas JavaScript array is also a
queue because this whole notion of pop and, and, and push, those are queue type functions, right?
When you're talking about queues and stacks and that kind of stuff, like shift, unshift,
those are queue functions. So really the array object in JavaScript, that's really an object is your array, your object, your queue. Like,
it's kind of crazy. And I have one more pop quiz thing here. So, go ahead.
Real quick, I just wanted to, like, add something because I thought we had kind of
talked about this, but it feels like this came out, and it it probably off, you know, outside of any recording, but like the attributes of like JavaScript properties,
like there, cause you brought up the writable, but there's also like,
cause so you're talking about these rays. I was like, Oh,
let me go pull up the official documentation.
I'm kind of curious to see some of this, you know,
cause I was super curious to see like, okay, well, is there a default?
Like, you know, how does the? How does the default max sizing work?
And then I pulled up the actual ECMA standard on it
and specifically related to the length property.
It was talking about it has the attributes
writable, enumerable, and configurable.
And I thought that that had come up once before
in something that we had talked about. But
length is the number type.
And so that's why it has that writable
property. Right? But...
Yeah, so if they'd done a function call instead, you know, like maybe.getLength.
GetLength, right. I am curious
though, because like in the official specification
there is this thing that
because you were talking about like how you could quickly empty
out the array or resize the array
or like how you could truncate it,
but there's this thing where it says
attempting to set the length of
the length property on an array
object to a value that is numerically
less than or equal to the largest numeric property name of an existing array indexed
non-deletable property of the array will result in the length being set to a
numeric value that is one greater than the largest numeric property name.
Oh,
so even when you set it,
it's going to reset it for you.
So if you say dot length is five. Oh, well, that's what I was trying to, that's why you set it, it's going to reset it for you. So if you say.length is 5, no.
Well, that's what I was trying to – that's why I was like,
well, let me like over here real quick.
That doesn't make sense.
Try something.
All right, here we go.
To the console.
To the console.
Here we go.
This makes for great listening.
Everybody hang on.
Hey, this is science over here, so.
That's right.
I'm sciencing over here let my
array equal we're gonna say one two three four five and then i'm gonna say my array dot length
and i get five which is good now i'm gonna say my array dot length equal two and it says two now i'm
gonna dump out my array dot length yeah it's still two
so i don't know what they're trying to say i i mean it does wipe out everything after so if i
just dump out my array now i have one comma two which is great because it truncated off three four
and five which is exactly what we said and my length length is still two. So I don't know what they were trying to say in that thing.
Yeah.
Well, I don't either, but that's why I'm kind of confused by it.
But I was kind of wondering like, oh, well,
maybe if this is what they're talking about,
like maybe the default has to relate to the properties.
Because they say if it's less than or equal to the largest numeric property
name of an existing array index non-deletable property,
like that's a whole list of words.
Like what?
What did you just say?
Yeah, man.
You can't read any of those,
those major standardized documents for,
for any kind of those definitions for languages.
Cause none of them make sense.
It's almost like legalese for coders.
It totally is.
It totally is. That's funny. So I'm
seeing the same thing where I do it. If I set the length to
zero, then it basically wipes the whole array. But one thing
that I thought was interesting, if I do like
my array at one equals one, my array
at two equals two,
and I set the length
to one,
then I expect my
x at one to still be equal to one.
And let's see if it is.
Should be.
So my length is now equal to one.
Oh, you know what?
I'm an idiot.
It's zero based.
So by saying, yeah.
So, okay.
Yeah.
Mystery solved.
So, yeah.
I mean, it's working exactly like you said it is.
Cool.
Yeah.
There's, I just noticed noticed in this spec here,
because the Mozilla documentation takes you straight to the length property documentation,
but above that, they actually have the pseudocode definition,
and it is surprisingly long for this.
Let me share this guy.
Oh, shoot.
Let me copy that, and i'm going to paste that uh after we talk about like what kind of programmer you want to be uh like there's different
kinds you know like do you want to write the next uber like one could be like would you want to you
know write the next javascript i'll tell you what i don't want to do anything that involves specs right multiple pages oh man no
doubt like scroll up from that length i just sent you so that should take you oh no that does take
you straight to the the pseudocode that i was describing and then below that like that's 15.4.5.1
so if dot two would be oh good Lord. It's like a document outline.
Right.
It is.
No.
Like, look how far the nesting goes.
Yeah, that's ridiculous.
We're talking about the length, man.
This thing goes, how many levels deep are we?
One, two, three levels, four levels deep in the pseudocode.
This 3 has an A, has an I, has a capital A.
Right?
I'm seeing some Roman numerals in here.
That's bad code. Bad pseudocode.
It had to repeat.
The outline went so deep, the numbers had to repeat.
It's like,
well, I guess we're using numbers again because...
Alright, so
Outlaw made a
statement on the last podcast that i thought was awesome because
it's it's truly the way most of us operate is like man i'm writing new code i'm not thinking
about this i'm just gonna create a list or i'm gonna to list something or whatever right
i'm not using arrays why would i use an array so So it made me curious, like,
is there,
is there a big difference in performance in the list and,
and an array in C sharp,
right?
And so what do you think,
what do you guys think?
Are they the same?
They won better than the other.
I'm going to guess the same because I think that lists use arrays underneath the covers.
I mean, okay.
So in full disclosure here, I didn't realize that the link was part of it.
I thought that was something else.
So I clicked on the link.
Oh, come on.
But I can still say what I would have thought, but now it's totally tainted so who cares what would you have thought if you're going to be
honest about this well i mean i kind of said it last time like the arrays i would assume would be
faster but i'm not going to care about that like The writeability and the ease of writing using the list is going to be good enough until I know that it's not.
And that's when I would look at the array.
So I feel like I gave my answer last time.
Rays are probably faster.
So I actually have a link to a Stack Overflow article.
Apparently it's not ide.geekforgeeks.
What is it?
ide.geekforgeek.com?
I can't remember.
At any rate, I had run this through a test.
There was somebody that actually wrote the code to just sort of put timers on large arrays.
I want to say it was 6 million items uh, you know, items in an array
or in a list and did it. And it turns out that the, the array is almost twice as fast. Like when
you average out the number of runs, it's, it's quite a bit faster. So I'd be lying if I didn't
wonder if under the covers, if a list was using array, because that's what I was curious about when I went looking for this.
But I have my suspicions that the array would be faster, and it looks like it is doing some memory optimization and contiguous memory type things, so that it is quite a bit faster. Yeah, the reasoning wasn't very sound. It's like, oh, it's a list, I mean, an array under the cover,
so it should be faster.
It's like, how is it going to be faster?
If it's a stack overflow answer, if you scroll down a little bit,
the second best answer has an interesting table
that kind of talks about the pros and cons of array versus list versus linked list.
And surprisingly, list did win in one category,
uh,
which was,
uh,
lots of append calls.
And it says it's because of a pseudo append.
I don't know what that means.
I'm,
I'm guessing there's some sort of like fast,
like optimization particular to adding go.
That's different than arrays.
I don't know what that means,
but that's interesting.
Oh,
by the way, the IDE it's IDE dot geeks for geeks.org.
And so I just ran this and it's not quite twice as fast.
So this is just a one run.
So the list was 2084 milliseconds.
So, you know, 2.1 seconds and the array for that same loop, which was almost 6 million items is 1.6 seconds. So, you know, 2.1 seconds and the array for that same loop, which was almost 6 million
items is 1.6 seconds. So you say 0.4, which is probably about a 30 ish percent. So, and you know,
multiple runs will give you similar type results. So pretty interesting. Uh, it is faster user
rates, but you also lose a lot of the functionality, like just being able to add multiple things to it, right?
And it's the reason he said that he uses lists is because the same reason all of us use lists is because it's just so easy, right?
Oh, you need to add another item to it?
Okay, list.add, done, right?
Oh, wait a second.
I exited the length of my array.
Let me reallocate this thing. Right. Like it's just not worth the hassle 90% of the time. And if you're not micro-optimizing,
then it doesn't matter. So what else I got? Yeah. We're usually not storing 6 million items in our
lists either. So. Exactly. Exactly. So we mentioned mentioned earlier like in a strongly typed language
in your if you're in c-sharp if you're in java you know those type of things if you have an array
the items have to be the same type um and loosely type or languages like ruby javascript that kind
of stuff you can put whatever you want in there, you know, feel free. It's still an array though. If I have things,
if I have like an N 32 and index zero and an N 64 and index one,
is it,
am I still in array?
Wait,
were you talking about an,
it's strongly typed either one.
You can't do that in a strongly type.
Yeah.
So in a loosely one.
Yeah.
I mean,
it doesn't matter.
It's still an array because all it's doing is creating you,
uh,
in order to index, to get to those items.
It doesn't care what the items are.
It just wants to know that you've, you know, continuously added things in order.
I was thinking JavaScript.
I can do like array dot is array and pass it my junk, my junky array that started out as an array.
And I kept adding stuff to it.
It's going to tell me that it's still an array but kind of by the wikipedia definition of contiguous memory with
same sizes they can do point arithmetic for fast lookups like i've kind of violated the spirit and
the rules of this data structure and yet the function still calls it an array but i think
the difference here though is that like if you to compare, let's go back to our comparison of C versus JavaScript, right?
In JavaScript, like you said earlier, Alan, when you do the type of brackets, it returns back object.
That's what it is.
It's an object.
So that's why you're able to do the weirdness on it.
It's not like a true array versus in C, when you create that array, you're getting a block of memory that is going to be like, okay, you're going to have contiguous blocks that are going to be like, here's where your first integer is going to go, your second integer is going to go, and your third integer.
But it's not returning back an object.
Right.
It is an integer array.
I think with all this stuff,
like there's kind of like the textbook definition and then there's like the
real world.
So all this stuff gets a little kind of gray,
but it's not interesting to look at.
Yep.
So again,
we said in most type languages and arrays is going to be a fixed sequential
size,
typically in contiguous memory,
unlike JavaScript where you can just keep adding to it. Like I mentioned, it's very similar to a queue at that point. It's an array is going to be a fixed sequential size, typically in contiguous memory, unlike JavaScript, where you can just keep adding to it.
Like I mentioned, it's very similar to a queue at that point.
It's an array.
The syntax looks like an array, but it's sort of acting like a queue.
And then we have...
Yeah, I put a couple notes in here,
kind of like a fun little when an array is not an array.
We really hit all these, but just I was kind of like struggling with the definition
because I would read about arrays on Wikipedia.
It's like, oh, it has fixed size, it's contiguous memory.
I'm like, well, what about Ruby?
It's not a fixed size,
but then you read about how it's implemented under the covers
and depending on the implementation,
the various engines using like we mentioned with JavaScript,
it can be done different ways.
And so you can declare a JavaScript array,
treat it not like an array,
and then underneath the covers,
it's not implemented using an array anymore.
And still JavaScript tells you an array and you would be hard pressed to
argue that it's not.
I mean,
you can say that you're not happy about it.
This thought was kind of interesting.
And it was kind of interesting to other languages too,
because like Ruby has,
you know, I mentioned like dynamic arrays which do that kind of trick that c-sharp lists do where it allocates an array underneath the covers and whatever language
is written i think c and then it will dynamically kind of grow that and so if it needs to uh grow
the array it will dynamically allocate like roughly double the spot the space and then
copy the new stuff over to it and it's's just kind of funny that, like, C Sharp
calls that a list, and Ruby calls
it an array. And
conceptually, they're kind of the same thing.
And then
you have in here, what about Python?
Yeah, it doesn't even have arrays. It's like, you know,
we're just going to call them lists, like,
get over it.
You just have to do it.
And arrays are one of those things that, like, you kind of need some language support to, like, deal.
I mean, I guess you could build your own by contiguous memory and then having some way to index it, like, maybe a function.
But, like, to me, like, I need those square brackets.
Like, if it doesn't have square brackets, it's not an array in my book.
So, it's all syntax for you.
You don't care what it's really doing.
Fair enough. Yeah, it is. So I thought it was kind of funny
distinction. So I think it's like, if it's got brackets,
it's an array.
Unless it's a test table.
There's no hope. Anyway, we
mentioned how you can do goofy
things in JavaScript, like putting different types
in there, even using them as keys.
And we'll have a link in here to a nice write-up
on some of the different engines and what they use
kind of underneath the hood.
And just like Alan said, basically,
when is the JavaScript array not technically an array?
It's like when you start doing weird stuff.
And let's face it, you know when you're doing weird stuff.
Yeah, we're looking at you.
You know.
Well, I mean, there were a couple examples, though, that you don't know that you're necessarily doing weird stuff.
Right.
There were three that stand out as like, I don't know how weird you might consider that.
Like the gap, the big indency gap, that was one.
If you added a property to
it, well, maybe you could consider that one.
You've turned it into
an object. But it was already an object, is my
point. Yeah, but it wasn't an
object. No, it totally was.
Before you tip-type on
that keyboard and you're doing something weird.
You do type of brackets, you get back
object. So if it's going to be an object, then
fine. It should have properties on it. Why not?
But yet, when you do that,
like you said, it totally changes it, and it's
no longer the array type
of object, so whatever.
What was your third one that you thought was interesting?
The reverse one? The reversing
was interesting, but that's
totally...
Yeah, that was a weird one, because it's like,
well,
I don't know if you said this so did you like if you put a hundred items in the array and then reversed it you'd be fine okay you'd be
fine it's only if you had it if you hadn't initialized it and you tried to reverse it
that's when it was the problem okay if you If you created the array by saying index 100 equals, you know, A, index 99 equals B,
if you went in that order, it's not going to treat it like an array.
But if you added them 1 through 100 and then said, you know, array.reverse, fine.
It's still an array because it's sorted properly.
It's just the fact that it kind of goes back to that big gap
thing right so you started at index 100 when you didn't have anything before that and so it's like
yo i don't guess this is an array because you just gave me some random index so well i guess
it depends too like i would be curious to check it out to see the fit like um
does it have to be like too large a size?
Maybe if it's small enough, then it's like, okay, yeah, fine.
We'll leave it as an array.
But if you go too big, because maybe at some point it's like, hey, man, I really can't tell if you're trying to use this thing as an array or an associative array.
So I want to assume associative array, and then we're trying to use this thing as an array or an associative array so right i want
to like assume associative ray and then we're going to change everything and for anybody that's
ever heard associative ray that's just an object that's really all that is it's like a keyed object
so you know what you know what i you know what i hate
that's a good one i hate that the reverse function in javascript changes the array in place
so you have an array one two three you do uh dot reverse on it it changes it to three two one
and now it returns it too so if you were to do like you know var uh new array equals array dot
reverse you might think hey I got two arrays here.
But it's not true.
It's the same array assigned to two different variables,
and they're both reversed.
If you do something like stuff in React in particular,
this is where I kind of noticed it,
because I'd never really thought about it before.
I did something and I dot reversed the array.
Well, I noticed every time I appended to the array,
it would get reversed again. So every time that thing rendered, it would totally reverse the array. Well, I noticed every time I appended to the array, it would get reversed
again. So every time the thing rendered,
it would totally swap the order.
Why would you do that,
JavaScript? They're trying to save
memory, man. Don't modify
my arguments.
That's the best practices.
Wasn't that like one of Uncle Bob's
bad no-nos,
right? Like if you
changed the property that was passed in?
Yeah, I mean, I still do that.
But in this case, you're not actually passing in.
It's the object calling the method.
Hey, I put comments above it.
I'm like saying, like, I shouldn't do this, but it's okay because I put a comment.
That's right.
So a few other things to close out here on arrays because this is more information than what you thought you'd ever get out of
an array, right?
The maximum size in JavaScript is two to the 31st for 32-bit systems,
basically, is what you got.
That's roughly 4.29 billion elements.
Which totally sucks, man.
Why wouldn't they make it, like, why 32-bit, not 64?
So that's interesting.
So let's go ahead and jump into Java.
I was joking.
Java is the same, though.
It doesn't matter if you're running on a 64 bit architecture or you're 128 or you 256 bit.
Not that they exist yet or any of those.
They cap out at the 32 bit maximum, which is, you know, the 2 to the 31st unsigned, which is interesting.
There's actually articles all over the place out there of people that are like,
hey, I'm submitting this to the Java standards to say this is how they could implement it.
I don't think they care because they would have just done it already.
Oracle certainly doesn't care. Yeah.
I was totally joking because I think if you need, you know, more than 4.29 billion elements in your array, you might be doing it wrong if you're trying to keep that all in memory.
Like, I was totally joking.
Like, that should be more than enough.
You would think so.
But in C Sharp, if you wanted to, you know, test out your memory,
you can do it.
You can actually,
so maximum size by default,
the max size of an array is two gigabytes,
two gigabytes in a 64 bit environment.
That's,
that's a lot,
right?
You can avoid the size restriction by setting the enabled attribute of the
garbage or GC allow
very large objects configuration
to true in the runtime environment.
However,
the array will still be limited to a total
of 4 billion elements into
a maximum index and it gives these things.
So, kind of interesting. C Sharp
allows you to store a lot of data
in memory for arrays. But isn't that the
same as the Java?
The number of elements.
Yeah,
definitely.
They were both two gigabit and 4 billion elements though.
They're all,
they're all the same.
I could have sworn I'd seen something in C sharp that said it was bigger,
but no,
you're right.
They are the same.
So yeah,
it's just,
it's just that the C C C C Sharp documentation was referring to it in, like, size of the memory space of the array, not the number of elements of the array.
Yeah, so you can cram more in there than two gigabytes, but you're still limited to the 4.29 billion elements.
That's a decent amount.
And so wrapping this thing up,
when do you want to use this?
In type languages,
you probably want to use it
if you're trying to squeeze
every bit of performance out of it
that you can, right?
It's one of the very fastest ways
to store and access the data in memory.
And you're not having to move things around
in that array because that stinks.
Right.
And that's really the next part is, otherwise, want to use a list, link list, that kind of thing because they're more flexible and you have a lot more things you can do with them and you don't have to worry about the sizing of those things.
Well, I was going to add to that, though.
Like if you if you know that you're going to have a billion things in in memory
then you would want to choose an array but if you're only going to have like a reasonable number
like you know if you're if you're counting your things in like the hundreds or less then a list
is probably going to be just fine enough for you until you realize it's a problem but you know once you hear like
oh i gotta have a million or you know millions of these things in memory then it might be more
advantageous to use the array definitely unless you're like an n factorial algorithm and you're
just like looping over that thing over and over and over and over again. Well, then your problem is your,
your looping problem,
right?
Not the size.
I'm okay with that.
Cool.
This episode is sponsored by discover dot bot.
Now bots are everywhere.
Could you imagine, guys, seriously,
could you imagine Slack without bots
and the fun that that has?
Have you ever wondered, like,
well, how can I create my own bot?
Like, what does it take?
So Discover.Bot is an online community for bot creators.
Amazon Registry Services Inc. has created Discover.Bot
to serve as a platform-agnostic
digital space for bot developers and enthusiasts of all skill levels to learn from one another,
share their stories, and move the conversation forward together.
On its own, even a good idea isn't always as powerful as it could be. But when a good idea is shared, it gains strength and momentum.
It becomes capable of changing things in ways both small and large.
A good idea shared becomes an innovation.
Discover.bot aims to sit at the intersection of ideas and innovation.
They want to help people turn their experiences, discoveries, stories, advice, and knowledge
into part of a shared
canon that moves everyone forward. For veterans and beginners alike, Discover.Bot is a place for
learning, teaching, and talking. Head to discover.bot slash coding box. That's discover.bot
slash coding box to learn about how to get started on your next great bot.
All right.
So next up, I'm going to tell you about link lists.
And I am a big fan of link lists, even though they definitely have some problems and some
disadvantages compared to arrays.
And so I think they're much less common.
At least, you know, I don't certainly don't use them nearly as much as I do arrays.
But again, the big part of that is just because the languages I use,
like C Sharp and JavaScript, make it really easy for me to not worry
about the types of things that are traditionally problems with arrays, like coming up with that
size up front. I don't know. I mean, when we talked about
your favorite social platforms, LinkedList wasn't that far off. I mean, that's the
one, you know, when you're just trying to keep it
professional.
Nicely done.
I kind of feel like linked list is one of those things in computer science
where they tell you about arrays and you're like, oh,
that's really cool. And you're like, yeah, but
linked lists are cool too. And then
they spend one lecture trying to
convince you that linked lists are
better for some things and they give you a couple examples and you go back to using arrays
all the time and there's no doubt that they have some definite advantages but i just don't have
them come up that often so i definitely have a preference for arrays but today i'm going to try
and sell you on why link lists are also cool mario you gotta work on your salesmanship here, man.
Yeah, I'm going to make you feel sympathetic for link lists.
And what link lists are are basically ordered sets of objects that contain references to the next item in a list, kind of like a conga line.
So you think I've got my object and my object knows It also, you know, it knows about my object,
but it also has a pointer to the next object in line.
And so typically in code,
you would kind of have like a wrapper class or something where like a node
class or has a reference to the thing you actually care about,
like the person in the Congo line.
And then you would have a pointer called next.
And in some cases,
you can have a double linked list where you also keep track of the previous and we'll talk about the advantages of that and then that
lets you go backwards and forwards in your list really easily and it has some uh some particular
advantages over arrays so what's the deal with the double? So the cool thing about having a double E-linked list where you've got a pointer to the person in front of you, the person behind you in the conga line, is that if you only have that person, you've taken that person kind of out of the context of the list.
And you know that they're in the list or in the conga line, but you're only looking at that one person.
You want to take that out of there, then you can easily say, hey, get your previous person, say it's next to
your next now. And in that one line, you've cut this thing out of the list. That's not something
you can do in array. If you've got an array of 100 numbers, you can't just snip out number 49
and then still be able to loop over it. It's not going to work, right? You're going to have to
shift everything down one.
That's terrible.
Well, you can in JavaScript.
Yeah, I mean, you can in JavaScript.
I feel like that's going to be like an asterisk for every conversation we're going to have
while we go over data structures.
Well, you can do it in JavaScript.
Yeah, it's good.
Star, you can do it in JavaScript.
Star, star.
Nothing is technically anything in JavaScript. So we're
not even really talking about the same things.
All right, go ahead.
Sorry.
So that was the deal with the double.
And
so with the double, you say you can snip
it out. You cannot do that with a single.
Yeah, you can't do it with a single in the same step.
So if I grab the one person
out of the conga line, and so it's a you know, you can't do it with a single and the same step. So if I grab the one person out of the conga line and so I say, you know, hey, Jamie, come over here. You're out. I can't do that without knowing specifically who the person, the previous person in the conga line was and who the next person was. And if I only knew the next, then I can't do that. So the only way to remove
an item from a singly linked list is to say, you know, when you only know the item and say,
okay, we'll tell you what, let's loop through that list and keep looking at the next value.
And the next value is equal to Jamie. Then take Jamie's next and set it to my next. And so now Jamie is cut out.
So it was just kind of a convenience thing.
And like,
ultimately the problem is the overhead of you having to maintain those double
values.
And so link the double link lists are not really that,
that common.
I think it's just kind of like a weird little exception that it's kind of nice
to have.
And if you are using something like a linked list class in C sharp
then there's a good chance it's doing a double linked list because kind of why not it's ultimately
you know it's one more line of code it doesn't significantly impact the actual
runtime like the big o time of adding a new node because it's just a constant operation so why not
so if I were writing a linked list today i would probably
not think about doing it double but really there's not a compelling reason not to other than if you're
doing this from scratch for some reason now the pros of linked lists are one you don't have to
specify a size up front which means no over provisioning right you don't have to specify a size up front, which means no over-provisioning, right? You don't have to worry
about creating like a new link list or sorry, a new list in C sharp and having an allocated array
of size 1000 underneath the covers. And you're only using three of them. So that's really nice.
And it's also, there's no need for contiguous memory, which is really great if you have like
really big lists. And so you don't have to find this huge chunk of memory that's all in one spot. You don't have to like, what do they call it?
Windows defrag your memory just in order to get your list in there. So you can have these things
hopping all over the place. That's really nice. And then it's easy to insert and delete. I mentioned
like the Congo line example, where if you know the previous and
next, then it's really easy.
Otherwise, you just have to loop through,
find the appropriate...
I keep wanting to say parent, but it's not a tree.
It's really similar
to how one's set up, but
it's just a one-dimensional.
This is the simple.
This is what trees are built on.
This is the
progenitor
and it because of um that ability to snip out and actually to add things in the middle too
it's really nice too so like you know array we mentioned those shifts and um was it shift and
shift unshift unshift those really stink in arrays because you have to move everything over
which means looping through the entire array and say, this equals that, this equals that, this equals that, however many times.
In a linked list, it's really easy to say, like, okay, you want to add a new item to the beginning of the list?
Like, okay, here's my new node.
It's next equals the first item in the list currently.
You know, hey, while we're talking about this, because I really liked the conga example.
So for those who aren't familiar with either of these, let's go with the conga example for the singly linked list.
So both of your hands are on the person in front of you.
So both of your hands only know about that one person. But think of a doubly linked list as you're holding hands,
one hand, your left hand is holding the hand of the person behind you and your right hand is
holding the person, the hand of the person in front of you. So each hand knows a different
person. So if you wanted to remove yourself from the list, you just combine your, you know,
take your two hands and, you know, give your partners each the other partner's hand, right?
And now you're out of the list.
Does that sound like a good visualization for those following along?
Yes.
It's like you say, Jamie, you're out of the conga line.
In the double-linked list, it's like, okay, fine.
You basically clap those other two hands together,
and then you're out.
Go have a seat out of the conga line.
I have a question about this because I remember going back to college days when we were doing java and it's like you
said you start with arrays and then you kind of end up in linked lists are these still like a big
thing is this still um a data structure that's used a lot in something like java or have they
gone on to things like vectors or whatever it like i'm just curious because like c sharp i
didn't even realize they had linked lists until we were looking at this topic because it's just
is it a data structure either one of you guys have gone to in c sharp i mean i i've well for one
i've only ever used it as like for school and interview kind of questions like that's the
only time i've ever had it like really be a thing.
Right.
Okay.
And I did go back and double check too,
because I was curious and the C sharp linked list type is a doubly sorted
list.
So I,
that's not to say that it doesn't have its place.
I've just never found myself in a situation where I'm going to have an
object and I'm like,
Oh,
Hey,
what was the previous one? Because typically I'm just already have an object and i'm like oh hey what was the
previous one because typically i'm just already in an array and i would like know where i am so
i'm like i just go back minus one you know yeah uh unless you're doing some pretty like serious
algorithmic type stuff i don't think there's a real strong compelling reason you know i do see
trees come up a lot like in in like-world applications as well as interviews and whatnot.
And it's so similar.
Like, the difference between a linked list and a tree is, like, how many children you have.
Like, it's not just the next.
And so, really, the difference between a double-linked list and a binary tree is kind of just convention, right?
It's two pointers.
Well, I don't know if you go
with the conga line thing it's basically you'd have multiple lines forming behind you really
i mean that's yeah so conceptually it's different how you do it and it doesn't make sense to like
you know use a linked list for a binary tree because you like you can't snip out the middle
like what does it mean if you just snip out one of the nodes like it just gets kind of weird
but it's just kind of funny to me. Like that,
the way you would program a linked list and the way that you program a tree is like nearly identical.
Just one has like children and a link just has one.
Okay.
Yeah.
I was curious.
So I was,
I did some quick,
uh,
Googling to see like,
Hey,
what are some real world uses of the,
uh,
of a linked list?
And the one that has the most upvotes on Stack Overflow was a FIFO data structure, a first-in, first-out.
Okay, that makes sense.
Yeah, because you want to be able to pop the first thing off of the list all the time, which in array we mentioned is really slow. But if you want to get the first item out of a list,
it's like, okay, well, list equals the head,
which is the first item, dot next.
And then yank it out, right?
That's it. You're done.
Okay, cool.
All right.
Sorry, didn't mean to hijack you with the random side.
What you got?
No, it's all good.
No, I think the thing I like best about it
is basically just that it's elegant.
You know, there's things that are ugly to do in array, like moving those things around or the shift, de-shift, or un-shift, that are just really elegant and feel nice in lists.
So that's cool.
And the over-provisioning thing is actually a pretty big deal.
If you're declaring a lot of lists in C Sharp and not linked lists, list T, then it's going to be over provisioning like a
lot. And so it's kind of, kind of gross if you're doing a lot of it, but it's also gross to do a
lot of it anyway. So good luck now, the downsides. So no random access. I can't say, give me index 14, right?
Even telling you the length of a linked list is a bit of a problem.
So you'll see data structures that solve that.
They'll say, okay, this is a linked list, but we also have a special property called
length and we just keep track of that sort of thing.
But that also means that you're restricted how you can interact with that list.
And so you can't just go and say, hey, Jamie, you're out out now you have to go through some other sort of interface that's going to keep track
of that number so you start to drop the advantages of it being a list which stinks so yeah if you
want to know the length of a list you got to go through the conga line you know count the heads
and uh that's pretty much what you're looking at and you can't just say go to number 14 because
all you keep track of is really the first person in
the list.
And you have to iterate through it in order to get to the 14th one.
So that's up.
That's on you to do.
So that stinks.
And it's also a terrible choice for some algorithms because of that,
like a binary search.
Remember that's the one where you like flip open the dictionary to the
middle and it gives my,
the word I'm looking for greater than or less than let me flip to the middle of that. And you keep going down. So, that's the one where you flip open the dictionary to the middle and it gives the word I'm looking for greater than or less than.
And we flip to the middle of that and you keep going down.
So even with a very
large dictionary,
you can get to the word you're looking for
in a logarithmic amount of steps.
A very tiny amount of steps.
Terrible for a list because even to get to the middle
of the list means, well, I got to kind of go
find the length of the list.
So let me
get to the end. Go back. middle of the list means well i gotta kind of go find the length of lists so let me do to do to do
get to the end then go back and then go to the middle so in arrays each one of those is an add
add add so over one step so they're basically constant time they're nothing every time we have
to navigate somewhere in the list it's like a O of N operation just to get to that node.
So terrible for binary search.
And there's a little bit of extra memory required there.
Like you think of the array, it's super efficient, right? We allocate, you know, the size of int times however many elements you want to have in
that array.
So linked list, you're going to have an integer, but you're also going to have a pointer to
the object that wraps the integer and you're going to have a pointer to but you're also going to have a pointer to the object that wraps the integer, and you're going to have a pointer
to the next object,
next thing in the Congo list. So,
compared to an array, you know, just that example
there, it's three times the amount of memory
that you need
compared to the array.
So, that kind of
stinks.
You know, I was thinking back to
my comment a moment ago because i was like
when i mentioned the fifo and i'm like no even if it was like a lifo uh you can still use a linked
list and then i realized like oh i meant to like check for real world uses for doubly linked lists
and i accidentally used the google you know uh suggestion when it came up with like lean list.
I'm like, yeah, of course.
You know, I'm trying to do two things at one time.
Yeah, that's what I wanted.
But the real world use for the doubly linked list,
the accepted answer on Stack Overflow was as it relates to operating systems,
that that's how you can like move processes from one queue to another.
Like, you know, it's used there to know which ones are being blocked
and which ones aren't and which ones to release.
So still very academic.
I mean, if you're writing a lot of operating systems,
then I probably insulted you with what I just described of it.
But you're like, of course that's where it's used, Outlaw.
But yeah.
Yeah, it's pretty cool to know just like given this process,
like go get me the previous one or go get me the next one.
If we were dealing with an array, first of all,
we'd have to set a max cap like, okay, well, I don't know.
17 is the max number of processes.
And then in addition to having our process,
we'd also have to know its index in that line.
And we'd have to know the max length
so we don't try to go next.
And so it's just extra stuff that you have to do
that gets a little nasty.
So if you find yourself trying to keep
the index of your item in the array
in addition to the item in the array,
in addition to the length of the array,
if you're looking at those types of things,
then maybe you should be looking at doing a linked list
instead.
Another reason, there's three
reasons I basically found for when to use them.
Lots of inserts and deletes. If you're doing
things in place with an array, you have to bump
them all. So if you're doing like
inserting things into the middle of your list, this
is great for that.
If you don't need random access, you don't need to fix
size.
That's when you use linked list.
Or you have
to start at the beginning.
Yeah, it stinks.
What about languages that don't have
pointers that you can
use? JavaScript doesn't have pointers,
but there's nothing stopping you from using
linked list. You basically just have a reference to your
next object.
So an example here would be like var node1 equals the brackets, value equals 6.
And you do node2 equals brackets, value equals 7.
And then you do node1.next equals node2.
And you just made a simple linked list.
You don't even have to keep track of node 2 anymore.
You can just set your var list equals node 1.
Done.
That's pretty cool.
C Sharp has a linked list class, which is really interesting because my first kind of thought was, well, that's kind of silly.
I guess, you know, why not build it in? It's something i could build in easily and do a generic support through t so it's
something that it's would be really easy for your program but then i took a look at the actual
methods they have on it they actually have a lot of really nice stuff that just make life easier
for you so if you're going to do a linked list and see you might as well use the linked list class because it has methods like add first, add last, remove last, add after elements.
And all that's doing is stuff that you'd be doing manually.
So it's not doing any rocket surgery or anything there.
If you say add after and pass the number three, it's going to go, all right, let me start at the head, 0, 1, 2, two three and then add it after that so it's not
doing any less work it's just done for you so that's nice and they probably did a better low
level low level type job than you're going to do so you might as well use it have you done much
rocket surgery no that's okay just just curious all right i went uh I went to high school around that time that one song came out that don't
impress me much.
Oh, wow.
The what song?
Is that Shania Twain?
That Don't
Keep Me Warm in the Middle of the Night?
Yeah, that is Shania Twain.
That is?
Look at that.
Don't act like you didn't know who Shania Twain was.
I don't know about the song.
I didn't know the song.
That don't impress me much.
Yeah.
Oh, wait.
How did it go?
Oh, maybe I have heard of it.
Don't get me wrong.
I think you're all right.
That don't keep me warm in the middle of the night.
That's right.
That don't impress me much. Oh, as you guys
sing it, now it's starting to come back to me. That's what did
it. Yeah.
It's the froggy voice along with that.
It's winter over here in our hemisphere.
So, you know, you think about
these things.
All right. So, linked list
first list. Hopefully, it hasn't
been too confusing because they sound so similar.
And list is actually backed by an array, as we mentioned earlier.
And linked list is backed by a linked list.
And linked list can cheaply add or remove items out of the middle.
And list can only cheaply add things to the end.
So that's about it for linked lists.
Coolness. All right. end so that's about it for link lists coolness all right and it's now that time of the show where we do what we do best we beg for for you to take some time out of your day and leave us a review if you haven't already please
do for those like one of the ones that just recently did it who left us a review several months back and then came back and left us another review you know super thank you for
taking the time to do that i mean it's a it really does put a smile on our faces it it keeps
encouraging us to keep going forward because i mean you, you guys, guys, gals, you, you write it
and tell us, you know, how it's helped you and, and, you know, you've advanced and you're switching
careers or whatever. And it's just really nice to see that. And, and it helps other people find us
too. So, uh, if you would, you know, head to codingblocks.net slash review and hit one of
those two links up there to either take it iTunes or Stitcher and, you know, drop us a few words
and, and, you know, put a smile on our faces yeah i'll make you a deal if we can get to
one percent of subscribers leaving a review i will get a jquery tattoo
are you gonna tell them how many subscribers we have or no outlaws
outlaws gonna tell you wait a minute wait minute. I see some crossed fingers here.
Hold on.
Now it's my time for my favorite part of the show.
You totally had your fingers crossed.
Hold on now.
Survey says.
Wait, wait, wait, wait, wait, wait, wait, wait.
You're not getting away from this tattoo business because don't you already owe us a Visual Studio tattoo?
No, I got that tattoo.
Yeah.
Okay. You're going to have to tune in the video to see this thing. A Visual Studio tattoo? No, I got that tattoo. Yeah, okay.
You're going to have to tune in the video to see this thing.
Go subscribe on YouTube.
Can you show it?
Leave a review.
All right.
Well, with that, let's head into my favorite portion of the show.
It's The Survey Says. I didn't say it right that time.
Yeah.
Survey says.
All right. So last episode we said, who is your favorite virtual assistant?
And your choices were Alexa, the OG of assistants, or Google Assistant because accuracy matters, or Siri, who needs accuracy with looks this good, or Cortana, hipsterific.
I only like the things no one else likes.
All right, let's start Joe.
You go first.
Tell me your pick and what percentage you think it had.
All right, I'm going to go with Siri with 35%.
Got aggressive on me.
All right.
Man, I'm struggling on this one.
I don't know whether people are going to assume
when you're talking about virtual assistants in their phones
or these little gadgets that you put at home now.
But those are also virtual assistants.
I know.
I know.
I'm going to say Google Assistant because accuracy matters. home now but those are also virtual assistants i know i know i'm gonna say google assistant
because accuracy matters and to be fair i'm gonna go 35 also okay so joe 35 siri alan also 35 but
google right all right i feel like joe is trolling me here. Like, what are you doing, dude?
Seriously?
Is that exactly right?
You picked Siri?
Yeah.
Like, seriously, Siri?
Nobody likes Alexa.
Okay.
No one's heard of Cortana.
Let's just get this out of the way.
Google Assistant doesn't even have a human name.
Obviously. have a human name obviously i didn't mean to like right as you right as you said it like
you know jokingly laugh when you said it but like involuntarily it came out so i apologize
for laughing at your your answer but alan won let's just go ahead give it out of course it
was going to be google assistant Why would it be anything else?
But how high was it?
Although, yeah, you kind of underrepresented what you thought it would be.
It was like 65% of the vote.
It was right away the winner of that survey.
And it makes sense.
It's the best one.
There's no question.
Man,
I'm telling you, you could ask Siri to play a song and it's going to create
a calendar entry for you
or something like that.
Like setting alarm for 4am.
No, I didn't say that.
I wish I could remember which song it was.
It was so frustrating because
I was with my kids and there was some song and I asked Siri to play the song and it couldn't find it.
Even though it verbatim repeated the song and artist because I'd said both.
And it was like if I flip-flopped the order, then it understood when it was able to find the song.
I'm like, man, come on.
Dude, I will tell you, Alexa is amazing at setting kitchen timers.
But outside of that, I just, I give up.
I'm sorry, I didn't understand the question.
I'm like, of course you didn't.
Right.
Yeah.
I mean, that's the thing google the google
assistant their their its ability to understand parse your speech and understand it and translate
that into a meaningful action is incredible and unreal even though like alexa might have been the
original in terms of like the dedicated device assistant,
because if we're talking about phones, then it wasn't, right?
Because in that case, it would be Siri.
But yeah, it doesn't compare.
It's not nearly as good, in my opinion.
And apparently, also in the survey, it was second place.
But man, it's such a distant second.
Such a distant second.
And Siri, by the way, was last place.
Oh, really?
Yeah.
It was last place.
And I say that as a diehard iPhone user.
Right.
You know, we've talked about like how much even like we had had that episode where all of the things I was excited about
coming out were like, oh, hey, I could use this with Google Assistant and everything.
Come on. So tell me this, though, just to round this thing
out, do you have the Google app installed on your iPhone and you use
the Google app for asking things? I know you don't have the convenience
of holding down the button and saying, hey, Siri, but do you use the Google app for asking things. I know you don't have the convenience of holding down the button and saying,
Hey Siri,
but do you use the Google apps voice thing?
I have that app,
but I don't really use that app now.
Okay.
But I mean,
I have like all of the Google apps,
uh,
you know,
I mean,
I,
well,
they,
they have a lot,
so I'm not going to say all,
but I mean,
I have like a dozen Google apps. Let's say, let's call it. Well, there's sunsetting them all next month. So I'm not going to say all, but I mean, I have like a dozen Google apps.
Let's say,
let's call it.
Well,
there's sunsetting them all next month.
So yeah,
they sunset 12 a month.
But that you say that,
but like you could have a Google,
like this just recently happened like over the summer.
I think it was like there was a Google news app and they're like,
oh,
well we're switching it to this Google news app.
And you're like,
what?
Why didn't you just upgrade the one that was already installed,
man?
Like,
I don't understand why you had to like, why didn't you just upgrade the one that was already installed, man? Like, I don't understand
why you had to, like, it's the same functionality,
they're the same names,
and yet you decided,
nah, this needs a new SKU. It was a different
department, man. It was a different department.
We didn't realize we already had a news app.
You know, you're probably
not wrong.
Yeah.
The Google apps are just too amazing, so you can't not use them but i was
surprised to see that cortana beat out siri that one and i was like really it kind of hurt a little
bit no no no because like i said as the diehard iphone user like i was like of course it's google
so no no no hurt feelings over here at all oh That's amazing. So what we got on tap for today?
All right. So today's survey is, you know, we're rounding out the end of this year. We're wrapping
up 2018 here. So 2019 is coming sooner than you might think. And if you haven't already been thinking about what you might want to set for your 2019 goals, then you should start. And so if you have already
started thinking about those goals, like what do you want to focus on improving in 2019?
So your choices are front end. There's a three-piece service for everything now,
or for those that want that spelled out, that'd be a 3P service for everything now. Or for those that want that spelled out, that'd be a third party service
for everything now. Jamstack.
Jamstack 2019.
Back end because
Flexbox done. Persistence
is king. Data, data, data.
Or algorithms and data structures, can't go wrong with the fundamentals.
Clean code, master the tactical before the strategic.
Or architecture, I've ascended to higher levels of abstraction.
And lastly, DevOps.
Good luck doing anything without me.
I'm looking forward to these.
Yeah, this is going to be an interesting one.
Yeah.
I was going to say like where you think it was going to be, but then I was like, no, we don't want to do that.
But we did ask that last time, and I said I didn't want to give it away for the virtual one.
So obviously, you can guess now that I thought that Google would be the winner.
Yes.
Very nice.
Well, I'm buying Jamstack 2020 on.NET right now, So we'll put a new site up there.
That's got my vote.
Man, get out of here.
Get off my lawn.
This episode is sponsored by
Maining Publications.
Now, I know I don't have to tell you
about Maining Publications.
You already know Maining Publications.
You probably already have
a dozen Maining Publications books, at least on your bookshelf. I know I do. Some of my personal
favorites we've discussed many times on this show, The Art of Unit Testing. I love this book.
We've talked about it so much. C-sharp in depth. What does John Skeet not know about C Sharp? Is there something?
Real World Machine Learning is another one that I don't know if we've talked about it a lot, but that's a book that I really did like.
Just so many great books by Manning.
Yeah, I want to mention too, we brought it up a few times, but our buddy Zach Brady, friend of the show, the reactionary, he's got a course up there, React in Motion. And I've been going through that lately.
I'm actually in the process of going through it again.
And what I really like about it, in addition to his flair and interesting video editing choices, which are really awesome.
I like when he pops in and out of the left and right.
So he kind of exits stage left.
But in addition to watching the video, and you can set the speed on it too, it actually has text.
So I can read along while I'm watching.
It just kind of helps it sink in.
And I'm actually eyeing the book Kafka Streams in Action.
So if you go to their website, you can search and you're going to find books on all sorts of subjects.
Very cool.
And Manning is running a special promotion through December.
The countdown to 2019 will run on Manning.com all the way through the end of
December. Answer just a single question every day and you'll be in the running to win free eBooks,
videos, and even a whole year's worth of new releases. Plus every week, everyone will get
to enjoy massive discounts on Manning products. All you need to do is sign up to Manning's deal of the day at www.manning.com
slash mail dash preferences, and you're good to go. Again, head to manning.com slash mail dash
preferences to sign up. And also while you're there, take a moment to shop around for your
favorite books. You've already, you already have your favorites. Now find your next favorite book from Manning and use the code CODBLOCK40 to save 40% on your next favorite book.
That's 40% with the code CODBLOCK40.
All right.
And so jumping back in, the next data structure we have to talk about, and it's funny because I say data, he says data.
What do you say, Joe?
It depends.
Data?
I mean, it's Star Trek.
It's data.
It's data.
All right.
And that might be why I say it like that.
I don't know.
All right.
So the next one up is a Q.
And by say a Q, I actually mean a queue and then all kinds of derivatives of it.
So what is it and how does it work?
Well, it's very similar to a stack.
And ironically, we arranged all our show notes.
So you don't get that up front.
So the biggest difference is the order in which data is removed.
And that's the key part here.
So queues are FIFOs.
That means they're first in and first out.
So the oldest items get removed first.
And I put an analogy here.
Just think about your school lunch line when you used to get in line, right?
If you're the first person in line, you are also the first person out of that line. So you get served first, you leave the line.
It's useful. It's primary use. And there's not a lot to say about it. I mean, literally,
it's just, you add stuff to it and things get, you know, you put things in and then they just
come out. So they're useful when ordering is important via the FIFO.
Breadth first searches. So that's interesting because if you look at the trees, right,
you have one at the top and then two, three, four on the next level of five, six, seven,
eight, whatever. So it's actually going to go through each level doing the breadth first search.
Resource sharing among multiple consumers. So if you think about subscribers
to events or something, the first person or the first item that subscribes to that event is going
to be the first one that gets notified and it does its work and then the next and so on.
Asynchronous data syncing. This was interesting. So they were basically talking about when you have like networks and the data that you send is not necessarily received at the same rate.
And so you're still getting the first piece in and operating on that as other data is coming in.
Right.
So even as it's building up, you can't do it out of order.
So I was just trying to announce my ideas here.
So I thought through like,
um,
you know,
when they're useful,
right?
So some,
some add ons to what you said.
So,
uh,
event buses or event queues,
right?
Like you,
you want to send out a bunch of messages,
a bunch of things just happened either,
you know,
because of user interaction or because of, uh, you know, other, other things within the system. Right.
And you want to fire those events off. But, um, you know, as the, as, as listeners to that queue,
right, you want to get them in the order that they happened, right? You don't want to, like, if you're taking input from the keyboard,
right? And you were typing in your password as P-A-S-S-W-O-R-D, you want to make sure that it
came in as like, if you're listening for that queue of events, you want it in the order that
it happened in order to be able to like, you know, validate it. Right. And if that is your password, then we need to talk.
And then the resource sharing idea,
which is kind of similar to where I was going with the event bus in the
deli in there or the event queue is we've talked about delegates before.
So when you in like C Sharp, for example,
if you had a delegate to handle your functions,
like you're literally just adding on to like here,
you're appending to a list of like,
here's the things.
And when the event fires,
when you call that delegate,
it's going down the order through that queue
of delegates to call each one.
So like, yep, now it's your turn to process this thing.
Now it's your turn to process.
Now it's your turn to process.
Yep.
Hey, I got another one too.
Actually, I just found out about this from the Syntax FM show.
Have you guys ever heard of FIFO bottles?
No. FIFO bottles? No.
FIFO bottles.
So these are the types of bottles that you'll see a lot of times at like restaurants, like Subway, for example, Subway Subs, where there'll be a bottle where the, like I said, mayonnaise or ranch dressing or ketchup, where you squeeze the stuff out of the bottom, right?
So you pick it up, you hover over your sandwich, squeeze, stuff stuff out of the bottom right so you pick it up you hover over your
sandwich squeeze stuff comes out of the bottom and if you want to put new stuff on top like
ranch whatever you unscrew that the top of the bottle and just put it right in the top
what's nice about these five o bottles is that the oldest mayonnaise gets used first so you never
have to clean this bottle you never have to clean this bottle. You never
have to throw it away. You just keep pushing
the new mayonnaise on top
and you always use the older stuff first
because it's FIFO. It's a FIFO
bottle. I like it.
Yeah, so you can get like a...
We'll have a link here, an affiliate link if you
want to buy some FIFO bottles.
They actually look pretty nice. It's kind of gross to think about
never washing them. You should probably wash them once in a while but you know the restaurants don't
it sounds like the idea there is that you can use these things squirt squirt and it's always
going to use the oldest stuff first so it's uh less chance of spoiling i always wondered why like
um like subway was the example you gave like they did they do have like weird looking bottles and
always wondered about that.
I never thought about it being a FIFO squeeze bottle.
Yes, because they don't clean them.
Right.
Yeah, so I'll never eat there again.
Thanks.
Of course, Taco Bell is one of the valued partners that uses this thing, so great.
We can't have nice things.
Just because you don't know how the sausage is made
doesn't mean it's better anywhere else man that's true all right so the next thing up that we have
is one of the offshoots of a queue it's called a priority queue and it's just what it sounds like
basically it's similar to the standard queue except each item actually has a priority associated with it.
So the higher priority the item is, the faster it comes out or the more precedence it has to be pulled off the queue first. So the highest priority want to go first. And then if all the
priorities are the same, then it falls back to the standard queuing method, which is the FIFO. So it's useful when you're doing certain algorithms like Dijkstra's algorithm.
When you're trying to find the shortest path between things, it can be useful as a data structure for that kind of thing.
And if you think about that, if I remember correctly, you have all the links and you're trying to take the mins or whatever.
So you just assign priorities to those.
Data compression, apparently it's used in the Huffman algorithms.
They say that it's also useful in AI and machine learning type stuff.
I didn't dig into it too much.
There's actually a huge list of things that priority queues are used for.
And I have a link in the show notes for that.
So that one's an interesting one.
It seems to be used quite a bit.
When we talk about breadth-first searches,
you need some sort of external data source or data structure
so you can kind of keep track of basically the things
that you need to talk to next.
And they talked about using a priority queue because, you know,
we talked about this example of going through your family
to ask for money.
And so it's like, I'm going to ask for you, and for you and then you and you and you and then i'll come back
and ask your kids if you guys don't hook me up so i don't know that we covered this when when you
were given the brief description of the the priority queue but like if you had say 10 items
that were all like the low whatever then let's call it normal priority is, and then you add an 11th one and it's got a higher priority level,
then instead of pulling that first one off of the queue,
even though it's first in the queue,
you would skip that one and the first 10 to go to that 11th one
because it has a higher priority.
Right, right.
It's always going to be the first thing out, whichever one's trumping it.
And I got a really good real-world use case of it.
You do?
What is it?
Processes.
Like, have you ever been in, like, on Linux,
you could change the process priority just by doing, like, a nice on it?
It's like, yeah, okay, fine.
You can play nice, and this can be lower priority, or you can set it, change it to be a higher
priority. Uh, in windows, I believe you can do the same thing, but the only way that I can recall,
and I don't have a windows prompt handy is through the task manager where you could change the
priority level of the process. And at that point, your, you know that point, the processor itself
is deciding which
when it's
trying to decide it's scheduling
for each thread,
it could pick the higher priority ones
to go first
and they get more run time.
Yeah, that's never worked out well for me.
Anytime I've ever tried to bump something to
super high priority, I just end up breaking my system.
You ever do expedited shipping?
So, like the shipping rate is still the same.
So, it's, you know, two-day shipping.
It's still two-day shipping.
But you do expedited shipping.
It's like, okay, you're going out in the next batch.
Yeah.
Yep.
That's a perfect, perfect example of it.
So, with that priority queue there, do you maintain the priority order when you
put the item in is that when you say bump to the first or yeah i guess you have to otherwise you
have to loop through everyone to see every time uh i'm assuming that's underlying implementation
because you're going to actually put the priority on it when the item comes in and it probably
automatically bumps it somewhere you just like basically start at one end and go until you're.
Well, I would imagine.
Call me crazy, but what if you just, I guess maybe you'd have to know like how many different levels of priority you're going to have, but couldn't you just have like underlying, you would have that many cues.
So you'd have your high priority cue over here.
It'd be like the airlines, right? Like here's where zone one queues up. Here's where zone two queues up, right? And first class is in its own queue.
So you'd have separate queues for each of those priority levels. And that way,
if something did come in to one of the higher priority levels, it's not rearranging anything.
You're just like, when it's time to pull the next thing, you're going through all of the queues from highest priority to lowest.
And you're saying like, okay, who has something for me to process next?
Okay, it was in the medium priority.
Okay, I'm going to process that thing.
And then you go back through and you're like, who has something for me to process next?
Oh, it was in the highest priority.
Okay, I'm going to pull from you first.
Maybe?
Yeah, the thing that's kind of yucky is like if there's ones that don't have, so it's like, hey, give me yours.
Oh, you don't have one?
Let me go.
You know, it's like Southwest.
It's like, okay, we had all the A's.
We had all the B's.
All right, C's.
Okay, we got one.
And then you got to start all over every time.
Yeah, you end up in the, like, if you, by default, then you would just have like a default priority level of like you're in the middle.
You're, you know what I mean?
Yeah.
Yeah, you can definitely do it that way.
I was imagining you would actually, like, the way I was was imagining i don't know if it's right or wrong whatever but
i was thinking you would start at the end and basically go until you see a priority that's
more important than yours and then you would insert there i guess the downside of that is that
or rather um one that is equal to or greater than you and that way you still end up being kind of
the last of your priority.
So it would kind of stink if like you always got inserted the first of your
priorities.
If like you and I have the same priority,
who goes first?
If I just came in,
but then you have to traverse your entire list.
Like worst case scenario,
if everybody in the list is of the same priority and you have a billion items
in that queue,
you just traversed a billion items to determine,
oh, I just got to add you to the end.
That's why I think you would start at the end, the opposite end,
and always move to the left.
Yeah, but either way, you'd still be going all the way through the billion records
to just determine, oh, there was no one higher priority.
You still get put at the end.
So check it out.
It doesn't look like that's how it's done.
Looking at the implementation details of how these things are typically done,
the priority queues are usually done in a binary heap.
Oh, wow.
So it's not even like the regular queue where you're going through the items.
It's more of a tree.
I didn't expect that.
Yeah.
Just do an episode on heaps.
We probably should.
So, yeah, I mean, kind of interesting when you dig into these things.
And there was even a question about why are binary heaps sort of the preferred method over another one.
And I don't remember which one it was because I can't get back to it right now.
But,
but yeah,
so the,
the underlying implementation isn't exactly probably what we would have
thought just,
you know,
off the top of our heads.
No,
that's interesting.
And it shows that I am not up to date on my heaps.
Right.
Yeah.
Now,
now my idea sounds crazy.
So, but this is just another reason why data structures are so important,
right? Just knowing the various things out there,
it makes you think through a problem differently, completely. So,
so the next one up is called a DQ. It's a double-ended queue. All right.
So it's funny because you'll find out in a minute that this is similar to
another data structure,
but it allows insert and delete at both ends of the queue. So we mentioned that a regular queue
allows deletes at the front of the queue, right? The first in first out, this allows you to do it
on both sides. Um, this basically makes this type of queue, a hybrid of a stack and a queue because,
uh, outlaw get into that a little bit so i'm going to
leave that one alone um but they're useful basically when you're handling clockwise and
and counterclockwise rotations in oh sub one time and if you go back to our big o notation stuff
that we talked about that means it's direct access. So can we just call it what it is
there? Really? I mean, like, let me, let me just say like, you're at the DMV. Okay. You've been in
this line now for three hours and you weren't paying attention. You were just playing some,
some, you know, candy crush on your phone, but you've been in that line for so long, your battery's running out.
You're like, man, I got a conserved battery.
I'm done.
And you look up, and you happen to actually see, like, wait a minute.
They're pulling people from both ends of the line.
I'm in the middle.
Like, it's just chaos is what it is.
They should just call it the chaos type.
Like that's crazy.
That's so awesome.
So we're going to refer to this not as the DQ as the DMVQ.
And the time that you're going to use this is when you need something that's efficient
in adding or removing items at both ends of the queue.
That's,
I mean,
uh,
that's it like this.
That's basically what this one is.
Next up,
we have the circular queue and this one,
you probably guess what this one is too.
It's also known as a ring buffer,
which I had not heard of.
Um,
so basically all it means is the last item in the queue points
to the first item. So if you're going through the items in the queue and you get to the end and you
keep going, then you're going to start back over at the beginning, right? It's just a circular door,
essentially. The way that you can see if a queue is full, this is kind of interesting, is that the
first item minus one equals the last item.
Then you're in a full queue.
Basically, the entire thing has been filled up.
So they're useful.
This was really interesting to me.
Never would have thought about it.
They're useful for memory management.
So it allows you to utilize memory locations that might have been skipped otherwise.
So if you think about a queue,
you have 10 spots in it and you filled up the first three items.
Well, you have seven untouched,
but your third item is basically,
I assume is going to point to your first, but you still have item.
You still have room in there and you can keep adding things in there.
So it's kind of a way to keep memory useful that otherwise would have just been hanging out doing nothing.
They say you do these for, like, traffic systems.
So it's kind of interesting.
If you think about, like, traffic lights, you're just constantly cycling through, you know, red, amber, and green.
And it's just basically on a cycle, right?
At some interval, it's going to change from red to amber.
And then after that interval, it's going to change to green.
Then it starts back over.
And then CPU scheduling, same type thing here.
You know, it's basically just kicking around,
going through its processes,
swapping things in and out of the active CPU.
And then it moves on to the next time. When it gets in and out of the active CPU, and then it
moves on to the next. I'm legacy in a list
starts back over.
Yeah.
Where's the color of your energy?
That's a lot of good things there.
Sorry.
Oh, I'm having fun
looking for
real-world uses for some of these.
So one of them that came up related to the circular queue was, well, you mentioned the roundabouts and traffic systems, but calendars.
Calendaring, right?
Like Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday, Monday, right? P.D.s. Exactly. Terrible, sorry. enduring right like you know monday monday tuesday wednesday thursday friday saturday sunday monday
right pds exactly terrible sorry yeah there you go i'm sorry i i'm not gonna talk anymore
yeah i got all the old school songs dude uh but uh going back to the the d real-world cases for a moment, two awesome cases that I was like, oh, yeah, I totally didn't even think about these.
History would be one.
So looking at the Stack Overflow answer here, like browser web history, right?
Because eventually it's going to age out, right? Because you would age, you would, eventually it's going to age out, right?
So you're going to have some old history that you're,
you know,
who cares anymore?
You can delete that one,
but you're going to keep popping new stuff in to the,
into the history.
Or another one would be like that they listed here was an undo operations,
storing the undo operations.
Cause eventually you'll,
you'll age those out.
So,
so I guess another way to say this is like for the DQ version of the Q,
or the double-ended Q, when you might need to age something out of the Q
as well as continue adding to the cue that's when a double into cue might be a of uh
you might be an advantage to you because you're working on both sides of the cue at the same time
yeah very cool um oh the the last thing that i had on here for the pros or cons of the circular cue
is there's not really any cons to this one as much as just knowing when to
use it like there were really no downsides to it like a lot of the data structures we've talked
about there's there's sort of been this list of hey this is the good things these are the bads
there really wasn't anything on this one it's just it's a useful feature if you need it
all right so with that we've already kind of hinted enough about stacks.
So why don't we talk about them?
So what is it?
So it's kind of the opposite of the queue.
We already understand that the queue is first in, first out.
Stack is last in, first out data structure.
So think about anything like the name pretty much is this.
This type actually lives up to its name more than other types might.
Think about anything you've ever stacked a stack of plates, a stack of books, stack of pancakes.
You don't have random access to the things that are in that
stack. You can access the top most item of the stack. So, you know, that's the one thing like,
I've been annoyed by this and I think I read it like in the imposter's handbook. I think that
there was a similar statement where he had the same, same issue where issue where if you have your plates stacked up in your cabinet, for example,
that bottom plate is brand new, never used.
Maybe when family comes over a couple times a year, then it gets used,
but it pretty much still looks exactly like when you bought it.
But that top plate, man, it's,
it's the, the coloring is all faded. You know, maybe there's a, a chip or a crack and you're
like, whatever, it's still good enough. And you keep using it. Right. Like, but that,
but that's the stack. So there's two principal operations on the stack. There is a push and a pop.
And I'll go over those in a minute, but there's often also a peak, but the push and pop are
the principal parts of this.
So the push operation is going to put a new element on the top of the stack and the pop
is going to remove the top most element from the stack.
And the peak will allow you to see the top most but not actually remove it.
So if you were to think about like the peak, for example,
is like let's say that the three of us, we go to breakfast,
and we just say like, hey, bring us a whole pile of pancakes,
a whole stack of pancakes, right?
And like we each grab the first 15 because, bring us a whole pile of pancakes, a whole stack of pancakes, right? And like we grabbed the first 15.
We each grabbed the first 15 because, you know, we want some pancakes.
But we can see left there on that stack, we can see the topmost pancake and decide like, oh, man, I really want that one.
That one's got extra chocolate chips in it, right?
But we didn't remove it yet.
But you can see it.
You can see that it's there, like that's the that's the peak operation um have you ever seen like um if you're
doing like a calculator i don't know if like that was kind of like a common computer sciencey kind
of problem back in the day uh and you're doing like post fix or prefix whatever order on the
the stack you would use it to do your addition so it was like uh if you saw an operator and uh well you know i don't really remember how it worked but basically the idea is that you would use it to do your addition so it was like if you saw an operator and uh well you
know i don't really remember how it worked but basically the idea is that you would look at the
top of the stack and if it was uh like a number then you would pop it off and the one before it
and like do the operation but if it was an operator then you would go ahead and push your
thing on so it's really important to be able to peek at the top list and same with like parentheses
or brackets curly brackets if you're doing any sort of parsing, it's common to say like
if, uh, you know, the top thing on the list is like a parentheses and add the item, otherwise
pop stuff off the list.
So it's nice to be able to do that.
Cause it'd be pretty annoying to have to like pop something off the list just to put it
back on because I don't actually need it yet.
Yeah.
And, and, you know, as I'm talking about this thing,
you know,
I,
I'm saying stuff about like,
Hey,
you're going to put something on top and you're going to remove something from
the top.
But technically that's not really how they're implemented necessarily,
but that's just how like we can easily visualize it.
So if you look at diagrams of stack,
you know,
or,
you know,
go reading about them and everything like
that's typically how you're going to see the visualization. So you might see some of them
times where they're like, they go left to right and you know, they're, they're just pulling from
like the right hand side of it. Um, but, uh, yeah. So, um, we've talked about things like
arrays and linked lists. So at its core, these stacks can be implemented using either, right?
An array or the linked list.
And in this case, you're just removing an item from the end of that, right?
But technically, you'd really only need like a singly linked list.
You wouldn't need the double linked list that we talked about,
right?
You only need to know that then the next item would then become the top of the
stack at that point.
It's always removed from one end,
right?
Yeah.
Cause you're only going to remove from one.
So,
all right.
So what might be some pros to the stack?
Um, it's a fast way to know where you've been,
right? And your read rights to this are always going to be O of 1 since you're only
touching the top of this stack. Cons, though, to stacks is that depending on the language and
the implementation, stacks, stacks going back to
our conversation about like different parts of memory, um, that we talked about stacks may be
used to store both the data as well as the return addresses. Um, so this may can make them susceptible
to a stack smashing attack. Try to say that 10 times fast.
When your input sizes aren't verified.
So this is a variation.
The stack smashing is a variation of a buffer overflow.
So I say that because whether you, you know, you might not even be aware of how things are being called or like,
you know, um, but you could inadvertently give access to, uh, because you aren't checking your
inputs, then, you know, someone could pass in something that's that overfills it. And now they
have the return address to go to some other spot within your routine or some other
library. And now, you know, that's how a lot of hacks end up happening, right? Is that you're
trying to crash systems and overflow data points. All right. So another con to this, similar to the
queue, because stacks and queues are very much go hand in hand, right?
So there's no random access to a stack, just like there was no random access to the queue. You know, in the stack, the last thing you put in is the first thing out.
Okay, so when might you want to use these? So if you are backtracking while traversing a graph
or a tree, then that's why I said like a fast way to know where you've been. So you're trying to,
uh, search this tree. You want to know like, Hey, what was the last element that I was at?
Because I need to go backwards now because I've, you know,
I've gotten to the end, right? So that's where this comes in. And I think you mentioned like
breadth first. So this is like, uh, if you remember like depth first search, then this
might where that come in handy, right? Um, if you wanted to reverse things like a string or any array-like thing, then a stack would be great for that to be able to reverse that.
You could just pop the item into the stack and then get it back out, and you've got it in reversed order as you pop them out of the stack.
Your call stack.
I mean, we talk about that. Like we talk about call, we talk about stacks all the time and you might not even think
about it, but your call stack is a stack, right?
A real stack.
Yes.
Yeah.
Uh, and if you were writing your own kind of a compiler of some sort, right?
Uh, stacks are often used for ensuring that the opening and closing constructs,
things like curly braces or parentheses or like closing tags, you know, to ensure that they're
balanced. You could use a stack to ensure that. It's often used. So there wasn't a whole lot to
say about stacks since, you know, there was so much buildup to it in regards to like a raise and cues and
whatnot that by the time you get there,
like,
okay,
yeah,
this is just a cube,
but in the opposite direction.
Good stuff.
This really comes up a lot in like computer science,
you kind of questions like,
you know,
we gave a couple examples there of like places where they're actually used,
but like this is one of those things I see come up
in like those kind of algorithmic-y kind of questions.
A lot of times, like the solution is something
that uses a stack because it's something that kind of
is used for either parsing or used to kind of track
current state without having to do a lot of
kind of going back and forth and memorizing stuff.
So kind of like a cheap rule of thumb that I kind of think about
and look at problems like code wars or something.
Or if it seems like I'm having to do a lot of work to track the state that I'm in,
if I'm doing anything that remotely feels like or looks like parsing,
then there's a good chance that a stack is going to come into play
because I probably only need to care about certain conditions.
And once those conditions are met,
I can kind of ditch them out of my stack and move on until I'm done.
And so it works really nice with recursion, scales really well.
It's an efficient way of solving problems.
You don't have to keep looking at the same data.
And it just seems to work really well in a lot of use cases.
Yeah.
And then another reason why you would know this,
I found it humorous that like,
uh,
you know,
we mentioned the imposter's handbook earlier.
Yeah.
A lot of these,
a lot of these day structures,
like he was quick to remind you,
like,
why do you need these?
Cause it's going to come up in an interview.
You're going to see these come up in an interview.
Every one of them.
Well,
maybe not all of them.
The,
the,
the Q subtypes,
maybe not so much but everything else
wait till the next interview and i'm gonna be like explain a float
watch out got it this is happening you didn't know this oh man all right so uh
man this episode is gonna be so link heavy uh are going to be a ton of links in here.
So be sure to check the resources section to find helpful links.
And we said this before, while you're listening to the episode, you can actually pull up the show notes and follow along.
So if you didn't already know that, now you do.
And you'll be able to see some of those links and go read about it as you're listening to it.
So with that, let's head into Alan's favorite portion of the show.
It's the tip of the week.
Yeah, and once again, I cheated and pulled from our slash tips page,
which you can get to at codingbox.net slash tips.
Or I don't know if we want to use this guy.
What do you think?
Is it a secret?? Uh, secret.
You can't.
Sure.
It was going to be a new Twitter thing.
So yeah, I guess let's go ahead and say it and then let me give a little brief thing
about it.
We got a new domain name.
It's shorter, so it's nice for the sort of thing.
So if you go to CB, like it's encoding blocks, CB dot show slash tips, then you can enter a tip there, put your name in there,
and I can use it.
I'm going to find out what that password is.
You've got to, by entering this information,
you're explicitly giving me permission to read your tip on air
because I'm so bad at coming up with them and I need your help.
And they're so good.
And today's tip was provided by Gaprogman who is famous for lots of reasons
including the.NET Core podcast, which is great.
And he pointed out that you should use NPM CI and build
servers instead of NPM build because it's faster and more resilient.
And you can kind of read up on why that is specifically,
but basically the rough gist of it is that it avoids much of duplicate work
and it uses package.json to get the specific versions of things.
And it's really good about noticing duplications in the node modules
and skipping those.
Wait, so why wouldn't you use this everywhere instead of
just a build server?
That's a good question. I forget.
I call my computer a build server, so I guess
I just use it. Where are you when I need
you, God Programming?
It sounds like it's so
cool that why would you ever use
NPM build? I'll tell you why.
Here's a real-world use case of why you would want to use NPM Build over NPM CI.
Let's say that you are a contractor and you charge by the hour.
I like where you're headed.
It's in your best interest to not be so blatantly obvious that you're slowing things down,
but, you know, like subtly, you know.
Or maybe you're trying to heat up your room.
Then you use NPM build versus NPM CI apparently.
Yeah.
All right.
Yeah, it's really fast.
Just reproduce some builds.
Blah, blah, blah.
All right.
So I shouldn't ask questions of the tips that you get for free.
Man, I'm tired.
I looked at this thing hours ago.
Oh, man.
We do some long shows, y'all.
Man, you know what's funny is, like, do I...
Do factory design pattern framework.
Did I...
Okay, so... Oh, I mean... mean so will you give me a hard time
no no so this is something that i think i put in a long time ago and i don't remember if i ever
said it or not so man i i put that there for you alan i thought i told you so
yeah i didn't i thought you i thought you kept it there in the show notes because you already knew about it.
Man, I can't remember.
Why don't you save that one for next time then?
No, we'll do them both.
I don't mind because I'll find something else interesting between now and then.
So, this one's called Dew Factory Design Pattern Framework.
Joe Recurs and Joe had sent this to us a while back.
And I don't guess I'd ever mentioned it but i meant to um but if you search for it it's basically i want to say
it was like software or a download that would actually give you code of the various different
design patterns and it was pretty nice. I don't remember
what the cost was or anything, but I'll have to find a link for it somewhere and I'll include it
in the show notes. So thank you to Joe Recursion Joe on that one. And then, so while I was looking
for things like the arrays versus list performance things, I came across this little golden nugget called benchmark.net.
And it's made for testing out things
similar to this,
where you want to find out the performance.
And they say in here,
in the description on the GitHub page,
benchmarking is really hard.
You can easily make mistakes
during performance measurements, blah, blah, blah.
So this thing's built to protect you
from several of the things that come up, like say multiple projects, all that, but it will
actually take these, these pieces of code and you can use it to benchmark things and you'll get nice
little charts and graphs and all that stuff out of it. So I thought that was super cool. They have a site called benchmark.net.org that has examples and all that kind of stuff on it.
So we'll have links to both that and the GitHub page.
And pretty cool stuff.
And it goes right in line with what we've been talking about today.
Yeah.
I just want to jump in on why you wouldn't just always do npm ci
did you get a hold of them yeah yeah woke them up got them on the bat phone
the god product man phone uh and uh figured it out uh it uh skips by default uh or by using ci
it'll skip certain user-oriented features so it'll like not download documentation and stuff
like that and there's a couple of things that you can mark in your package that I won't do.
And yeah, so that's nice.
It also will disregard any node modules folder that you already have.
This is kind of intended for CI environments.
So it'll blow away your folder and do it every time,
which stinks if you're doing something locally all the time.
But on a build server where you're doing that anyway,
it's just kind of like a nice default feature.
Okay.
So it's not that it's necessarily always more performant.
It's doing things for you that can keep you out of trouble in a build
environment too.
Okay,
cool.
Nope.
So,
okay.
I feel like I'm being trolled here.
So one of you guys put like a random link in here.
You're like, like oh you're supposed
to use this and i did so i see i saw this tweet come in last night and uh i liked it so we're
we're officially changing this section of the show to uh it's the get command of the week and
you know whichever one of you two decided to put this in as my tip of the week without letting me know thought like, oh, I would probably like trip on myself.
No, no, no, no, no.
I got you covered.
So here's one that we haven't talked about, which is the get subtree command.
This one is quite awesome if you've never experienced it.
So here's this case for the subtree command.
Let's say you are in a situation to where your code base is getting kind of big and you're like,
you know what, man, this namespace here, I really don't even want this in this repository anymore.
I want to create a new, uh, a new repository for that, but man, I'm going to lose all the history.
Nuh-uh.
With the git subtree command, you could export that directory out as its own repository and
maintain its history, but it gets better.
You could also do the inverse of that, where're like you know i have this this other repository
over here and i really just want to bring it into this other repository i want to i want to merge
these two things in without losing my history right you can do that with also the same command
you can merge those two.
You'll bring that other repository into your main repository and maintain all of your history.
So whichever one of you two thought you would trip me up,
joke's on you, buddy.
Get Subtree.
And I want to thank Jimmy at Greenfield Coder
for calling me out on the Get Command of the Week.
Jimmy, there's your Get Command of the Week.
That's awesome.
The real –
You just pulled that one?
You just had it ready?
No, I literally just pulled that one out of my head.
When I saw –
You just had that sitting there on tip of the tongue?
I wasn't planning on talking about it, but I saw this.
One of you just typed this in, and I'm like, wait, what?
And then, because I saw that, because that tweet came in, like, early, early this morning, and I probably shouldn't have even been up to see it, but I was.
And I was like, oh, sweet.
Yeah, I like that.
But, yeah, it was early, but it was, like, early enough that I forgot about it. So thank you for reminding me of it though um but the the tip that i had planned on saying was uh we've
talked about so many of these like um lists of like uh your ultimate uh oh man i can't even think
of the term that we had for it like there were several where we had like a repo of like here's
every blog that you might ever want to read or here's every command for oh not a checklist
what do we call those things at any rate i i
can't remember neither of you guys remember what i'm talking about all right i gotta i gotta find
words that better describe um but yeah so uh conrad conrad sent us uh an email where he gave us basically the same kind of list, but of documentation,
dev docs.io.
So you can go in and you could find anything that you might want to find
documentation on.
You want to find some JavaScript documentation?
Sure.
It's right there.
What about angular right there in the same place?
How about Apache?
Boom,
right there.
There's so many different choices in here. How many places do you know of where you can see
your documentation for Apache and JavaScript and the GCC compiler all on the same site?
Searchable too. Right. Yeah, how awesome is that? I mean, there is a long list of libraries and frameworks and languages and applications all with the documentation right there.
It's awesome.
Man, I don't know that I can trust this list.
ColdFusion's not in there?
There's a reason for that.
EXTJS isn't in there?
There's a reason for that. There's a ColdFusion podcast, a there? There's a reason for that. EXTJS isn't in there? There's a reason for that.
There's a ColdFusion podcast, a lot of you know.
Really?
Yeah.
I'm sorry.
What's it called?
Sorry, Jack.
It's called CF Alive.
That's ridiculous.
ColdFusion Alive.
I didn't mean to take your your uh your spotlight
there outlaw but it was funny that the things that that we sometimes reflect on don't exist there
and cold fusion is awesome i don't mean to sound down on it no it's really not see if we see if i'll put that's a really cool thing right there the dev docs
yep so that's my tip of the week so very nice all right well thank you very much remember you
can send in tips by going to cb.show slash wait cb.show show slash tips.
Oh, man.
And thanks for sticking with us.
We started talking about data structures.
We're going to have some more data structures coming up here pretty soon.
So stay tuned.
Yeah, and I wanted to add, like, you know, don't be alarmed if you see us, like, start tweeting that out, that shorter one out.
Because, you know, mainly got it so that we could, you know, save some character space on tweets and whatnot or you know places where it might matter but uh yeah so with that
uh we hope you've enjoyed the show be sure to subscribe to us uh if you happen to be listening
to this because a friend let you borrow it or they pointed you to it um you know and if you
haven't already uh like Alan mentioned earlier,
you can,
we would really greatly appreciate it.
If you left us a review,
it,
it really does mean a lot to us,
puts a smile on our face.
You can find some helpful links at www.codingblocks.net slash review.
And,
and Michael's always saying,
and I always forget,
share it with a friend.
If you've,
you know,
if,
if somebody's in need of some coding
improvement, share it with them.
Maybe they're not a friend, but
share it with everybody.
While you're up there at codingblocks.net,
you can check out all the show notes, examples,
discussions, and more.
And send your feedback, questions, and rants to the Slack
channel at codingblocks.slack.com
and you can go to Twitter, you can find
at codingblocks, or you go to codingblocks.net
and find all of our social links at the top
of the page and if you're interested in
our campaign, you can go over
to jamstack2020.net
and oh dang it!
How do we
cancel this? Wait, somebody cut this.
Jamstack, what?
It's the future, man. We're going to
do an episode on Jamstack. I'm going to tell you
why it's the future of
the internet. Is it the blockchain
of JavaScript? Oh, no.
You painted it.