DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Upgrade Your Code Conventions

Read these six fairly new ideas for code conventions, and see if they help you write cleaner, more manageable code.

Sam Atkinson user avatar by
Sam Atkinson
·
Dec. 17, 15 · Opinion
Like (36)
Save
Tweet
Share
24.46K Views

Join the DZone community and get the full member experience.

Join For Free

There’s a lot of styles and conventions that we follow as Java developers just because that’s how things are done and we don’t take the time to stop and question them. Over time I’ve developed some code styles and patterns that go against the grain. I was outraged about many of them when someone first showed them to me, but after spending some time using them, I realised they result in a much cleaner, manageable codebase and a happier developer.

Don’t just write these ideas off because they’re different. I suggest taking one or two of the below and try them out for a couple of weeks. If you still don’t like them then it won’t take long to back them out.

Zero Comments (Except on Public APIs)

I honestly thought that as an industry we’d already agreed on this one, but having paired with a lot of interviewees I’ve discovered this really isn’t the case. So let’s go into it once more: no comments.  Comments become stale very quickly. If you write a comment above a code block there’s no guarantee that the next person that edits that block will update the comments, and from experience I know they won’t. Code blocks get deleted. Requirements change. Your comment is going to cause more harm than good.

The solution is simply to write self documenting code. Just by naming stuff well it should be patently clear what’s going on. If it isn’t then you need to refactor and break it down into simpler forms. Don’t worry about long method names if it makes what’s happening explicit. We have autocomplete, no one is going to have to type the full name out.

The obvious exception is public APIs. If you’re building a library to hand out, still use clear method names, but Javadoc can be really helpful in this, and only this situation.

Don’t Start Test Methods With the Word “Test”

There’s really no need. Your method is annotated as a test, and the class is in the test package. We know it’s a test. Your test method should specify what the requirement is that it’s testing, for example: “reversesTheWordRandomToModnar()” or “adds70ToBalanceOf100ToMakeBalanceOf170(). This makes it totally clear exactly what is under test and what is expected.

If you’re using IntelliJ there’s a particularly awesome plugin called Enso which will turn your test name into a sentence, and is viewable from the thing you’re testing. This means when you’re looking at any class, Enso will show you what the spec is for it (assuming you’re using TDD, which you are, right?)

Don’t Use @Override

This is something that is quite contentious, but hear me out. If you don’t use @Override, the worst case is that you’re overriding another implementation and the original version will get called instead of your new version. Fortunately, you’re using TDD and you’re testing the amalgamated blocks of your codebase, so this would get caught, which renders the @Override as redundant code. Redundant code is bad and distracting. Stop using @Override, rely on TDD instead.

Don’t Use getX()/setY()

This really winds people up for no apparent reason. the use of getXXX and setXXX is a remnant of JavaBeans, an era long past and no longer needed. It makes code horrible with no added benefit. Drop the get/set in favour of the field name. car.engine() will give you an engine, and car.engine(new v8()) set the engine to something new. This is still really clear and makes for much cleaner code if you end up accessing something over a couple of levels, e.g. car.lights().frontLeft() vs. car.getLights().getFrontLeft(). This is the one that at first I was most against, then came around to, and now I’m extremely passionate about.

Working Code > Performant Code

Ok, so this is less stylistic and more general, but the amount of time I’ve seen people take a problem and overengineer a solution just grates me. Solve the problem at it’s most basic level and check the performance. 99 time out of 100, it’ll be fast/scalable/buzzword enough. Instead I normally see people creating complicated caching solutions that do nothing to improve the performance but make the code a rats nest. Do the most basic thing you can to solve the problem, then optimise. At least this way you can prove there’s a problem and have empirical data to prove it’s fixed.

Use Your Own Exception Type

Again, I thought we’d all agreed as an industry but I was wrong. Checked exceptions are terrible. Almost all other languages (hi C#) realised this and didn’t even include them as a type. In any app I write, I will create my own Exception type. Any Exception thrown in my app will get wrapped in my created one and thrown as a runtime exception. This makes my code much cleaner (I don’t have throws XXXException spreading throughout my application) and it also means that if I see something in the logs I can trace that it has come from within my code (and where) or if it’s some completely unexpected exception.

code style

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java REST API Frameworks
  • 5 Software Developer Competencies: How To Recognize a Good Programmer
  • Use AWS Controllers for Kubernetes To Deploy a Serverless Data Processing Solution With SQS, Lambda, and DynamoDB
  • 5 Best Python Testing Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: