DZone
Agile Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Agile Zone > Two Words About Clean Code

Two Words About Clean Code

Happy weekend! Take a quick break in between doing chores and learn an easy way to write cleaner code in just a few minutes.

Kamil Gołąbek user avatar by
Kamil Gołąbek
·
Apr. 22, 17 · Agile Zone · Tutorial
Like (12)
Save
Tweet
6.37K Views

Join the DZone community and get the full member experience.

Join For Free

There are many things to say about clean code and it might not be something new or be revealing to you, but I want to share a very helpful approach, which can help you to write nice looking code.

Background

Let's imagine that we have a piece of software that allows us to create documents with some indexes and file attachments. Our task is to listen to an event, like DOCUMENT_CREATED or something like that, and then carry out the following actions: 

- Validate the document and move it to the Error folder if needed.

- Send an email to the administrator.

- Move the document to another folder based on its category index.

That's all, and enough to screw up the code.

A Reasonable Approach

1. Do not write any implementations first.

2. Instead, write out all your logic using only method names (i.e. descriptions).

public void onDocumentCreatedListener(Document doc){
  if(isValid(doc)){
    sendEmailNotificationToAdmin(doc);
    moveToCorrectFolder(doc);
  }else{
   moveToError(doc); 
  }

}

We don't even have logic methods in the above code, but our IDE can create ones instead of us (ex: Eclipse).

The method above is the same as describing the each step in this process, as it allows us to think about each step.

3. Write implementations.

4. Fun!

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 12 Modern CSS Techniques For Older CSS Problems
  • Spring, IoC Containers, and Static Code: Design Principles
  • Low Code and No Code: The Security Challenge
  • No-Code/Low-Code Use Cases in the Enterprise

Comments

Agile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo