惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
AWS News Blog
AWS News Blog
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
美团技术团队
T
Threatpost
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
Blog — PlanetScale
Blog — PlanetScale
C
Cybersecurity and Infrastructure Security Agency CISA
F
Full Disclosure
博客园_首页
N
Netflix TechBlog - Medium
Security Latest
Security Latest
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Announcements
Recent Announcements
博客园 - Franky
P
Palo Alto Networks Blog
Project Zero
Project Zero
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
GbyAI
GbyAI

博客园 - idior

每日代码 - 7/1 减小方法参数的依赖 每日代码 - 6/29 读写分离 每日代码 - 6/28 代码逻辑分组 每日代码 - 6/26 lambda表达式 Resume Covariance and Contravariance How does ElementName Binding work? Weird behavior of DataContext Inheritance How does ElementName Binding work – Part 3 InheritanceContext How does ElementName Binding work – Part 2 BindingExpression Logical Tree & Visual Tree Memory leak caused by EventHandle - weak event Resources on Debugging/Tracing WPF How does ElementName Binding work? - Part 1 Logical Tree & NameScope Inside WCF Runtime 有谁准备参加10月份的MVP聚会? MVP聚会 Practical .NET2 and C#2 翻译样章 Be evil or not?
每日代码 - 6/27 避免创建非法对象
idior · 2013-06-27 · via 博客园 - idior
 
public class BuyerHelper
{
    private static final UserRepository userRepository;

    public static void createAndCheckBuyer(Cart cart)
    {
        int userId = cart.getUserId();
        User user = userRepository.getUserById(userId);

        Buyer buyer = BuyFactory.createFrom(user);
        BaseCheckResult checkResult = BuyerChecker.checkBuyerPermission(buyer);
        
        if (checkResult.isSuccess())
            cart.setBuyer(buyer);
        else
            throw new InvaildBuyerException();        
    }
}

很简单的一段代码,存在以下问题:

1. 责任不清晰。

该方法既包含了验证用户是否是一个合法买家的功能,又实现了根据用户创建买家的功能,并且还隐藏了一个很危险的功能,它还负责将这个创建好的买家设置到cart的属性。而cart是一个外来参数,这意味着后续的代码中很有可能会依赖这个被附加到cart上的buyer属性上,而这一点其实是比较隐晦的。

2. 创建非法对象。

本方法实现的功能应该是判断一个用户是否是一个有效的买家,理论上来说如果一个用户不是有效买家的话,就不应该出现一个买家对象。但是,在这段代码中,确实先不管三七二十一先把Buyer对象创建出来,然后再对它进行验证。我们应该尽力避免创建非法对象。