Coding Blocks

After 112 episodes, Michael can’t introduce the show, Allen pronounces it “ma-meee”, and don’t make Joe run your janky tests as The Pragmatic Programmer teaches us how we should use exceptions and program deliberately.

How are you reading this? If you answered via your podcast player, you can find this episode’s full show notes and join the conversation at https://www.codingblocks.net/episode113.

 

Sponsors

  • Datadog.com/codingblocks – Sign up today for a free 14 day trial and get a free Datadog t-shirt after creating your first dashboard.

Survey Says …

When you want to bring in a new technology or take a new approach when implementing something new or add to the tech stack, do you ...?

Take the survey here:
https://www.codingblocks.net/episode113

 

News

  • Thank you for taking a moment out of your day to leave us a review.
    • iTunes: MatteKarla, WinnerOfTheRaceCondition, michael_mancuso
    • Stitcher: rundevcycle, Canmichaelpronouncethis, WinnerOfTheRaceCondition, C_Flat_Fella, UncleBobsNephew, alexUnique
  • Autonomous ErgoChair 2 Review (YouTube)
  • Come see us Saturday, September 14, 2019 at the Atlanta Code Camp 2019 (atlantacodecamp.com)
  • Are they cakes, cookies, or biscuits? (Wikipedia)

Intentional Code

When to use Exceptions

  • In an earlier chapter, Dead Programs Tell No Lies, the book recommends:
    • Checking for every possible error.
    • Favor crashing your program over running into an inconsistent state.
  • This can get really ugly! Especially if you believe in the “one return at the bottom” methodology for your methods.
  • You can accomplish the same thing by just catching an exception for a block of code, and throwing your own with additional information.
  • This is nice, but it brings up the question? When should you return a failed status, and when should you throw an exception?
  • Do you tend to throw more exceptions in one layer more than another, such as throwing more in your C# layer than your JS layer?
  • The authors advise throwing exceptions for unexpected events.
  • Ask yourself, will the code still work if I remove the exception handlers? If you answered “no”, then maybe your throwing exceptions for non-exceptional circumstances.
Tip 34
  • Use exceptions for exceptional problems

Exceptions vs Error Handling

  • Should you throw an exception if you try to open a file, and it doesn’t exist?
    • If it should be there, i.e. a config, yes, throw the exception.
    • If it might be OK for it not to be there, i.e. you’re polling for a file to be created, then no, you should handle the error condition.
  • Is it dangerous to rely on implicit exception throwing, i.e. opening a file that isn’t there?
    • On the one hand, it’s cleaner without checking for the exceptions, but there’s no signaling to your co-coders that you did this intentionally.
    • Exceptions are a kind of coupling because they break the normal input/output contract.
  • Some languages / frameworks allow you to register error handlers that are outside the flow of the normal problem.
    • This is great for certain types of problems, like serialization problems, particularly when there is a prescribed flow, such as error pages, serialization, or SSL errors.

Programming by Coincidence

  • What does it mean to “program by coincidence”?
    • Getting lured into a false sense of security and then getting hit by what you were trying to avoid.
  • Avoid programming by coincidence and instead program deliberately. Don’t rely on being lucky.
  • Writing code and seeing that it works without fully understanding why is how you program by coincidence.
    • This really becomes a problem when something goes wrong and you can’t figure out why because you never knew why it worked to start off with.
  • We may not be innocent …
    • What if you write code that adheres to some other code that was done in error … if that code is eventually fixed, your own code may fail.
  • So if it’s working, why would you touch it?
    • It might not actually be working …
      • Maybe it doesn’t work with a different resolution.
      • Undocumented code might change, thus changing your “luck”.
      • Unnecessary method calls slow down the code.
      • Those extra calls increase the risk of bugs.
  • Write code that others implement with well documented code that adheres to a contract.

Accidents of Context

  • You can also make the mistake that you assume certain things are a given, such as that there’s a UI or that there’s a given language.

Implicit Assumptions

  • Don’t assume something, prove it.
  • Assumptions that aren’t based on fact become a major sticking point in many cases.
Tip 44
  • Don’t Program by Coincidence

How to Program Deliberately

  • Always be aware of what you’re doing.
  • Don’t code blindfolded, Make sure you understand what you’re programming in, both business domain related and programming language.
  • Code from a plan.
  • Rely on reliable things. Don’t code based on assumptions.
  • Document assumptions.
  • Test your code _and_ your assumptions.
  • Prioritize and spend time on the most important aspects first.
  • Don’t let old code dictate new code. Be prepared to refactor if necessary.

Resources We Like

  • The Pragmatic Programmer by Andrew Hunt, David Thomas (Amazon)
  • The Pragmatic Bookshelf (pragprog.com)
  • Oh mother… | Family Feud (YouTube)
    • OMG! It’s here! Oh mother… the ON-AIR VERSION!!! | Family Feud (YouTube)
  • Thunder Talks (episode 87)
  • Spotify engineering culture (part 1) (labs.spotify.com)

Tip of the Week

Direct download: coding-blocks-episode-113.mp3
Category:Software Development -- posted at: 8:01pm EDT

We continue our dive into The Pragmatic Programmer and debate when is it text manipulation vs code generation as Joe can’t read his bill, Michael makes a painful recommendation, and Allen’s gaming lives up to Southern expectations.

In case you’re reading these show notes via your podcast player, you can find this episode’s full show notes at https://www.codingblocks.net/episode112 and join in on the conversation.

Sponsors

  • Clubhouse – The first project management platform for software development that brings everyone on every team together to build better products. Sign up for two free months of Clubhouse by visiting clubhouse.io/codingblocks.
  • Datadog.com/codingblocks – Sign up today for a free 14 day trial and get a free Datadog t-shirt after creating your first dashboard.

Survey Says …

What native language are you most interested in?

Take the survey here:
https://www.codingblocks.net/episode112

News

  • We really appreciate every review we get. Thank you for taking the time.
    • iTunes: AsIRoseOneMorn, MrBramme, MP7373, tbone189, BernieF1982, Davidwrpayne, mldennison
    • Stitcher: Ben T, moreginger, Tomski, Java Joe

Blurring the Text Manipulation Line

Text Manipulation

  • Programmers manipulate text the same way woodworkers shape wood.
    • Text manipulation tools are like routers: noisy, messy, brutish.
  • You can use text manipulation tools to trim the data into shape.
    • Once you master them, they can provide an impressive amount of finesse.
  • Alternative is to build a more polished tool, check it in, test it, etc.
Tip 28
  • Learn a Text Manipulation Language.

Code Generators

  • When you have a repetitive task, why not generate it?
  • The generated code takes away complexity and reduces errors.
  • And it’s reuse has little to no additional cost.
Tip 29
  • Write Code That Writes Code.

There are two types of code generators:

  1. Passive code generators are run once (scaffolding).
  2. Active code generators are used each time they are required.

Passive code generators save typing by automating…

  • New files from a template, i.e. the “File -> New” experience.
  • One off conversions (one language to another).
    • These don’t need to be completely perfect.
  • Producing lookup tables and other resources that are expensive to compute.
  • Full-fledged source file.

You get to pick how accurate you want the generators to be. Maybe it writes 80% of the code for you and you do the rest by hand.

Active code generators

  • Active code generators are necessary if you want to adhere to the DRY principle.
    • This form is not considered duplication because it’s generated as needed by taking a single representation and converting it to all of the forms you need.
  • Great for system boundaries (think databases or web services).
  • Great for keeping things in sync.
  • Recommend creating your own parser.

Why generate when you can just … program?

  • Scaffolding, so it’s a starting off point that you edit.
  • Performance.
  • System boundaries.
  • Some uses work best when built into your build pipeline.
    • Think about automatically generating code to match your DB at compile time, like a T4 generator for Entity Framework.
  • It’s often easier to express the code to be generated in a language neutral representation so that it can be output in multiple languages.
    • Something like System.CodeDom comes to mind.
  • These generators don’t need to be complex.
  • And the output doesn’t always need to be code. It could be XML, JSON, etc.

Resources We Like

Tip of the Week

  • Within Visual Studio Code, after you use CTRL+F to find some text in your current document, you can take it a step further by pressing ALT+ENTER to enter into block selection/edit mode.
  • Turn learning Vi into a game with VIM Adventures. (vim-adventures.com)
  • Never confuse forward Slash with back Slash again.
Direct download: coding-blocks-episode-112.mp3
Category:Software Development -- posted at: 8:01pm EDT

1