您的Java代码真的可以投入生产吗?

您的Java真的准备好投入生产了吗?

这里有五个技巧,可以防止用户滥用Java!

Duke, the Java mascot, rocking out

Write dumb code

My article Why Senior Devs Write Dumb Code and How to Spot a Junior From A 中号ile Away is still shared often on Twitter even though its almost two years old. The advice to write dumb code isn't mine, it's Java architect Brian Goetz's, who said:

通常,在Java应用程序中编写快速代码的方法是编写愚蠢的代码,这些代码简单,干净并且遵循最明显的面向对象原则。

This is an extension of the KISS principle and basically means: avoid being clever. Write simple code the way it was meant to be written. Know and write effective Java.

但是“哑代码”还有另一个优点:编译器知道如何对其进行优化。 因此,您也可以提高性能。

Start with the most defensive option

您可能已经听过一些建议,建议您限制方法或变量的可见性,并限制有关封装的内容。 听起来听起来很无聊,科学且令人产生哈欠。

Duke, the Java mascot, experimenting in a chemistry lab

同时,大多数Java教程都会提供类的示例,其中唯一使用的可访问性修饰符是上市. IDE class generation also seems to default to 上市 classes and methods. If you fully generate a class in an IDE, you're bound to end up with something like this:

public class Contact {
    private String name;
    private String emailAddress;

    public Contact(String name, String emailAddress) {
        this.name = name;
        this.emailAddress = emailAddress;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }
}

On the contrary, I advise devs to start with the most defensive option possible and relax to a less defensive option only when necessary. So for the above example, make each member variable final and avoid implementing getters or setters unless you can prove you need them.

Furthermore, default to package-private for any constructors or methods. It may be helpful to review Java access modifiers.

因此,先验代码在变成其他东西之前变成了下面的代码:

class Contact {
    private final String name;
    private final String emailAddress;

    Contact(String name, String emailAddress) {
        this.name = name;
        this.emailAddress = emailAddress;
    }
}

编写这样的代码的开发人员正在防御性地考虑范围和不变性,这是一件好事。 此外,这引出我的下一个观点。

Duke, the Java mascot, giving a thumbs up

Prefer to expose behavior over state

You may have noticed that you can't do anything with an instance of Contact that's written according to my "starting point" guideline. Exactly! So you may be wondering where do you go from there? And my answer is: prefer to expose behavior over state.

This forces the question, what do you want to do with Contact? There's a big difference between a data transfer object whose purpose actually is to carry state somewhere and a normal object whose purpose should be to provide behavior.

也许后者很难想象,所以让我提供一个简单的邮件应用程序示例,其中我们需要从联系人列表中发送电子邮件的功能。 在一个简单的邮件应用中,我建议联系这种行为的根源。

public class Contact implements Emailable {
    // ...

    @Override
    public EmailResult sendEmailTo(Contact destination, String body) {
        // ...
    }
}

随着应用程序的发展,这种方法也许已不复存在。 但这是一个好主意。 请注意,构成状态的名称和电子邮件地址联系对象也在该对象内使用。 这样可以将状态和行为整齐地组织在一起,并且无需公开共享任何状态。

And if the purpose of Contactis to transfer data, make it an immutable object which is both thread-safe and protected from accidental programming errors that set its values incorrectly.

Duke, the Java mascot, holding a globe

Write comprehensive tests

在截止日期的压力下,测试通常是放在砧板上的第一步。 人类心理学的不幸结果可能是致命的。 除非另有证明,否则大型复杂应用程序中的Java应该被认为是错误的。 只有测试才能证明是相反的。

除非另有证明,否则大型复杂应用程序中的Java应该被认为是错误的。

Get to know a test framework like JUnit and related tools like Mockito.

Two great resources to learn great testing practices include xUnit Ťest Patterns by Gerard Meszaros and Pragmatic Unit Ťesting in Java 8 by Jeff Langr.

Fix linter and IDE warnings

In addition to what the compiler tells you, let the IDE and static analysis tools like CheckStyle inform you of ways your code can be more modern, secure, and correct. IntelliJ IDEA has a great code inspections feature that is like having a Java expert at your side as you build your application. Don't waste that advice!

IntelliJ Code Inspections, courtesy of IntellIJ Hel
IntelliJ Code Inspections, courtesy of IntellIJ Help

Production Java!

遵循这些技巧将产生可用于生产环境的Java! 那就是:可以承受狂野的Java。 Java是一门很棒的语言,具有有用的编译器以及强大的工具和库生态系统,可帮助提高并确保质量。 通过遵循最佳实践并利用这些工具,我相信任何应用程序都可以得到改进,并且您作为Java开发人员可以专注于重要的事情-构建应用程序-而不是花费所有时间来修复那些 本来可以早点抓到的。 祝好运!

from: https://dev.to//scottshipp/is-your-java-code-really-production-ready-ogp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值