同事说,我写Java代码像写诗

「 关注“石杉的架构笔记”,大厂架构经验倾囊相授 

 儒猿技术团队最新出品

《基于ShardingSphere的分库分表实战》

74329452ff831ff9b41ed38291b21c2e.png


文章来源:http://33h.co/kntu3

前几天空闲时间写了一遍关于平时自己写代码的一些习惯,这里跟大家分享一下。

定义配置文件信息

有时候我们为了统一管理会把一些变量放到 yml 配置文件中。

例如:

fd665e613dbf2738bfa857ae4b71c179.png

用 @ConfigurationProperties 代替 @Value。

使用方法:

定义对应字段的实体:

@Data
// 指定前缀
@ConfigurationProperties(prefix = "developer")
@Component
public class DeveloperProperty {
    private String name;
    private String website;
    private String qq;
    private String phoneNumber;
}

使用时注入这个 bean:

@RestController
@RequiredArgsConstructor
public class PropertyController {

    final DeveloperProperty developerProperty;

    @GetMapping("/property")
    public Object index() {
       return developerProperty.getName();
    }
}

用@RequiredArgsConstructor代替@Autowired

我们都知道注入一个 bean 有三种方式哦(set 注入,构造器注入,注解注入),spring 推荐我们使用构造器的方式注入 Bean。

我们来看看上段代码编译完之后的样子:

b7ff37a0a76423e41c83b8b0c81e4fa8.png

RequiredArgsConstructor:lombok 提供。

代码模块化

阿里巴巴 Java 开发手册中说到每个方法的代码不要超过 50 行(我没记错的话)。

在实际的开发中我们要善于拆分自己的接口或方法,做到一个方法只处理一种逻辑,说不定以后某个功能就用到了,拿来即用。

24d09e9df3c0446f11c4874e99cfa197.png

抛异常而不是返回

在写业务代码的时候,经常会根据不同的结果返回不同的信息,尽量减少返回,会显得代码比较乱。

反例:

efdbee4ed91ff2d962a5e5943a6f7111.png

正例:

750b58e59521548cb9be03026132c888.png

减少不必要的db

尽可能的减少对数据库的查询。

举例子:删除一个服务(已下架或未上架的才能删除),之前有看别人写的代码,会先根据 id 查询该记录,然后做一些判断。

反例:

e4335745cbac08737c290452aa915266.png

正例:

17b16d6c815a4e92c8ff346185060bf4.png

不要返回null

反例:

e51958400969d40c487b57c4630bac0d.png

正例:

6df0c802befcbe7b29e2786ad127cda7.png

别处调用方法时,避免不必要的空指针。

if else

不要太多了 if else if,可以试试策略模式代替。

减少controller业务代码

业务代码尽量放到 service 层进行处理,后期维护起来也好操作而且美观。

反例:

c25750a5975c4e18a27dfefc0e833bc4.png

正例:

cd6694d0d5ca12f42a4d178690b4c78e.png

利用好Idea

目前为止市面上的企业基本都用 idea 作为开发工具了吧。

举一个小例子:idea 会对我们的代码进行判断,提出合理的建议。

例如:

b76ce96f8587b58cacadd5afe4a0124f.png

它推荐我们用 lanbda 的形式代替。

点击 replace:

2d645ce3c3288b90dfe5f408f7310682.png

阅读源码

一定要养成阅读源码的好习惯包括优秀的开源项目 GitHub上stars:>1000,会从中学好好多知识包括其对代码的设计思想以及高级 API,面试加分(好多面试官习惯问源码相关的知识)。

设计模式

23 种设计模式,要尝试代码中运用设计模式思想,写出的代码即规范又美观还高大上,哈哈。

拥抱新知识

像我们这种工作年限少的程序员,我觉得要多学习自己认知之外的知识,不能每天 crud,有机会就多用用有点难度的知识,没有机会(项目较传统),可以自己下班多些相关 demo 练习。

基础问题

map 遍历:

HashMap<String, String> map = new HashMap<>();
        map.put("name", "du");
        for (String key : map.keySet()) {
            String value = map.get(key);
        }

        map.forEach((k, v) -> {

        });

        // 推荐
        for (Map.Entry<String, String> entry : map.entrySet()) {

        }

optional 判空:

//获取子目录列表
    public List<CatalogueTreeNode> getChild(String pid) {
            if (V.isEmpty(pid)) {
            pid = BasicDic.TEMPORARY_DIRECTORY_ROOT;
        }
        CatalogueTreeNode node = treeNodeMap.get(pid);

        return Optional.ofNullable(node)
                .map(CatalogueTreeNode::getChild)
                .orElse(Collections.emptyList());
    }

递归

大数据量的递归时,避免在递归方法里 new 对象,可以试试把对象当作方法参数进行传递使用

注释

类、接口方法、注解、较复杂的方法,注释都要写而且要写清楚,有时候写注释不是给别人看的,而是给自己看的

判断元素是否存在

hashSet 而不是 list

list 判断一个元素是否存在的代码:

ArrayList<String> list = new ArrayList<>();

// 判断a是否在list中

for (int i = 0; i < list.size(); i++)
       if ("a".equals(elementData[i]))
          return i;

由此可见其复杂度为 On

而 hashSet 底层采用 hashMap 作为数据结构进行存储,元素都放到 map 的 key(即链表中)

HashSet<String> set = new HashSet<>();

// 判断a是否在set中

int index = hash(a);

return getNode(index) != null

由此可见其复杂度为 O1。

统一管理线程池

有的会在用到多线程的地方就创建一个线程池,为了统一管理,建议建立一个线程池,统一管理,统一设置非核心线程数,拒绝策略等等。

private static volatile ThreadPoolExecutor threadPoolExecutor = null;

    private static final int CORE_POOL_SIZE = 0;
    private static final int MAX_MUM_POOL_SIZE = 1000;
    private static final long KEEP_ALIVE_TIME = 2;
    private static final TimeUnit UNIT = TimeUnit.MINUTES;
    private static final int CAPACITY = 1;
    private static final RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();

    private static final BasicThreadFactory factory =
            new BasicThreadFactory.Builder().namingPattern("aiserviceplatform-util-thread-pool-%d").build();

    private ThreadPoolFactory() {
    }

    public static ThreadPoolExecutor getInstance() {
        if (threadPoolExecutor == null) {
            synchronized (ThreadPoolFactory.class) {
                if (threadPoolExecutor == null) {
                    threadPoolExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_MUM_POOL_SIZE, KEEP_ALIVE_TIME, UNIT, new ArrayBlockingQueue<>(CAPACITY), factory, HANDLER);
                }
            }
        }
        return threadPoolExecutor;
    }

大量数据同步

在同步大数据量的信息时(常用多线程方式:如果表里有唯一索引或者对表进行 update 操作那么就会经常锁表),优先考虑添加临时表的方式。

String realTabelName = ....;
String tempTableName = realTableName + "_temp";

createTable(tempTableName); // 创建临时表
boolean flag = sync(tempTableName); // 同步数据

// 根据结果
if(flag) {
    dropTable(realTabelName);
    alterTabelName(realTableName, tempTableName); // 临时表改名 实际表
}else {
    // 同步失败 删除临时表
    dropTabel(tempTableName)
}

实测:比对一张表做处理会快很多而且不会出问题。

接口入参

定义接口的方法时,如接受一个集合类型,尽可能的用 Collection<T>,因为其他业务场景可能会穿一个 list 有的会传一个 hashSet,用起来还要改,Map 也是。

正例:

Collection<String> getNodeIds(Collection<String> ids);

锁的颗粒度

同步方法直接在方法上加 synchronized 实现加锁,同步代码块则在方法内部加锁,很明显,同步方法锁的范围比较大,而同步代码块范围要小点,一般同步的范围越大,性能就越差,一般需要加锁进行同步的时候,肯定是范围越小越好,这样性能更好

我一般使用 lock:ReentrantLock,ReentrantReadWriteLock,减少锁的颗粒度,提高系统的并发能力。

缓存名称

简短,精练,以文件夹形式保存,xxx:xxx:xxx,以:隔开。

f0d91ab80405012d7fbb3c43b687b792.png

方便查看缓存信息。

@Cacheable

使用 Cacheable 做缓存时,也要加上缓存失效时间。

之前提出疑问怎么有的缓存没有失效时间,看代码使用 Cacheable 缓存的,不会添加就没加(???)。

e845854b3c9763971a886782364991bd.png

@Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        return new RedisCacheManager(
                RedisCacheWriter.lockingRedisCacheWriter(factory),
                this.getRedisCacheConfigurationWithTtl(1),
                this.getRedisCacheConfigurationMap()
        );
    }

    private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer hour) {
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        return RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(
                RedisSerializationContext
                        .SerializationPair
                        .fromSerializer(jackson2JsonRedisSerializer)).entryTtl(Duration.ofHours(hour));
    }

    public static final String REGION_LIST_BY_CODE_CACHE_KEY = "region:list";
    public static final String REGION_NAME_BY_CODE_CACHE_KEY = "region:name";

    /**
     * 已知缓存名称的映射以及用于这些缓存的配置
     */
    private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
        Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
        // 自定义缓存名称对应的配置
        redisCacheConfigurationMap.put(REGION_LIST_BY_CODE_CACHE_KEY, this.getRedisCacheConfigurationWithTtl(24));
        redisCacheConfigurationMap.put(REGION_NAME_BY_CODE_CACHE_KEY, this.getRedisCacheConfigurationWithTtl(120));
        redisCacheConfigurationMap.put(RedisKey.MARK_SERVICE_RENEW, this.getRedisCacheConfigurationWithTtl(-1));
        redisCacheConfigurationMap.put(RedisKey.MARK_ORGAN_RENEW, this.getRedisCacheConfigurationWithTtl(-1));
        redisCacheConfigurationMap.put(RedisKey.MARK_SERVICE_STREAM, this.getRedisCacheConfigurationWithTtl(-1));
        redisCacheConfigurationMap.put(RedisKey.MARK_ORGAN_STREAM, this.getRedisCacheConfigurationWithTtl(-1));
        return redisCacheConfigurationMap;
    }

异步任务

为了提高接口的响应速度,我们会把比较耗时的操作异步处理。例如异步任务,消息队列等。

-------------  END  -------------

帮您解忧,儒猿特别打造 “跳槽训练专栏系列”

内容试看(点击)   |    课程目录(点击)

bdfd6d3ef3466f3e770b2ad90d7022b3.png

610fad2089f8844e3de7119645efb92c.png

3086ada43bb30429e7479a099e549977.png

<<  滑动查看更多专栏  >>

e146b99a2cadd1a0a5b488d0a97c7667.png

点个在看你最好看

2981da3535f48a7bb50d9b3c1f8afd72.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值