Java coding style - Part one

Java coding style:

1. Factory method should be stateless.

State normally refers to the member variables of class. Stateless, more precisely, it means immutable.

Factory is just to create objects, and one call should not affect anothers, if it is mutable, then the method call mnight have the chance to change the states.
and then it would affect other calls which are not supposed to happen.

2.
Restrict the access level of all the class members as much as possible, this conforms to principle of encapsulation

3.
Assert to validate the parameters of constructor, this way to avoid the invalid data of member variables as early as in the construction stage
e.g. in Spring, if you assert to validate the values, intead of leaving the error to later, application even won‘t be able to start up when Spring tries to initialise these objects.

4.
Try best to use constructor to initialise the object, this way you can validate the assigned values to member variables, you can construct the object
in one place.

e.g. in Spring, using constructor to init the beans, you can make the member variables as "final", this way, the member variables get assigned and initialised
early in the construction stage, you don‘t have to create an empty object first and then set the fields using setters. What‘ more, the member variables won‘t be exposed to others
in this case, so you get a better access control over these member variables.

时间: 2024-08-11 09:42:36

Java coding style - Part one的相关文章

Google's C++ coding style

v0.2 - Last updated November 8, 2013 源自 Google's C++ coding style rev. 3.274 目录 由 DocToc生成     头文件        #define用法        前向声明        内联函数        -inl.h文件        函数参数顺序        include的命名和顺序    作用域        命名空间            未命名空间            命名空间       

编程风格(Coding Style)要求

编程风格(Coding Style)要求2.1.1 文件(1) 每个模块(module)一般应存在于单独的源文件中,通常源文件名与所包含模块名相同.(2) 每个设计文件开头应包含如下注释内容:? 年份及公司名称.? 作者.? 文件名.? 所属项目.? 顶层模块.? 模块名称及其描述.? 修改纪录.2.1.2 大小写(1) 如无特别需要,模块名和信号名一律采用小写字母.(2) 为醒目起见,常数(`define定义)/参数(parameter定义)采用大写字母.2.1.3 标识符(1) 标识符采用传

linux c coding style

Linux kernel coding style This is a short document describing the preferred coding style for the linux kernel. Coding style is very personal, and I won't _force_ my views on anybody, but this is what goes for anything that I have to be able to mainta

[CPP] Coding Style

C++ Coding Style C++很多强大的语言特性导致它的复杂,其复杂性会使得代码更容易出现bug.难于阅读和维护. 本Coding Style用来约束C++编程,使得代码在有效使用C++语言特性的同时易于管理[代码的一致性高于一切]. 参考:<Google C++ Style Guide> 分类 标题 规则 备注(示例) 头文件 每个.cc文件都应对应一个.h文件 #define保护 1.  #define PROJECT_PATH_FILE_H_ 防止.h文件被多重包含: 1. P

Google C++ Coding Style:引用参数

Google C++ Coding Style定义 输入参数以值或者const引用形式传入,输出参数使用指针. 所有以引用形式输入参数必须加上const,即const T&的形式. 即如下形式: void Foo(const string &in, string *out); 在如下情况下, 可以使用const T*的形式: * 需要进行指针的判空 (即空指针是合理的). * 需要使用到输入参数的指针或引用形式. 为什么要使用const T&形式? 以值传入是最为安全的形式,因为它

Verilog case coding style

1.一般情况下,综合器将case语句综合成多路选择器,但也可能综合成优先级译码器. 2.case语句中,如果条件列举不完全,将综合出不必要的锁存器. 综合器指令://synopsys parallel_case & //synopsys full_case 使用//synopsys parallel_case可以引导综合器生成多路选择器. 1 always @(cs_state) 2 begin 3 case(cs_state) // synopsys parallel_case 4 2'b00

Python&#39;s Coding Style

一个程序员良好的素养可以从他的代码风格里看出. Python官方的开发者指南,PEP8中,列举了Style Guide for Python Code. 摘录The Python's Tutorial中的依依列出. 1.用4空格缩进,而不是tab键. 2.使每行不超过79个字符,目的是小屏幕用户也能很好的阅读. 3.用空一行的方式去把函数,类,和函数中大的代码块分开. 4.如果可能的话,尽量把注释写在一行里. 5.Use docstrings.使用文档字符串.(Ps.这个可得利用好了) 6.在操

python coding style guide 的高速落地实践

python coding style guide 的高速落地实践 机器和人各有所长,如coding style检查这样的可自己主动化的工作理应交给机器去完毕,故发此文帮助你在几分钟内实现coding style的自己主动检查. 1.有哪些著名的Python Coding Style Guide PEP8 https://www.python.org/dev/peps/pep-0008/ 发明Python语言丰碑人物Guido van Rossum的亲自写的Coding Style, 知名度5颗

【九章算法免费公开课】从 strStr 谈面试技巧与 Coding Style

刷题到底刷到什么程度才够? 题目不会直接说不会么? 为什么题目都做出来还是老是挂? 觉得面试官总在为难你? 从来就搞不懂动态规划是什么? 如何正确的骑驴找马? 什么是正确的Coding Style? 面试中该与面试官如何沟通? 本周日,九章算法<算法班>金牌讲师-段誉 为您倾情奉献,同时提供实时在线问答环节,解答您最想知道的面试"内幕"! 讲座时间: 北京时间6月14日 09:30-11:30 (周日) 美西时间6月13日 18:30-20:30(周六) 报名链接: htt