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

Java后端技术

共 2496字,需浏览 5分钟

 ·

2022-02-20 06:21

往期热门文章:

1、2021年互联网公司“死亡”名单!2022 年跳槽一定要谨慎些!2、京东程序员离职怒删代码被判10个月,京东到家请人花三万恢复!3、AlphaCode 惊世登场!编程版“阿法狗”悄悄参赛,击败一半程序员4、被阿里P8面了两个小时,技术、业务有来有回......5、再见丑陋的 SwaggerUI,这款API文档生成神器界面更炫酷,逼格更高!文章来源:http://33h.co/kntu3

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


定义配置文件信息


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


例如:

f7f61ae605cdfa93d0f34c2b1b65789d.webp

用 @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。


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

RequiredArgsConstructor:lombok 提供。


代码模块化


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


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

86d482c665f72dbb17af449eb008880b.webp

抛异常而不是返回


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


反例:e93803d916b14a0f45e96dadf286be38.webp正例:8f2ff5e75273c44afb3a3d978bb8fd6b.webp

减少不必要的db✦


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


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


反例:a8895bd5350c5a8ca1d807c3bac97ef1.webp正例:686214ce67a5eca7e5ba36d41899cdd7.webp

不要返回null✦


反例:677ac1bd718e6afc738d154b4bbecb64.webp正例:468b839e8a6e51ede4a4428035539ba1.webp

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


if else


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


减少controller业务代码


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


反例:e65c9b89887fb3301218166aadccd843.webp正例:ec39288877c4c7e19e8b811d73ea1a68.webp

利用好Idea


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


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


例如:04079fa20775d91980498f9926e91f6d.webp

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


点击 replace:aad3d04e4b92af25da7881fb1c65780c.webp

阅读源码


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


设计模式


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


拥抱新知识


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


基础问题


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

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

        });

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

        }


optional 判空:
    //获取子目录列表
    public List 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 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<Stringset = 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,因为其他业务场景可能会穿一个 list 有的会传一个 hashSet,用起来还要改,Map 也是。


正例:
Collection<String> getNodeIds(Collection<String> ids);


锁的颗粒度


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


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


缓存名称✦


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

a3e06dcb03579c15782e90d272de620b.webp

方便查看缓存信息。


@Cacheable✦


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


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

7487dbbf4f3ec14e7abe7da5ab68e2e2.webp


@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;
    }


异步任务


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


往期热门文章:

1、滴滴程序员被亲戚鄙视:年薪八十万还不如二本教书的...2、IT界惊现文豪!华为领导及阿里P10遭吐槽

3、上海地铁乘车码“变红”,吓倒一众乘客,官方:为营造节日气氛……4Spring Boot 项目打成 .exe 程序?实战来了!5Spring Boot太重,Vert.x真香!6中美程序员不完全对比7、Spring Boot 3.0 M1 发布,正式弃用 Java 8,最低要求 Java 17。。。8、一个“扛住100亿次请求”的春晚红包系统9、你觉得HTTPS能防止重放攻击吗?10、数据一致性,为什么不推荐双写?

浏览 42
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报