Beautiful Code

Writing for readability

Use Left and Right arrow keys to move through the sides

Ryan Stille

About Ryan Stille

Readable Code - Why do we care?

  1. Code Complexity Costs Us Time ($$$)

  2. The most expensive part of development is often the programmer

    Servers, disk space, bandwidth, etc. are cheap

  3. Sanity

    Job satisfaction is more likely when you aren't wasting time trying to understand code

We'll cover

  1. Variable & Function Names

    What a function does should be clear based on it's name

  2. Helper Methods

    Getting rid of repetitive code, abstracting details

  3. Formatting

    Lining up code for readability

  4. Conditionals and Flow

    Easier to understand

  5. Comments

    Helpful comments

  6. A few more thoughts

Variable & Function Names

  1. Be descriptive

    i and j are not descriptive

  2. Be short

    Choose a shorter name over a longer one if it conveys as much meaning as the longer one

  3. Be consistent

    If customer has a property called firstName, user should not have a property called fName

Variable names - Be descriptive

Ambiguous Better
text
length
authenticated
date
size
limit
searchTerm
runwayLengthFeet
isAuthenticated
createDate
fileSizeBytes
maxDownloadsPerDay

Variable names - Be short

Long Better
runwayLengthFeet
number_of_search_engines
transaction_typeID_sapWork_create_material
runwayFeet
numEngines
???

Descriptive names

Descriptive names - Better

Variable Names

Variable Names

Variable Names

Variable Names

Helper Functions

  1. Helper Function

    A function whose purpose is to provide a service to one or more other functions nearby. Helper functions are often implemented as local functions. - Programming in Scala, Martin Odersky.

  2. Focus on what's important

    Move logical chunks of code to separate methods

  3. Code Reuse

    Move common code into reusable function

Move unimportant code to separate functions

Move unimportant code to separate functions

Don't forget to mark helper functions as private

reuse code

Reuse code

Line up code

Line up code

Line up code

Line up code

Line up code

Conditionals and Flow - order of comparison

Conditionals and Flow - if/else order

Conditionals and Flow - if/else order

  1. Prefer handling the positive option first

  2. Prefer handling the simpler option first

Conditionals and Flow - if/else order

Conditionals and Flow - Returning Early

Conditionals and Flow - Returning Early

Conditionals and Flow - Returning Early

Conditionals and Flow - Returning Early

Comments

  1. Self documenting Code

  2. Don't comment the obvious

  3. What to comment

    Comment why you did something. Comment what the next programmer will need to know to understand your code.

  4. Don't check-in code that is commented

Comments - poor examples

Comments - better examples

Comments - business logic

A Few More Thoughts

  1. Do one Thing

    Break your code into pieces that do one thing and do it well

  2. Writing Code That Isn't Needed

    Wait to write code until you actually need it. Remove code that isn't being used.

Takeaways

  1. We are communicating in code

    Write for a human, not the computer

  2. Place a Priority on Simplicity

    It will take some extra effort in the beginning put will pay off later

Resources

The Art of Readable Code
Dustin Boswell &
Trevor Foucher
Code Simplicity
Max Kanat-Alexander
The Developer's Code
Ka Wai Cheung

The end

/

#