Programmer Life

The 7 Coding Styles That Are Dated

I was a firm believer in them but now I am converted.

Elye
Elye
Aug 18, 2020 · 6 min read
Image for post
Image for post
Picture by geralt on Pixabay

1. Use m or this to indicate member variables

Past reason

class MyClass {
var mMember = "member"

fun doSomething() {
val local = "local"

println(this.mMember)
println(local)
}
}

Now

Image for post
Image for post
Different coloring of variables

2. Explicit declare public, protected or private always

public class MyClass {
public val publicVariable: String = "100"
private fun privateFunction() {}
public fun publicFunction() {}
}

Past reason

Now

Image for post
Image for post
The private Function won’t appear on auto-complete

3. Explicit declare a variable type always

public class MyClass {
public val publicVariable: String = "100"
private fun privateFunction() {}
public fun publicFunction() {}
}

Past reason

Now

Image for post
Image for post
10 + MyClass().publicVairable will error out immediately as Int and String can’t be added together.

4. Member variable should always be private

public class MyClass{
private var member = "member";

public fun getMember(): String {
return member;
}

public fun setMember(value: String) {
member = value;
}
}

Past reason

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

class MyClass {
var member = "member"
}
class MyClass {
var member = "member"
get(): String {
println("Setting member")
return field
}
set(value: String) {
println("Setting member with $value")
field = value
}
}

5. Start and end curly brackets should be aligned

class MyClass 
{
private var member: String = "member"

fun doSomething(state: Boolean)
{
val local = "local"
println(member)
println(local)
}
}

Past reason

Now

class MyClass {
private var member: String = "member"

fun doSomething(state: Boolean) {
val local = "local"
println(member)
println(local)
}
}
Image for post
Image for post
Easily expand or collapse code scope

6. Use tab for all indentation

Past reason

Image for post
Image for post
The old text editor, one need to type every space

Now

Image for post
Image for post
The modern IDE, till perform auto-indentation of spaces

7. Use of semicolon to end a code statement

Past reason

Now

Image for post
Image for post
Photo by Fotis Fotopoulos on Unsplash
Image for post
Image for post

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface.

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox.

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic.

Get the Medium app