Clean Code in Java

Writing clean code is important because it allows you to communicate clearly with the next person who uses what you write. Being able to go back to previously written code and understand its purpose is the key, especially in the field of Software Development.

Write clean code and don't go for compromises.

CleanCode.png, Dec 2021

Introduction

  • One programming language per file
  • Name of variables : not ws -> WebSettings
  • TED stands from — TERSE, Expressive, Do one thing
  • DRY principle — Don’t Repeat Yourself
  • Self-documentation code
  • Favour code over comments
  • Format for readability
  • Clear ident
  • Layer of abstractions

Naming

  • Better variables names
  • P → prices
  • Classes
  • Single responsibility to the class
  • Bad: WebsiteBO
  • Good: User, Account
  • Guidleines
  • Noun , not verb
  • Be specific
  • Single responsibility
  • Avoid generic suffixes like MyFunctions
  • Methods
  • Poor names: Get, Process, Start
  • Good: GetRightsUsers, IsValidStatus
  • Rubber Ducking
  • Verbalising — different part of the brain
  • Abbreviations
  • Avoid: RegUsr, RegisterUsr,
  • No standard
  • Put full name: RegisterUser
  • Booleans
  • Dirty: close, clean, status,login
  • Clean: isClose, isClean, loggedIn
  • Symmetry
  • Bad: on/disable, Lock/open

Conditionals

  • Compare boolean Implicitly
  • If(isStatusValid == true) — bad
  • Assign boolean implicitly
  • Boolean isFat= false;
  • If(maxWeight > 180)
  • isFat = true;
  • Positive Conditionls
  • Is(!isFat) — DIRTY
  • Mind should be positive
  • Ternary elegance
  • Int registrationFee = isSpeaker >0:450;
  • YAGNI — you ain’t gonna need it — no extra complexity
  • Stringy Typed
  • If(zooAnimal == “Zebra”) — dirty
  • If(zooAnimal == ZooAnimal.Zebra) — with Enum
  • No typos mistakes
  • Autocomplete help
  • zooAnimal — documented, how many references are
  • Constants, ENUM
  • If (age > 21) — dirty
  • If (age > MAJOR_AGE) — ok
  • If (status == “active”) — dirty
  • Status.ACTIVE — ok
  • Complex Conditions
  • Bool variable ????
  • Enums vs Polymorphism
  • Switch with Enum
  • Instead of Switch create an abstract class, inheritance for branches of switch (3 → 3)
  • Factory in place
  • Be decalrative
  • Functional programming
  • Table Driven Methods
  • If, ifelse, ifelse….
  • Use DB table

Functions

  • When
  • Avoid Duplication
  • Indentation
  • Unclear intent
  • 1task
  • Avoid Duplication
  • Excessive Indentation
  • Extract Method
  • Return Early
  • Return when the conditions is not met
  • Years ago — one return
  • Code Complete, Steve McConnell
  • Fail Fast
  • If (StringUtils.isEmpty(user Nanme) throw an error);
  • Convey Intent
  • Do one thing
  • MayFly Variables
  • Scope of variables to be reduced
  • Initialise just in time
  • Parameters
  • Strive 0–2 parameters
  • Function — do one thing
  • Exceptions
  • Broke immediately
  • Try/catch/log -> not allowed
  • Bubble it

Classes

  • Principle of Proximity

Page top