# A Caution On Superfluous Code

DevFeed: [A Caution On Superfluous Code](<https://devfeed.tech/articles/a-caution-on-superfluous-code-22301.md>)

Original publisher: [Read original article](<https://www.thecodedself.com/A-Caution-On-Superfluous-Code/>)

Author: Keegan Rush

Published: 2016-12-15T00:00:00Z

Content type: article

Language: en

Sources: [The Coded Self](<https://devfeed.tech/sources/the-coded-self.md>)

Topics: [Code](<https://devfeed.tech/topics/code.md>), [Library](<https://devfeed.tech/topics/library.md>), [SDKs](<https://devfeed.tech/topics/sdks.md>), [Swift](<https://devfeed.tech/topics/swift.md>), [Programming](<https://devfeed.tech/topics/programming.md>)

Tags: [bugs](<https://devfeed.tech/tags/bugs.md>), [code](<https://devfeed.tech/tags/code.md>), [improvements](<https://devfeed.tech/tags/improvements.md>), [library](<https://devfeed.tech/tags/library.md>), [performance](<https://devfeed.tech/tags/performance.md>), [sdks](<https://devfeed.tech/tags/sdks.md>), [standard-library](<https://devfeed.tech/tags/standard-library.md>), [swift](<https://devfeed.tech/tags/swift.md>)

## AI overview

The article cautions developers against writing superfluous code that adds complexity without changing product behavior. It attributes this problem partly to insufficient understanding of a language or library, and recommends using existing standard-library and platform SDK functionality when it meets the need. A Swift NotificationCenter example shows how an unnecessary registration flag can be removed because removing an observer is safe even when it is not registered.

## Source excerpt

The line of code that the developer can write the fastest, the line of code that the developer can maintain the cheapest, and the line of code that never breaks for the user, is the line of code that the developer never had to write. - Steve Jobs Superfluous code is code that is written unnecessarily. It is code that has all the added complexity of valuable code, but it adds no value of its own. If you were to remove it, the product would behave exactly the same. I can think of a few causes of superfluous code: Improper understanding of the toolset If a developer doesn't properly understand the language or the library that is being used, how can he be certain that the code he writes is suited to the task at hand? Oftentimes code is written that duplicates functionality included in the standard library. Now the same functionality exists in two different places: in your code base, and in your library. It has to be maintained twice. And tested twice. For complex algorithms, you're losing the benefit of years and years of bug fixes and performance improvements to the implementation that's available to you through your toolset. Take the time to understand what is available to you. If it suits your needs, use it. Don't write superfluous code when you can write no code and gain the same value from your platform's SDKs. But, if you don't know what is available to you, you're doomed to repeat the bugs of the past. I recently came across the following Swift code: import Foundation class Foo { var isRegisteredForNotifications = false init() { registerForNotifications() } func registerForNotifications() { isRegisteredForNotifications = true NotificationCenter.default.addObserver(forName:Notification.Name(rawValue:"MyNotification"), object:nil, queue:nil, usingBlock:notificationWasFired) } func notificationWasFired(notification: Notification) { /* . . */ } deinit { if isRegisteredForNotifications { NotificationCenter.removeObserver(self) } } } NotificationCenter is a class in Sw