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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Open Source: A Pathway To Personal and Professional Growth
  • Enhancing Software Quality with Checkstyle and PMD: A Practical Guide
  • Linting Excellence: How Black, isort, and Ruff Elevate Python Code Quality
  • Mastering GitHub Copilot: Top 25 Metrics Redefining Developer Productivity

Trending

  • Unit Testing Large Codebases: Principles, Practices, and C++ Examples
  • Building Reliable LLM-Powered Microservices With Kubernetes on AWS
  • A Deep Dive Into Firmware Over the Air for IoT Devices
  • Enforcing Architecture With ArchUnit in Java

Keep Your Code Consistent

Tabs vs. spaces? Braces or no braces? How should you name your tests? Who cares? Just pick one with your team and keep your code clean and consistent.

By 
Grzegorz Ziemoński user avatar
Grzegorz Ziemoński
·
Feb. 02, 17 · Opinion
Likes (10)
Comment
Save
Tweet
Share
15.2K Views

Join the DZone community and get the full member experience.

Join For Free

There's a bunch of things in programming that have seemingly little to no meaning. Some of those things, despite their "seemingly" little meaning cause a lot of pointless discussions e.g. tabs vs. spaces (as long as you use 4 spaces) or braces vs. no braces (as long as you use braces). I'd say that it doesn't matter which one you choose, but there is one important thing to remember. Once you've made a decision, you have to be consistent!

Why Does it Matter?

When I first thought about writing this article, I thought I'd put up an argument like: Look. You spend much more time reading code than writing it. So when talking about consistency, we're mostly concerned with reading. Imagine you're reading a book that has different fonts on each page, uses different spacing and punctuation or even the writing style changes every other paragraph. Would it read nicely? Now, such an argument would be lame, as the analogy doesn't necessarily have to hold, but it looks I made it anyway ;).

Having weakened you with a shot from my lame cannon, I can now try to convince you to keep consistent using some other arguments:

  • Merging changes is easier: Anyone who has tried resolving an SCM conflict with a guy who uses a different formatter is smiling right about now. Some consistency issues make you waste even more time in the pleasant process of merging changes.
  • Code is more predictable: If you see a construct in the code, there's a higher chance that you know what it is, why it is there, where to look for more, and so on because there might be similar ones in the codebase already.
  • Some errors might be easier to spot or avoid: The simplest example being either always using braces or always making the body of conditionals and loops one-liners.
  • Weird people like me are happier: Some of us just need things to be in perfect symmetry, well-ordered, etc. Otherwise, our brain raises an alert and we get less effective.

What to Keep Consistent?

Whitespaces

Whitespaces help you organize your code nicely. They let you keep things close that should stay close and separate things that deserve to be separated. Conventions regarding whitespaces can save you a ton of time when merging, so it's good to start with this one.

When it comes to examples, as already mentioned, one of the choices would be tabs vs. spaces. If spaces, then how many spaces? How many tabs/spaces when breaking a line? Where do you put new lines and how many? Should you put them between fields? Annotated fields? Methods? Before and after loops? Parts of your tests? I could go on and on, but you most likely get the point already.

Member Ordering

Ordering is another very important point when merging as SCMs get lost when you shuffle things in a source file. It also affects predictability — because you want to know whether you should look up or down the source file for the thing you want to find.

For this point, there are two particularly important choices:

  1. What order do different language constructs go in a class? Most likely, you will choose something like: constants, fields, constructors, methods, inner classes.
  2. What order should methods go in a class? Here I'd expect something like the step-down rule.

Braces

Braces are not a hot topic in Java when it comes to their placement in a line, but they surely are when it comes to one-line conditionals and loops. Some argue that always using braces makes you safer because you will never forget to add them to an existing statement when extending it. Others argue that those statements should contain one line of code anyway and thus braces are not necessary. In other languages, the placement of braces in a line is also discussed, because some claim that one>

Having said all of the above, decide with your team: Should you use braces with conditionals and loops? And, if necessary: Where do you put braces in a line?

Naming

Naming is very important in software development. I guess nobody questions that. While, in general, we strive for "good names," there are some things that cannot be evaluated as better or worse. We just have to agree on them.

The first and very important example that comes to my mind is: How should you name your tests? Depending on language and framework used, there might be a lot of different options. In general, you want to specify the functionality being tested, the assumed system or object state, and the expected behavior. Another thing to decide on would be when to capitalize a letter in a name? It's common to see a project where some abbreviations are moved into names as is, while others have only the first letter capitalized. And maybe one more inconsistency that I recently came across: Where to put prefixes like "Test" in a name?

Package Structure

Different modules/components of your application might have similar building blocks at the lower level, e.g. entities, context configurations, repositories, etc. Keeping their internal package structure consistent across the application might make the project more browsable and thus more comprehensible, at the cost of having some packages with just one or two classes inside.

Solutions to Similar Problems

This point is less about conventions and more about coding sense. In general, we want to use similar solutions to similar problems, especially inside a single source file. For example, if somebody is using a Stream for filtering a collection, you don't write a for loop with an if statement inside a few lines below unless you have a good reason to do so. If the whole project uses a library class for constructing URLs, you don't do String escapes and concatenation just because you know how. If the project uses JUnit's assertions for unit tests, you don't add AssertJ to the projects just because you prefer it. These are just a few examples that annoyed me in the past, but you get the point.

Your Turn!

The list above is surely incomplete — these are just the things that came across my mind at the time of writing this article. If you have any ideas for other things to keep consistent, please put them in the comments so that we can all benefit!

code style

Published at DZone with permission of Grzegorz Ziemoński, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Open Source: A Pathway To Personal and Professional Growth
  • Enhancing Software Quality with Checkstyle and PMD: A Practical Guide
  • Linting Excellence: How Black, isort, and Ruff Elevate Python Code Quality
  • Mastering GitHub Copilot: Top 25 Metrics Redefining Developer Productivity

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!