Embedded

273: Off the Topic of My Jammies


Listen Later

Chris and Elecia chat with each other about the new year. All is fine until she starts quizzing him about some language details of his new project.

Many object-oriented resources suggest using composition (has-a) over inheritance (is-a-type-of) (wiki). Where do swift extensions fit in? It seems to me (Elecia here) that extension is invisible composition that allows adding of functions.

For example, say you want a TiltSensor and you already have an ImuSensor object so you need to add a function for TiltComputation.

You could make the TiltSensor contain an ImuSensor (composition). You call the ImuSensor functions to check the readings when running TiltComputation function. You don't need to know what is in ImuSensor, only what the API is.

You could have TiltSensor be a child class of ImuSensor (inheritance) so that TiltSensor responds to all ImuSensor functions as well as its new TiltComputation function. You could use the variables in ImuSensor directly for TiltCompulation but you will need to know what is in ImuSensor for that to work.

Or, in Swift, you could have TiltSensor be an extension of ImuSensor. Except it wouldn't be called TiltSensor, it would be part of ImuSensor: any file that had access to your extensions would be able to create an ImuSensor instance and call TiltComputation as if it was part of the original ImuSensor API. The TiltComputation function would only have access to its extension's variables and ImuSensor's API. You get to add new functionality without breaking backward compatibility.

Some more resources on this topic:

  • Composing Types in Swift

  • Swift Language Guide: Extensions

Swift Programming: The Big Nerd Ranch Guide by Matthew Mathias and John Gallagher

iOS Programming: The Big Nerd Ranch Guide by Christian Keur and Aaron Hillegass

LinkedIn Learning Courses

Blender Beta with EEVEE renderer

The Clean Coder: A Code of Conduct for Professional Programmers by Robert C. Martin

Code Complete: A Practical Handbook of Software Construction by Steve McConnell

Gelly Roll Glitter Pens (by Sakura)

Google Podcast Link (or see the Subscribe page)

...more
View all episodesView all episodes
Download on the App Store

EmbeddedBy Logical Elegance

  • 4.8
  • 4.8
  • 4.8
  • 4.8
  • 4.8

4.8

187 ratings


More shows like Embedded

View all
Radiolab by WNYC Studios

Radiolab

43,979 Listeners

Freakonomics Radio by Freakonomics Radio + Stitcher

Freakonomics Radio

32,111 Listeners

Planet Money by NPR

Planet Money

30,667 Listeners

Hanselminutes with Scott Hanselman by Scott Hanselman

Hanselminutes with Scott Hanselman

384 Listeners

The Changelog: Software Development, Open Source by Changelog Media

The Changelog: Software Development, Open Source

292 Listeners

Software Engineering Daily by Software Engineering Daily

Software Engineering Daily

625 Listeners

Talk Python To Me by Michael Kennedy

Talk Python To Me

585 Listeners

The Amp Hour Electronics Podcast by The Amp Hour (Chris Gammell and David L Jones)

The Amp Hour Electronics Podcast

232 Listeners

Python Bytes by Michael Kennedy and Brian Okken

Python Bytes

214 Listeners

Late Night Linux by The Late Night Linux Family

Late Night Linux

164 Listeners

Unnamed Reverse Engineering Podcast by Jen Costillo and Alvaro Prieto

Unnamed Reverse Engineering Podcast

40 Listeners

The Indicator from Planet Money by NPR

The Indicator from Planet Money

9,529 Listeners

CoRecursive: Coding Stories by Adam Gordon Bell - Software Developer

CoRecursive: Coding Stories

189 Listeners

The Real Python Podcast by Real Python

The Real Python Podcast

141 Listeners

Oxide and Friends by Oxide Computer Company

Oxide and Friends

60 Listeners