组织Java常量的理想方式 [英] Ideal way to organize Java constants

查看:113
本文介绍了组织Java常量的理想方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个基于旧jdk 1.4的巨大项目。我们已将网络应用程序迁移到JDK 1.6,但仍然存在许多低效的做法和糟糕的设计。



主要的痛点巨大的java类2500+行的代码在单个java文件中。这么多的文件,像这样。



试图重构类,我开始通过删除常量和将常量放在不同的Constants.java文件。但由于应用程序中有很多常量,因此常量文件具有增长到非常大的风险。



我希望反馈开发人员采用什么样的策略保持代码的清晰和可维护性。

解决方案

将常数保存在他们相关的类中,不感到有义务提取他们。它可以清除类的代码,但是在文件中混合不相关的常量不是一个改进。



保持关联的。 / p>

也可以在可能/有用的情况下将它们转换为枚举(但这需要一些重构)。


We have a huge projects based on an old jdk 1.4. We have migrated the web app to JDK 1.6 but still lot of inefficient practices and bad design exist in the code.

On of the major pain point huge java classes 2500+ lines of code in single java file. So many files like these.

In an attempt to refactor the classes I had started off by removing constants and putting constants in different Constants.java file. but since there are so many constants through-out the application, the constants file has the risk of growing to humongous proportions.

I would appreciate feedback on what strategy are developers adopting to keep code clean and maintainable.

解决方案

Keep your constants in the class they are related to, don't feel obligated to extract them. It may clean up the code of the class, but mixing unrelated constants in a file is not an improvement.

Keep things related together.

And also you can convert them to Enums when possible/useful (but that can require some refactoring).

这篇关于组织Java常量的理想方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆