Code Smell 181 — Nested Classes

Code Smell 181 — Nested Classes

Nested or Pseudo-Private Classes seem great for hiding implementation details.

TL;DR: Don’t use nested classes

Problems

Solutions

  1. Make the class public
  2. Keep the public class under your own namespace/module.
  3. Use a Facade to the external world to hide it.

Context

Some languages allow us to create private concepts that only live inside a more significant idea.

These classes are harder to test, harder to debug, and reuse.

Sample Code

Wrong

class Address {
String description = "Address: ";

public class City {
String name = "Doha";
}
}
public class Main {
public static void main(String[] args) {
Address homeAddress = new Address();
Address.City homeCity = homeAddress.new City();
System.out.println(homeAddress.description + homeCity.name);
}
}
// The output is "Adress: Doha"
//
// If we change privacy to 'private class City'
//
// We get an error " Address.City has private access in Address"

Right

class Address {
String description = "Address: ";
}

class City {
String name = "Doha";
}
public class Main {
public static void main(String[] args) {
Address homeAddress = new Address();
City homeCity = new City();
System.out.println(homeAddress.description + homeCity.name);
}
}
// The output is "Adress: Doha"
//
// Now we can reuse and test City concept

Detection

[X] Automatic

Since this is a language feature, we can detect it and avoid its usage.

Tags

  • Hierarchies

Conclusion

Many features are bloated with complex features.

We seldom need these new pop culture features.

We need to keep a minimal set of concepts.

More Info

Disclaimer

Code Smells are just my opinion.

Credits

Photo by Dana Ward on Unsplash

Developers are drawn to complexity like moths to a flame, frequently with the same result.

Neal Ford

Software Engineering Great Quotes

This article is part of the CodeSmell Series.

How to Find the Stinky parts of your Code


Code Smell 181 — Nested Classes was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Maximiliano Contieri

Code Smell 181 — Nested Classes

Nested or Pseudo-Private Classes seem great for hiding implementation details.

TL;DR: Don’t use nested classes

Problems

Solutions

  1. Make the class public
  2. Keep the public class under your own namespace/module.
  3. Use a Facade to the external world to hide it.

Context

Some languages allow us to create private concepts that only live inside a more significant idea.

These classes are harder to test, harder to debug, and reuse.

Sample Code

Wrong

class Address {
String description = "Address: ";

public class City {
String name = "Doha";
}
}
public class Main {
public static void main(String[] args) {
Address homeAddress = new Address();
Address.City homeCity = homeAddress.new City();
System.out.println(homeAddress.description + homeCity.name);
}
}
// The output is "Adress: Doha"
//
// If we change privacy to 'private class City'
//
// We get an error " Address.City has private access in Address"

Right

class Address {
String description = "Address: ";
}

class City {
String name = "Doha";
}
public class Main {
public static void main(String[] args) {
Address homeAddress = new Address();
City homeCity = new City();
System.out.println(homeAddress.description + homeCity.name);
}
}
// The output is "Adress: Doha"
//
// Now we can reuse and test City concept

Detection

[X] Automatic

Since this is a language feature, we can detect it and avoid its usage.

Tags

  • Hierarchies

Conclusion

Many features are bloated with complex features.

We seldom need these new pop culture features.

We need to keep a minimal set of concepts.

More Info

Disclaimer

Code Smells are just my opinion.

Credits

Photo by Dana Ward on Unsplash

Developers are drawn to complexity like moths to a flame, frequently with the same result.

Neal Ford

Software Engineering Great Quotes

This article is part of the CodeSmell Series.

How to Find the Stinky parts of your Code


Code Smell 181 — Nested Classes was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2022-11-23T02:31:15+00:00) Code Smell 181 — Nested Classes. Retrieved from https://www.scien.cx/2022/11/23/code-smell-181-nested-classes/

MLA
" » Code Smell 181 — Nested Classes." Maximiliano Contieri | Sciencx - Wednesday November 23, 2022, https://www.scien.cx/2022/11/23/code-smell-181-nested-classes/
HARVARD
Maximiliano Contieri | Sciencx Wednesday November 23, 2022 » Code Smell 181 — Nested Classes., viewed ,<https://www.scien.cx/2022/11/23/code-smell-181-nested-classes/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Code Smell 181 — Nested Classes. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/23/code-smell-181-nested-classes/
CHICAGO
" » Code Smell 181 — Nested Classes." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2022/11/23/code-smell-181-nested-classes/
IEEE
" » Code Smell 181 — Nested Classes." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2022/11/23/code-smell-181-nested-classes/. [Accessed: ]
rf:citation
» Code Smell 181 — Nested Classes | Maximiliano Contieri | Sciencx | https://www.scien.cx/2022/11/23/code-smell-181-nested-classes/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.