`

Clean Code: chapter 6~8

 
阅读更多

2011.04.07

Objects and Data Structures

 

Procedural code (code using data structures) makes it easy to add new functions without
changing the existing data structures. OO code, on the other hand, makes it easy to add
new classes without changing existing functions.


The complement is also true:
Procedural code makes it hard to add new data structures because all the functions must
change. OO code makes it hard to add new functions because all the classes must change.

 

So, the things that are hard for OO are easy for procedures, and the things that are
hard for procedures are easy for OO!
In any complex system there are going to be times when we want to add new data
types rather than new functions. For these cases objects and OO are most appropriate. On
the other hand, there will also be times when we’ll want to add new functions as opposed
to data types. In that case procedural code and data structures will be more appropriate.
Mature programmers know that the idea that everything is an object is a myth. Sometimes
you really do want simple data structures with procedures operating on them.

 

The Law of Demeter

 

There is a well-known heuristic called the Law of Demeter that says a module should not
know about the innards of the objects it manipulates.

 

They are indicative of a muddled design whose authors are unsure of—or worse, ignorant of—whether they need protection from functions or types.

 

i look on indicative of as a compound preposition.

 

2011.04.12

 

public class PerDiemMealExpenses implements MealExpenses {
    public int getTotal() {
        // return the per diem default
    }
}
 

This is called the SPECIAL CASE PATTERN [Fowler]. You create a class or configure an
object so that it handles a special case for you. When you do, the client code doesn’t have
to deal with exceptional behavior. That behavior is encapsulated in the special case object.

 

If you work in a code base with code like this, it might not look all that bad to you, but it is
bad! When we return null, we are essentially creating work for ourselves and foisting
problems upon our callers. All it takes is one missing null check to send an application
spinning out of control
.

 

If you code this way, you will minimize the chance of NullPointerExceptions and your
code will be cleaner.

 

you should avoid passing null in your code whenever possible.

 

Don't return null, don't pass null.

 

In most programming languages there is no good way to deal with a null that is
passed by a caller accidentally. Because this is the case, the rational approach is to forbid
passing null by default. When you do, you can code with the knowledge that a null in an
argument list is an indication of a problem, and end up with far fewer careless mistakes.

 

Clean code is readable, but it must also be robust. These are not conflicting goals. We can
write robust clean code if we see error handling as a separate concern, something that is
viewable independently of our main logic. To the degree that we are able to do that, we can
reason about it independently, and we can make great strides in the maintainability of our
code.

 

Learning tests verify that the third-party packages we are using work the way we
expect them to
.

 

Without these boundary tests to ease the migration, we might be tempted to stay with the old version longer than we should.

 

We had a pretty good idea of where our world ended and the new world began. As we
worked, we sometimes bumped up against this boundary. Though mists and clouds of
ignorance obscured our view beyond the boundary, our work made us aware of what we
wanted the boundary interface to be.

 



In the Figure above, you can see that we insulated the CommunicationsController classes
from the transmitter API (which was out of our control and undefined). By using our own
application specific interface, we kept our CommunicationsController code clean and
expressive. Once the transmitter API was defined, we wrote the TransmitterAdapter to
bridge the gap . The ADAPTER encapsulated the interaction with the API and provides a
single place to change when the API evolves
.

 

Interesting things happen at boundaries. Change is one of those things. Good software
designs accommodate change without huge investments and rework. When we use code
that is out of our control, special care must be taken to protect our investment and make
sure future change is not too costly.
Code at the boundaries needs clear separation and tests that define expectations. We
should avoid letting too much of our code know about the third-party particulars. It’s better
to depend on something you control than on something you don’t control, lest it end up
controlling you.
We manage third-party boundaries by having very few places in the code that refer to
them. We may wrap them as we did with Map, or we may use an ADAPTER to convert from
our perfect interface to the provided interface. Either way our code speaks to us better,
promotes internally consistent usage across the boundary, and has fewer maintenance
points when the third-party code changes.

 

 

 

 

 

 

  • 大小: 3.5 KB
分享到:
评论

相关推荐

    Clean.Code.Summary.B01AAS8T4A.pdf

    Chapter 1 Clean Code Chapter 2 Meaningful Names Chapter 3 Functions Chapter 4 Comments Chapter 5 Formatting Chapter 6 Objects and Data Structures Chapter 7 Error Handling Chapter 8 Boundaries Chapter ...

    24.Patterns.for.Clean.Code

    Robert lays out 24 patterns of thought and design that tend to produce faster, more reliable, and easier to maintain code without noticeably increasing the difficulty or decreasing the speed of coding...

    Clean Code

    But if code isn’t clean, it can bring a development organization to its knees. Every year, countless hours and significant resources are lost because of poorly written code. But it doesn’t have to ...

    Learn.CakePHP.With.Unit.Testing.1484212134

    Chapter 8: Model Tests Chapter 9: Controller Tests 1 Chapter 10: Mocks Chapter 11: Controller Tests 2 Chapter 12: Test Suites Chapter 13: Testing from Command Line Chapter 14: Goodies

    cleancode4noobs:清洁代码摘要(PT-BR)

    CleanCode4Noobs 罗伯特·塞西尔·马丁(Robert Cecil Martin)编写的《清洁代码:敏捷软件技巧手册》的摘要。 内容 参考 Robert C. Martin,“清洁代码:敏捷软件Craft.io手册” 如何贡献 贡献使开源社区成为了一...

    Python.Crash.Course.A.Hands-On.Project-Based.Introduction.to.Programming

    Chapter 8: Functions Chapter 9: Classes Chapter 10: Files and Exceptions Chapter 11: Testing Your Code Part II: Projects Project 1: Alien Invasion Project 2: Data Visualization Project 3: Web ...

    Practical.Swift

    Chapter 8: Testing Part II: Building the Grocery App Chapter 9: Grocery List App Interface Builder Chapter 10: Grocery App: MVVM Chapter 11: Grocery App: Core Data Chapter 12: Grocery App: Finish ...

    Apache.Maven.Cookbook.1785286129

    Over 90 hands-on recipes to successfully build ...Chapter 8: Handling Typical Build Requirements Chapter 9: Multimodule Projects Chapter 10: Java Development with Maven Chapter 11: Advanced Maven Usage

    Clean Architectures in Python A practical approach to better software design

    The first chapter discusses briefly the components and the ideas behind this software structure, while chapter 2 runs through a concrete example of clean architecture for a very simple web service....

    Learn.Mobile.Game.Development.in.One.Day.Using.Gamesalad

    Chapter 8: Behaviors Part 1 Chapter 9: Behaviors Part 2 Chapter 10: Player Input Chapter 11: Game 2 – Space Shooter Chapter 12: Audio Chapter 13: The Expression Editor Chapter 14: Collisions and ...

    C# Primer 中文版随书源码

    There are eight directories, ...Chapter 8: The Common Language Runtime Assemblies Attributes collectionBinary ildasm reflection serialize tester timer Pictures Textfiles Xmlfiles

    R.Unleash.Machine.Learning.Techniques

    Chapter 8: Finding Patterns – Market Basket Analysis Using Association Rules Chapter 9: Finding Groups of Data – Clustering with k-means Chapter 10: Evaluating Model Performance Chapter 11: ...

    Web.Scraping.with.Python.Collecting.Data.from.the.Modern.Web

    Chapter 6. Reading Documents Part II. Advanced Scraping Chapter 7. Cleaning Your Dirty Data Chapter 8. Reading and Writing Natural Languages Chapter 9. Crawling Through Forms and Logins Chapter 10. ...

    Less.Web.Development.Essentials.2nd.Edition

    Title: Less Web Development Essentials, 2nd Edition Author: Bass Jobsen ...Chapter 6: Using The Bootstrap 3 Frontend Framework Chapter 7: Less With External Applications And Frameworks

    ASP.NET.Web.API.and.Angular.2.17864

    Key Features Learn how to design Single Page ...Chapter 8: Third-Party Authentication and External Providers Chapter 9: User Registration and Account Edit Chapter 10: Finalization and Deployment

    Test-.Driven.Python.Development.1783987928

    Chapter 6. Maintaining Your Test Suite Chapter 7. Executable Documentation with doctest Chapter 8. Extending unittest with nose2 Chapter 9. Unit Testing Patterns Chapter 10. Tools to Improve Test-...

    Swift.by.Example.1785284703

    Create funky, impressive applications using Swift About This Book Learn Swift language features quickly, with playgrounds and in-depth examples Implement real iOS ...Chapter 8: Completing Cube Runner

    Programming.Google.App.Engine.with.Java

    Because App Engine supports common Java API standards, your code stays clean and portable. Get a hands-on introduction to App Engine's tools and features, using an example application Simulate App ...

    avaScript.Projects.for.Kids.1785

    Finally, it will show you a few tricks with OOP to make your code clean and will end with a few road maps on areas you can explore further. Table of Contents Chapter 1. Exploring JavaScript in the ...

Global site tag (gtag.js) - Google Analytics