A few common code smells to be aware of and how to fix them — Transcript
Full transcript
- 0:00a code smell is something in your code
- 0:02base that may be indicative of a larger
- 0:04problem in the system I use the word may
- 0:06here because sometimes there is a
- 0:09legitimate reason for code like this to
- 0:11exist but just like if you smell smoke
- 0:13you should probably make sure the house
- 0:14isn't on fire if you come across a code
- 0:16smell you should take a moment to
- 0:17evaluate it and see if your code needs a
- 0:19bit of care and attention so here's a
- 0:21few common code smells that you may find
- 0:23in your games why they can be concerning
- 0:25and how to fix them first up is long
- 0:28functions long functions are probably
- 0:30one of the most common code smells
- 0:31you'll come across we all write them
- 0:33from time to time because when you're in
- 0:35the zone you're just focused on getting
- 0:36the code out and don't want to break
- 0:38that flow and that's okay but you should
- 0:40come back to some of your longer
- 0:41functions later and make sure the
- 0:43function is an appropriate length I
- 0:45don't like to offer specific length
- 0:46recommendations as that just results in
- 0:48an arbitrary cutoff point but if you've
- 0:50got more than maybe 10 or 20 lines of
- 0:52code in a function ask yourself if all
- 0:54of the functions code is related to
- 0:56doing a single thing or if things could
- 0:58be broken out a bit we want to do this
- 0:59the soar code is easy to read easy to
- 1:02maintain and doesn't contain duplicate
- 1:04code for instance a function to
- 1:06calculate how much damage to take
- 1:07shouldn't also take care of everything
- 1:09else related to taking damage such as
- 1:11modifying units Health updating the UI
- 1:13playing a sound effect emitting some
- 1:15particles and so on you may want to
- 1:17calculate damage without applying it at
- 1:19some point in your game such as for the
- 1:20UI but this function could be
- 1:22incorporated into another general
- 1:23purpose take damage function that
- 1:25coordinates all of these tasks together
- 1:27when it is time to take damage another
- 1:29common issue is having long parameter
- 1:31list for functions and there's really
- 1:33two main issues with this the first is
- 1:35how to make sure code less readable as
- 1:37the function declaration probably
- 1:38doesn't all fit into a single line on
- 1:40your monitor and the other is that it
- 1:41implies this function may be too complex
- 1:43in one way or another maybe the function
- 1:45is trying to do too much or be too
- 1:47generic branching internally based on
- 1:49what parameters it receives and
- 1:51therefore needs a lot of parameters that
- 1:53it doesn't need in every invocation or
- 1:55maybe the data being passed in really is
- 1:57this complicated but it should be
- 1:58contained in a data object that some
- 2:00sort to make it easier to pass around as
- 2:02constantly Gathering 10 separate
- 2:04parameters every time you need to call a
- 2:05function is going to add a lot of bulk
- 2:07to your code and open up the potential
- 2:08for data to become out of sync in some
- 2:10way next up let's talk about duplicating
- 2:13code which is one of the first smells of
- 2:15a poorly organized code base
- 2:17the first time you copy a few lines
- 2:19around here and there and then later
- 2:21have to make a change to every single
- 2:23occurrence of that code is enough to
- 2:25make you realize how important it is to
- 2:26keep your code singular and unique if
- 2:28the same code is needed throughout a
- 2:30single script turn it into a function if
- 2:31a lot of different objects need the same
- 2:33functionality ask yourself if they're
- 2:35similar enough to inherit from a shared
- 2:36class or if not another option is to
- 2:39create a set of utility functions that
- 2:40house commonly need a code such as for
- 2:42formatting currency or to create more
- 2:44components in your application that can
- 2:46be attached to other objects wherever
- 2:47they're needed next up is God objects
- 2:50and I'm going to explain this one with
- 2:51an example so let's say you've got your
- 2:53player managing their health internally
- 2:54when they heal or take damage and now
- 2:57you need to update the UI you skipped my
- 2:59video on the Observer pattern so instead
- 3:01you just create a global object and use
- 3:03it to hold a reference to the player so
- 3:04the UI can ping for health changes every
- 3:06frame not exactly elegant but it works
- 3:09for now so then you decide you want to
- 3:10show a score based on how many enemies
- 3:12have been killed so you put a few more
- 3:14references into this global object then
- 3:16you need a few helper functions to
- 3:17manage everything and pass some data
- 3:19around so you add those in and you might
- 3:21as well make it so you can play a few
- 3:23sound effects from within this object
- 3:24since you're already passing health and
- 3:26enemy data around and it's easy to catch
- 3:28when something is happening plus you
- 3:29haven't really gotten around to figuring
- 3:31out how to handle audio yet so really
- 3:33where else would it go next thing you
- 3:35know you've got a god object on your
- 3:37hand an object with no singular purpose
- 3:39that essentially acts as a Dumping
- 3:41Ground for data and functions you
- 3:43couldn't decide where to otherwise place
- 3:45this is problematic as global data and
- 3:47objects are the singular strands that
- 3:49piled up into spaghetti code making it
- 3:51difficult to debug and reason about your
- 3:53game's State since significant changes
- 3:55to the data and function calls can occur
- 3:57from anywhere in the app plus once
- 3:59you've gone down this path it can make
- 4:01it hard to get out of it refactoring
- 4:03global data to non-global data generally
- 4:05isn't going to be a trivial task and the
- 4:07constant temptation of making just one
- 4:09more addition to the existing dumpster
- 4:11fire is going to be hard to resist there
- 4:13is a time and a place for Global objects
- 4:15but they should be designed with a
- 4:17purpose so ask questions study a few
- 4:19design encoding patterns and don't dump
- 4:21everything into a singular global object
- 4:23and lastly for how much I talk about
- 4:26good coding form and the importance of
- 4:27design and coding patterns it feels
- 4:29important to also take a bit to talk
- 4:31about the other side of the spectrum
- 4:32don't over engineer things building a
- 4:35solution that is much more complex and
- 4:37difficult to maintain than what's needed
- 4:39is just as bad and sometimes worse than
- 4:41having an under engineered solution sure
- 4:44that function may be a bit long but that
- 4:46doesn't mean you need to make an entire
- 4:47class with multiple layers of
- 4:48inheritance just to get the link down
- 4:50deep dependencies like that are
- 4:52difficult to maintain difficult to debug
- 4:54and reason about and can often add dead
- 4:56code that's not actually needed the goal
- 4:58is to make your code flexible enough
- 5:00that you can add to it or refactor as
- 5:02needed later on but not to take every
- 5:04feature as an exercise in designing an
- 5:06overly abstract convoluted architecture
- 5:08that may never be required I will
- 5:10probably discuss this point more in
- 5:12depth in a later video but here's a few
- 5:14questions I like to ask myself when
- 5:16figuring out what I need first off what
- 5:18features are required these are the
- 5:20things that need to be prioritized in
- 5:22the design and can help really inform
- 5:24what I'm gonna do next up what features
- 5:27are reasonably likely to be needed this
- 5:29could be something like an optional game
- 5:31mode I want to add if time allows a new
- 5:33feature that's still in the prototyping
- 5:35stage and so on I'll keep these features
- 5:38in mind when coding but their inclusion
- 5:40can't override what I know I need and I
- 5:42won't take much time or effort to
- 5:43support them until I know I need them
- 5:45this is the situation to pick the low
- 5:47hanging fruit parameterize a function
- 5:49create an enumerator to represent
- 5:51different configurations
- 5:53Etc these are small things that aren't
- 5:55going to be a lot of effort to implement
- 5:56they're not going to muddy up my code
- 5:58but they'll help make it just a little
- 6:00bit easier to implement the new features
- 6:01that I'm kind of anticipating possibly
- 6:03happening and the last question I'm
- 6:05going to leave you with is what's the
- 6:07scope and impact of the code high impact
- 6:10code such as the structure of a level
- 6:12should get more attention than code that
- 6:13is low in both scope and impact such as
- 6:16the callback for a specific function
- 6:17section on one menu that will never be
- 6:19reused remember Simplicity is often the
- 6:22best choice
About this transcript
This page contains the full transcript of A few common code smells to be aware of and how to fix them by The Shaggy Dev, generated from the public captions YouTube serves with the video. The transcript has 1,439 words across 202 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.