Тёмный

Why You Shouldn't Nest Your Code | Cakez Reacts 

Cakez
Подписаться 9 тыс.
Просмотров 2,4 тыс.
50% 1

By ‪@CodeAesthetic‬ - • Why You Shouldn't Nest...
Support him: / codeaesthetic
····················································································
Twitch ➤ / cakez77
Twitter ➤ / cakez77
Patreon ➤ / cakez77
Discord ➤ / discord
TokTok ➤ / cakez_77
Coding ► / @cakezdev
Gaming ► / @cakeztv
····················································································
My Game Tangy Defense
store.steampow...
····················································································
* About Me *
Hey! And welcome. I'm an indie game developer, currently working on a Tower Defense Game written entirely in C/C++, I tried using Unity or Unreal Engine in the past, but never really found much success. I'm mostly posting devlog / devblog videos where I show off the progress I make on the game.
····················································································
Check out some of my other videos!
• I Tried JAI, Can It Re...
• First Time Using Godot...
• Tired Of Unity? Let's ...
• This Made My Game So M...
• I Made Vampire Survivo...
• A Day In The Life Of A...
• My life changing year ...
····················································································
#cpp #gamedev #coding

Опубликовано:

 

17 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 65   
@RichardLofty
@RichardLofty 4 дня назад
Moving things to separate functions DOES remove nesting. Nesting in terms of visual clutter. Especially when there is code, that cant even fin on a monitor.
@InfiniteQuest86
@InfiniteQuest86 4 дня назад
Thank you. Exactly. Why is this so confusing to people. The same people against writing functions will write comments describing what they did. But no one will ever go and change the behavior and side effects of writeEntireFile(), but they will have no problem destroying anything in the middle of there when it's part of one giant 10,000 line main with only a comment at the top that says //writes entire file.
@squishy-tomato
@squishy-tomato 4 дня назад
Oh dear.... you should not read code like a book. That is complete madness. The idea of adding functions is to limit scope and give the reader a chance to go straight to the relevant parts. Nobody should be reading a code base from start to end, line by line. That is a massive waste of time and effort.
@bluesillybeard
@bluesillybeard 4 дня назад
The deepest I ever go is about 6 or 7 levels. I agree with Cakez here, for the most part. I generally don't extract something out into a function until it's actually repeated multiple times. Instead of extracting out to another function that's only used once, I generally make a scope and label it - sometimes I'll make a function if I can think of a really good name for it, but generally I don't. This is just what works for me, after about 5 or so years of working on hobby projects, and the occasional open source contribution.
@melonenlord2723
@melonenlord2723 4 дня назад
If you have something more complicated and you can put it into a nice function where the name of the function pretty much can tell what it does so you dont have to read the function itself, then it's fine to put it into a function i guess. Even if it only is needed once.
@bluesillybeard
@bluesillybeard 4 дня назад
@@melonenlord2723 Agreed
@saeedxgholami
@saeedxgholami 4 дня назад
You can press gd to jump to the function definition and by pressing ctrl + o to jump back where you was.
@ivanjermakov
@ivanjermakov 3 дня назад
I laughed when he changed indent to 8 spaces and nothing have changed. I was like yeah, gg=G
@dertobbe1176
@dertobbe1176 4 дня назад
As Thor from pirate software said, your dont need to worry about your Code because undertale exists. All Dialogs from undertale are written in one single switch statement. And guess what? You can play the game and the Player dont care, because the player never will know
@Vegan_Kebab_In_My_Hand
@Vegan_Kebab_In_My_Hand 4 дня назад
In my opinion, Thor is making the case for the functionality of the end product, not readability during production like the "never nesting" video. Now, code readability is inevitably related to the ability to finish a given project, that I wouldn't argue against. But I think that "player doesn't care how the code looks like" is still a different enough point/topic from "code readability for the programmer itself".
@SoDamnMetal
@SoDamnMetal 4 дня назад
So many developers are so weird, treating a coding style or language like a religion. Just do whatever makes most sense to you. Unless you work for a company, then do whatever your corporate overlords tell you. ☠
@rafagd
@rafagd 4 дня назад
The only good code convention is mine. Everyone else's is shit. It's as simple as that.
@maikha8963
@maikha8963 4 дня назад
With all the respect. I hate your code
@maikha8963
@maikha8963 4 дня назад
please put your curly braces with the if statements and for loops in one line please
@Cakez77
@Cakez77 4 дня назад
lol
@ivanjermakov
@ivanjermakov 3 дня назад
@@maikha8963 unfortunately, most C++ (also C, C#) projects use Allman indent style
@timmygilbert4102
@timmygilbert4102 18 часов назад
When programmer don't know about code folding
@MikAlexander
@MikAlexander 4 дня назад
This is like higher level ocd. All those clean code, never nesting, functional, those are just obsessions, not practical strategies.
@InfiniteQuest86
@InfiniteQuest86 4 дня назад
I don't know. When people are like writeEntireFile() is too abstract and you are making too many functions and it should all be in one 10,000 line main function, you are going too far. I don't think functions need to be 10 lines or less, but more functions is not less readable if they are named well. He was basically saying it's too confusing when everything is in a well name function because I can't trust the function name. The problem is him, not the coding. He doesn't think a well named function will do what it says. He's like it's too hard to read because I have no idea what writeEntireFile() does. Wft??!?! What do you mean you don't know what writeEntireFile() does? It writes the whole file.
@redstoneninja3375
@redstoneninja3375 4 дня назад
What file does it write to, what are the contents it writes to the file
@tubeincompetence
@tubeincompetence 4 дня назад
@@redstoneninja3375 ".... and why did it delete my steam library?"
@InfiniteQuest86
@InfiniteQuest86 4 дня назад
@@redstoneninja3375 Language depend and irrelevant to my point. Just pass those in if you are in C.
@Vegan_Kebab_In_My_Hand
@Vegan_Kebab_In_My_Hand 4 дня назад
So far I find that extracting longer blocks of code (even if not reused) into functions improves readability of my code. Same goes for too much nesting, idk why exactly yet since I'm still pretty new to programming in general. I'll need the scope of my projects to increase to really see how I prefer to code and progress. Anyways, someone else might feel differently about extraction, but this is the silver lining of solodev. We can shake our heads over how other developers' code looks and do our own thing.
@lucaxtshotting2378
@lucaxtshotting2378 4 дня назад
there are so many good reasons besides "use it more than once" to declare a function. Here are some of mine: - to maybe call it more than once - to extend its logic in an override - to verify if it is called in a test - to reference it (removeEventListener(myfunction); if callback === myFunction) - to do other type of reflexive ops - in some languages, to annotate its logic with more precision (I guess this is a type of reflexion) - in python, to call it inside a lambda exp (only expressions are allowed inside) - to profile it - because my boss told me to I'm definitely a nester. I nest so much I write "else" after throwing an exception or returning. I like early returns, just so the else is closer to the if. Sonar calls this "cognitive complexity", which I find rather f*cking ironic, since it is navigating through functions and clases and files what I find cognitively complex. This guy just read the Sonar manifesto and made a video off of it
@lucaxtshotting2378
@lucaxtshotting2378 4 дня назад
to me, exctracting means replacing logic for tags of what the logic does. Which I don't like even when done well. Just learn to read code and expect everyone to know how to read code. But then you have some programmers encapsulating logic in a boolean symbol (func or var) and end up with something like "if (this.shouldUpdate()) { this.update() }, which honestly f*ck you if you write that
@lucaxtshotting2378
@lucaxtshotting2378 4 дня назад
I think the c++ language extension comes with an auto formatter no? just shift alt f that shit for the lols bro
@KenlieroGames
@KenlieroGames 4 дня назад
I use a lot of functions when working with UE nodes. If the function grows a low, it can easily become a huge mess, where you have million nodes all over the place. Functions make it much more readable, specially when I use very long and easily understandable functions names. And it also gives me opportunity to call it from elsewhere. I also use a lot of nesting with structures, simply because it allows me to build very easily understandable ways to organize code, and allows me to easily use the same structures for many characters, when variables with structures are inherited from the same parent. My history is from pascal and C#, so I am very used to that kind of way to code.
@lucaxtshotting2378
@lucaxtshotting2378 4 дня назад
@@KenlieroGames to avoid the huge mess of big functions I just use empty lines followed by a small comment. Comments lie, yes, but so do symbol names (my company is french and truust me I'd rather them write a comment in french than use functions and vars with the names they give them. I think one of them specifically doesn't know the concept of "plural nouns". It is ridiculous). Breaking appart your code can expose some flaws in your logic, but if you refactor and your code gets simpler that's a skill issue. Defniitely doesn't help read the program though. Besides, moving params around is at best adding extra overhead to your program, at middle extra work and at worst risking mutability or type related bugs. At the end of the day is a matter of personal or team preference, that is why I'm vocal against unnecessary modulation, because it seems to be the "clean code" praesumptio
@lucaxtshotting2378
@lucaxtshotting2378 4 дня назад
nah but for real this guy created a "ColumnDescriptor" class which contains an "Iterable column" field.
@JG-nm9zk
@JG-nm9zk 4 дня назад
Excuse me.... How many lines are in game.cpp
@Giga4ever
@Giga4ever 4 дня назад
It's no surprise that you like nesting, because your code relies on global mutable state, and you didn't write a single test in your entire life as a programmer. A function signature can be seen as a contract: It tells you explicitly which state goes in, and what goes out. Nowadays, it's also common to encode possible failure cases in the types as well(C++'s std::expected, Rusts Result, etc.).
@Cakez77
@Cakez77 4 дня назад
Such a cringe comment, but that was "std::expected" from Mr. Inukai.
@Nikoline_The_Great
@Nikoline_The_Great 4 дня назад
I agree with your love of early returns. In fact that is why I love labled scopes that you can break out of. It effectively lets you do early return clauses in the middle of a longer function.
@tubeincompetence
@tubeincompetence 4 дня назад
In general I guess I mostly disagree. I sure wouldn't call adding a function call "nesting" though. But there is a balance to where you should break out code blocks to functions, or just keep it in place. Sometimes long functions can be easy to follow anyway.
@blenderpanzi
@blenderpanzi 3 дня назад
Reading the headline I thought it means not to indent at all.
@untlsn
@untlsn 4 дня назад
I like to treat code like book too. But not as a novel that i read from top to bottom, but as a manual. For example if i need to repair my washing machine I don't want to read entire manual I want to jump: Legend -> Repair -> XYZ: If you want to fix XYZ do ABC And yes names of functions in this video way not helpful
@tiwanndev
@tiwanndev 4 дня назад
I think there’s no “good way” of doing things. Just code the way you want and the way you are faster. I’m a never nester but this is just how I like to code and I won’t tell anybody this is the way to go
@principleshipcoleoid8095
@principleshipcoleoid8095 4 дня назад
Ngl I write code. It sometimes works. Sometimes underlying library fight with OS and cause a memory leak after the OS gets updated, but only on some machines... :p
@RichardLofty
@RichardLofty 4 дня назад
Every function call has it's cost. So wrapping everything into functions can be bad.
@dnoordink
@dnoordink 4 дня назад
I prefer early return to nesting, especially if it's a simple boolean. Much cleaner to my eye. But I don't mind nesting when it makes sense. They both have their place.
@RenderingUser
@RenderingUser 4 дня назад
26:11 its bascally this. do you value the details. or do you value the overall view. the former works if the program is tightly put together and one function needs info from another. the latter is generally better
@MikAlexander
@MikAlexander 4 дня назад
For overview you just produce a comment with index on the top of the program, and that's your overview.
@MikAlexander
@MikAlexander 4 дня назад
For that % earlier in the video, using inline function would inline that code by compiler, but otherwise it was bad practice.
@smx75
@smx75 4 дня назад
To me, it boils down to how people like to go through code. Never nesters tend to appreciate doing it in passes. The least amount of informations needed to get answers the better.
@MikAlexander
@MikAlexander 4 дня назад
The inversion is all right, but it's common practice.
@Kiyuja
@Kiyuja 4 дня назад
I dont think indentation is inherently bad. I wouldnt like to jump around in my class, just to keep the indents low. At the end of the day this is PURELY a matter of readability and I prefer having most of the logic in one place, unless something is used in multiple places.
@MikAlexander
@MikAlexander 4 дня назад
5:30 For those who don't know, function suppose to be long, not short. Every time you request a function the cpu has to load that chunk of code in to cache.
@Giga4ever
@Giga4ever 4 дня назад
Or you use a decent compiler that inlines your functions..
@InfiniteQuest86
@InfiniteQuest86 4 дня назад
This isn't the 80s. That's not even remotely true anymore.
@MikAlexander
@MikAlexander 4 дня назад
@@InfiniteQuest86 No, Carmack said it just few years ago on a podcast to keep functions long
@InfiniteQuest86
@InfiniteQuest86 4 дня назад
@@MikAlexander Then he's stuck in the 80s. Inline functions literally get pasted in. The function is long in assembly. But it's readable in code.
@ivanjermakov
@ivanjermakov 3 дня назад
For those who don't know, variable names must be short (ideally a single letter). Every time you define a variable, cpu has to read more bytes to make definition table. Seriously, if your compiler is not capable of such trivial optimizations, you should not worry about such high-level readability techniques.
@SR-ti6jj
@SR-ti6jj 3 дня назад
Gamedevs are trash programmers (come at me)
@tainicon4639
@tainicon4639 4 дня назад
Use goto statements instead
@thedapperfoxtrot
@thedapperfoxtrot 4 дня назад
I think liberty to choose how you want to write it is best, but at my school I'm forced to use extraction and inversion. We have formatting rules that say all functions must be
@InfiniteQuest86
@InfiniteQuest86 4 дня назад
If they didn't explain those things, maybe it's a lot to think about. But it's a no brainer. If you hit the end of a line, break it into more than one. If the function doesn't fit on the screen, break it up so it will. If you need more than 5 arguments then, you probably should be passing in a data structure that bundles them. Heck, I don't even bother with data/length being passed anymore. I just package them together into a struct. I don't even allow more than 3 arguments. Way too much typing and way too confusing.
@MikAlexander
@MikAlexander 4 дня назад
Carmack does it like this void MajorFunction( void ) { // MinorFunction1 // MinorFunction2 // MinorFunction3 }
@Giga4ever
@Giga4ever 4 дня назад
Carmack also says that nesting should be avoided in the same article..
@MikAlexander
@MikAlexander 3 дня назад
@@Giga4ever Yeah, it's a linear approach to code. Too much nesting isn't good, thought sometimes you need a very nested sequence.
Далее
Okay, I'm a bit scared now...
28:05
Просмотров 95 тыс.
Why You Shouldn't Nest Your Code
8:30
Просмотров 2,7 млн
They Made a Sequel to C
1:53:24
Просмотров 90 тыс.
Building An Ascii Particle System
1:45:39
Просмотров 124 тыс.
Demystifying the C++ Compiler!
12:52
Просмотров 14 тыс.
The Success of "Core Keeper" | Game Dev Analyzes
37:33
Просмотров 2,4 тыс.
Writing a game the hard way - from scratch using C. #1
34:20