Composing Software

Book description

Unlock the power of functional programming in JavaScript with this detailed guide. Master essential techniques like function composition, immutability, and higher-order functions to create modular, maintainable code that's easy to understand and reuse.

Key Features

  • An exploration of functional programming in JavaScript, from basics to advanced concepts
  • An extensive focus on real-world applications & problem-solving techniques
  • An introduction to both functional and object-oriented programming for a holistic view

Book Description

This book delves into functional programming and composition techniques in JavaScript, starting with core concepts like pure functions, shared state avoidance, and higher-order functions to build modular, maintainable code. Early chapters explore the fundamentals of functional programming, immutability, and its growing influence in the JavaScript community. You'll learn essential topics such as function composition, currying, and higher-order functions, as well as advanced concepts like abstract data types, functors, and monads. The book discusses the evolution of functional programming, its role in modern software development, and addresses challenges like the software crisis and composing with classes.

You'll learn essential topics like object-oriented programming, focusing on factory functions, functional mixins, and object composition. You'll also understand why traditional classes complicate composition and discover strategies for creating custom data types, lenses, and transducers. The book also covers best practices, emphasizing clean, reusable code and avoiding anti-patterns like excessive mocking.

By the end, you'll be ready to apply functional programming techniques to tackle complex design challenges and write more maintainable JavaScript code.

What you will learn

  • Grasp core principles for writing clean, efficient code
  • Combine functions to create complex operations with ease
  • Write functions that are predictable and easy to test
  • Eliminate shared state and side effects for more reliable code
  • Use functions as arguments or return values for flexibility
  • Learn data transformations and side effect management

Who this book is for

The book is ideal for JavaScript developers who want to deepen their understanding of functional programming and software composition. Familiarity with JavaScript basics is required. This book is suitable for both intermediate and advanced programmers who want to write more maintainable and modular code.

Table of contents

  1. Thank You
  2. Composing Software: An Introduction
    1. You Compose Software Every Day
    2. Conclusion
  3. The Dao of Immutability (The Way of the Functional Programmer)
    1. Forward
  4. The Rise and Fall and Rise of Functional Programming (Composable Software)
    1. The Rise of Functional Programming
    2. The Fall of Functional Programming
    3. The Rise of Functional Programming
    4. Functional Programming Has Always Been Alive and Well
  5. Why Learn Functional Programming in JavaScript?
  6. Pure Functions
    1. What is a Function?
    2. Mapping
    3. Pure Functions
    4. The Trouble with Shared State
    5. Same Input, Same Output
    6. No Side Effects
    7. Conclusion
  7. What is Functional Programming?
    1. Pure Functions
    2. Function Composition
    3. Shared State
    4. Immutability
    5. Side Effects
    6. Reusability Through Higher Order Functions
    7. Containers, Functors, Lists, and Streams
    8. Declarative vs Imperative
    9. Conclusion
  8. A Functional Programmer’s Introduction to JavaScript
    1. Expressions and Values
    2. Types
    3. Destructuring
    4. Comparisons and Ternaries
    5. Functions
    6. Currying
    7. Function Composition
    8. Arrays
    9. Method Chaining
    10. Conclusion
  9. Higher Order Functions
  10. Curry and Function Composition
    1. What is a curried function?
    2. What is a partial application?
    3. What’s the Difference?
    4. What is point-free style?
    5. Why do we curry?
    6. Trace
    7. Curry and Function Composition, Together
    8. Conclusion
  11. Abstraction & Composition
    1. Abstraction is simplification.
    2. Abstraction in Software
    3. Abstraction through composition
    4. How to Do More with Less Code
    5. Conclusion
    6. Reduce
    7. Reduce is Versatile
    8. Conclusion
  12. Abstract Data Types and the Software Crisis
    1. Common ADT Examples
    2. Why ADTs?
    3. History of ADTs
    4. Specifications for ADTs
    5. Stack ADT Example
    6. Concrete Implementations
    7. Conclusion
    8. Glossary
  13. Functors & Categories
    1. Why Functors?
    2. Functor Laws
    3. Category Theory
    4. Build Your Own Functor
    5. Curried Map
    6. Conclusion
  14. Monads
    1. You’re probably already using monads.
    2. What Monads are Made of
    3. Building a Kleisli Composition Function
    4. The Monad Laws
    5. Conclusion
  15. The Forgotten History of OOP
    1. The Big Idea
    2. The Essence of OOP
    3. What OOP Doesn’t Mean
    4. What is an object?
    5. We’ve lost the plot.
  16. Object Composition
    1. What is Object Composition?
    2. Three Different Forms of Object Composition
    3. Notes on Code Examples
    4. Aggregation
    5. Concatenation
    6. Delegation
    7. Conclusion
  17. Factory Functions
    1. Literals for One, Factories for Many
    2. Returning Objects
    3. Destructuring
    4. Computed Property Keys
    5. Default Parameters
    6. Type Inference
    7. Factory Functions for Mixin Composition
    8. Conclusion
  18. Functional Mixins
    1. Motivation
    2. What are mixins?
    3. What is functional inheritance?
    4. What is a functional mixin?
    5. Composing Functional Mixins
    6. When to Use Functional Mixins
    7. Caveats
    8. Conclusion
  19. Why Composition is Harder with Classes
    1. The Delegate Prototype
    2. The .constructor Property
    3. Class to Factory is a Breaking Change
  20. Composable Custom Data Types
    1. You can do this with any data type
    2. Composable Currency
  21. Lenses
    1. Why Lenses?
    2. Background
    3. Lens Laws
    4. Composing Lenses
  22. Transducers
    1. Why Transducers?
    2. Background and Etymology
    3. A Musical Analogy for Transducers
    4. Transducers compose top-to-bottom.
    5. Transducer Rules
    6. Transducing
    7. The Transducer Protocol
    8. Conclusion
  23. Elements of JavaScript Style
    1. 1. Make the function the unit of composition. One job for each function.
    2. 2. Omit needless code.
    3. 3. Use active voice.
    4. 4. Avoid a succession of loose statements.
    5. 5. Keep related code together.
    6. 6. Put statements and expressions in positive form.
    7. 7. Use parallel code for parallel concepts.
    8. Conclusion: Code should be simple, not simplistic.
  24. Mocking is a Code Smell
    1. TDD should lead to better design.
    2. What is a code smell?
    3. What is a mock?
    4. What is a unit test?
    5. What is test coverage?
    6. What is tight coupling?
    7. What causes tight coupling?
    8. What does composition have to do with mocking?
    9. How do we remove coupling?
    10. “Code smells” are warning signs, not laws. Mocks are not evil.

Product information

  • Title: Composing Software
  • Author(s): Eric Elliott
  • Release date: September 2024
  • Publisher(s): Packt Publishing
  • ISBN: 9781836644637