Top 5 Code Smells Newbies Developers Could Easily Identify & Avoid

网友投稿 535 2022-11-14

Top 5 Code Smells Newbies Developers Could Easily Identify & Avoid

Top 5 Code Smells Newbies Developers Could Easily Identify & Avoid

Posted by ​​Ajitesh Kumar​​​ / In ​​Freshers​​​, ​​Software Quality​​ / February 1, 2014

Representing Code Smells in form of WTF count :-)

For newbie developers, it has always remained a challenge to write code of great quality from day one. One of the primary concerns they have been found to have is following:

What kind of code could be termed as high quality code?

There are several characteristics of good quality code. However, a good starting point would be to write code which could be easily maintainable and usable (readable and understandable). Maintainability and Usability are two very important characteristic of software quality as per ISO 25000 as well. Two important features of a highly maintainable code are testability and reusability. Thus, the three key code quality that newbies could keep in mind are testability, reusability and usability.

Top 5 Code Smells Which are Easier to Identify & Avoid

If only newbies could watch following 5 areas (code smells) and avoid them, they would mostly end up with the code conforming to the requirements of testability, reusability and usability:

Long Classes: It would be good to keep a watch on number of lines of code (LOC) in the class. If it crosses more than 300 or so, you may want to check on the fact if the methods of the class are related and serving just one functionality our doing just one thing. This is in line with the famous SOLID principle namely Single Responsibility Principle. Another area to look at is if individual methods are getting longer enough whose detail is describe in next point related with long method. Having classes serving just one functionality is easier to change as there could be only one set of related reasons for which the class would be needed to change. Thus,these classes becomes greatly maintainable meaning testable and reusable. These classes can also be termed as cohesive classes.Long Methods: If the LOC in a method increases more than 50-60, it is time to watch out and make sure that method is just doing one thing. Most of the times, methods get longer because of following two reasons:

Methods consist of block of codes performing multiple functionality which can also be classified as violation of “single responsibility principle”. In this case, one could consider extracting block of codes performing different functionality into different methods.Methods consist of long conditional statements consisting of if-else statements which can again be extracted into different methods.

Long Conditionals Expressions: At times, methods get longer due to large number of conditional statements in the same method. This can also be explained in terms ofMcCabe Cyclomatic Complexitywhich can be calculated as number of conditional statement + 1. These conditional statements looks like if-else, while, for, switch etc.  If the cyclomatic complexity of a method becomes larger than 10 or so, it is time to watch out and see if these conditional expressions could be extracted into separate method. Long conditional expressionsgreatly reduces code usability that is code readability and understand-ability. More importantly, if you a unit testing freak, more the conditional statements in your method, more it becomesdifficult to achieve a higher code coverage.Long Parameter List: Newbies developers tend to create method with long parameter list that creates issue at later stage when many other methods have called these methods and there arises a need to add another parameter to the existing list. Once you see that parameter list of a method increases further than 4-5 parameters, it is time to examine whether these parameters are related and could be created as a parameter object and passed to methods as objects. This makes it easy to add further parameters to parameter object without the need to change the method signature and the code becomes greatly maintainable.Vague Classes & Method Names: This one is very simple to identify. If you can not get a good name to name your classes and methods, be sure that you may be missing on business requirements which is supposed to be served by the method or class. Alternatively, for other developers, if the method and class names becomes difficult to understand, it is time to think through and come up with the name that could help developers to understand the functionality that is supposed to be served the method or the class.

You may want to check the ​​refactoring patterns​​ that could help you with tips to avoid these smells and write neat code.

​​About​​​​Latest Posts​​

​​Ajitesh Kumar​​

Ajitesh is passionate about various different technologies including programming languages such as Java/JEE, Javascript, PHP, .NET, C/C++, mobile programming languages etc and, computing fundamentals such as application security, cloud computing, API, mobile apps, google glass, big data etc. Follow him on ​​​Twitter​​​ and ​​Google+​​.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:idea2020.3 安装插件JetBrains 插件市场安装 Cloud Toolkit
下一篇:Annan warns against use of more force in Syria
相关文章

 发表评论

暂时没有评论,来抢沙发吧~