In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what are the 7 outdated coding styles". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the 7 outdated coding styles?"
1. Use m or this to represent member variables
Rule: to distinguish between member variables and local variables, use any of the following methods
Use the Hungarian notation, mMemberVariable and localVariable. Where m represents a member variable.
The use of this, namely this.memberVariable and localVariable.
The reason for being out of date
The reason is that when we read the code, we can easily know whether they are member variables or local variables without looking at their declarations.
Class MyClass {var mMember = "member" fun doSomething () {val local = "local" println (this.mMember) println (local)}}
Now
If it is a modern IDE, this text-based distinction is no longer needed, see the same code below, which automatically paints them with different colors.
two。 Always explicitly declare public,protected or private
Rule: all variables and functions in a class must be explicitly declared as public,private or protected. Do not use the default state.
You need to specify the type, such as String or Int
You need to specify whether it is private or public.
Public class MyClass {public val publicVariable: String = "100" private fun privateFunction () {} public fun publicFunction () {}}
The reason for being out of date
This is to prevent someone from mistakenly accessing these functions or variables, that is, if the function is not declared, the user may not know the default state (if it is public or private).
Now
In modern IDE, we do not need to explicitly declare the default value, for example, Kotlin is public. Users will not inadvertently mistake it for the default state, because autocomplete only displays the public method. Therefore, it is unlikely that anyone will confuse the default state.
If there is any incorrect usage (such as accessing a private function), it will not go wrong at compile time. It makes an immediate error and gives a clear message.
3. Always declare variable types explicitly
Rule: all variables should be declared with their types, even if their values are clear, for example, you need to specify the type, such as String or Int.
Public class MyClass {public val publicVariable: String = "100" private fun privateFunction () {} public fun publicFunction () {}}
The reason for being out of date
This is to prevent someone from mistakenly accessing these functions or variables, such as assigning variables to the wrong type and causing compilation errors.
Now
If you use a modern programming language, there is no need to explicitly declare the type of a variable if it is inferred and explicit. This is called type reasoning, and it is available in many modern languages today.
If there is any wrong allocation, etc., it will not make an error at compile time. It will go wrong immediately and have a clear message.
4. Member variables should always be private
Rule: all member variables should be private, accessed through getter and setter, and apply to member variables that need to be set or obtained from the outside.
Public class MyClass {private var member = "member"; public fun getMember (): String {return member;} public fun setMember (value: String) {member = value;}}
The reason for being out of date
If we expose it for setting and getting, we need to do something in setting or getting, and we need to change all the code that accesses it.
Therefore, if we restrict the use of getter and setter, we can control it.
Class MyClass {private var member = "member"; fun getMember (): String {println ("Setting member") return member;} fun setMember (value: String) {println ("Setting member with $value") member = value;}
Now
In modern languages such as Kotlin, we can easily insert variables getter or setter into variables when needed without explicitly setting and getting two different functions.
Therefore, we can code in the following way without adding additional setter and getter functions to the class.
Class MyClass {var member = "member"}
When we need to operate on setter or getter, we can easily add them without changing the code that accesses the member.
Class MyClass {var member = "member" get (): String {println ("Setting member") return field} set (value: String) {println ("Setting member with $value") field = value}}
5. The opening and closing braces should be aligned.
Rule: all curly braces should be aligned in the same column so that we can find them easily, such as
Class MyClass {private var member: String = "member" fun doSomething (state: Boolean) {val local = "local" println (member) println (local)}}
The reason for being out of date
The reason is that through vertical observation, we can easily find their pairs, thus knowing where the scope of the function is.
Now
With the new IDE, we no longer need to align the opening and closing braces on the same column as long as the code looks neat.
Class MyClass {private var member: String = "member" fun doSomething (state: Boolean) {val local = "local" println (member) println (local)}}
This is because we can easily collapse or expand them, as shown below.
6. All indents use the tab key
Rule: use tab for all indents instead of spaces
The reason for being out of date
This reduces the number of typing required, as shown below, when you use spaces, you need to type multiple times
Now
With IDE, it will automatically indent the appropriate number of spaces for us. Having spaces also ensures that all code looks consistent throughout the user environment.
7. End code statements with semicolons
Rule: you must use a semicolon when you end a code statement.
The reason for being out of date
This is necessary because previous programming languages (including C and C + +, Java, etc.) made the parser recognize that it was over.
Now
With new modern languages such as Kotlin, you no longer need to write long statements (for example, we can name the variable a shorter, indented form).
Last
By changing my belief in the above seven coding styles, I made the following changes to the code:
At this point, I believe you have a deeper understanding of "what are the seven outdated coding styles?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.