Effective software programming and project management

by Aaron Yang

Why should you program “correctly”?

  • Improved comprehension
  • Edits are easy to do
  • Less technical debt
  • Overall development speed is faster

Clean Code

.... is small, expressive, simple

… does exactly what you expect it to do

… should read like english and flow naturally

Meaningful Names

All names should be descriptive to exactly what information they hold

You should already know 80% of what a function does just by reading the name

Names are indicative of the type of its data

function names should be verb phrases

variables are nouns

variables containing arrays usually are plural nouns

Meaningful Names

public int count(int[] array) {

int x = 0;

for(int y = 0; y < array.length; y++){

if(array[y] >= 18) {

x++;

}

}

return x;

}

Meaningful Names

public int countNumAgesOverThreshold(int[] ages) {

int currCount = 0;

for(int index = 0; index < ages.length; x++){

if(ages[x] >= 18) {

currCount++;

}

}

return currCount;

}

Meaningful Names

Class names should NOT be verbs:

Calculator, WikiPage, Account, Address Parser

Variable Names are nouns:

currCount, robotArm, frontRightWheel

Boolean are adj or adj phrases:

isAlive, isMarked, hasAuth

Method Names are verb and verb phrases:

moveRightWheel(), setupDrivetrain(), calculateVelocity()

No Magic Numbers

Magic numbers are hard-coded numbers that are not 0 or 1.

If you need to change that hard coded number, it will be hard to do so if you use that number in multiple locations throughout your code.

Modify your code such that any hard-coded numbers are declared at the top as constants (all caps) and such that function calls use the constants instead.

How to improve this?

public int countNumAgesOverThreshold(int[] ages) {

int currCount = 0;

for(int index = 0; index < ages.length; index++){

if(ages[index] >= 18) {

currCount++;

}

}

return currCount;

}

Here is one way of making it better

public int countNumAgesOverThreshold(int[] ages, int ageThreshold) {

int currCount = 0;

for(int index = 0; index < ages.length; index++){

if(ages[inex] >= ageThreshold) {

currCount++;

}

}

return currCount;

}

somewhere else:

final int ADULT_AGE;

int[] ages = {10, 15, 21};

int numOfAdults = countNumAgesOverThreshold(ages, ADULT_AGE);

How many lines should your function be?

5 lines

Not really, but actually yes

Often times, functions turn out to be over 5 lines. But at the back of our minds, engineers strive to make functions as short as they can. Usually they are < 20 lines.

Single Responsibility Principle

Functions should do one thing, and do it well.

They should do it only.

Writing better functions - How can we improve this?

public void setupRobot(){

// electrical

setupCamera();

checkWiring();

turnOnT(); … and more lines related to electrical

// drivetrain

setupWheels();

setupMotors();

… and more lines related to drivetrain

}

Abstract those lines to a function!

If your functions are getting long, consider

abstraction to bring down your line count

public void setupRobot(){

setupElectrical();

setupDrivetrain();

}

Formatting

Formatting is something that is supposed to be consistent so it is easy to read.

Companies may have company-wide formatting rules.

Formatting can also be team specific

Formatting

Formatting is something that is supposed to be consistent so it is easy to read.

Companies may have company-wide formatting rules.

Formatting can also be team specific

White space - organize your code and thinking

White space should separate out units of work and help you read the code better.

Essentially, blank lines should serve the same purpose as paragraph separators in english.

Add white space between units of work:

  • Use whitespace to group units of relevant code
  • Add 1 blank line before and after each code block (if statements, for loops, function declarations, etc)
  • Don’t add blank lines to the beginning and end of each code block
  • Don’t add more than one blank line

Live critique of code

Git how-to

What is Scrum?

An industry accepted way of organizing your team and software development process

Scrum process

  • Define requirements
  • Translate requirements into stories (or tasks)
  • Do some of these tasks
  • Review outcomes
  • Repeat the process

Scrum roles

  • Scrum master: champions the processes and leads the meeting
  • Product Owner: person in charge of defining tasks and shaping the development plan
  • Developers: the grunts :P

The product owner could be same person as scrum master

Scrum Processes - Sprint

Sprint planning (start of a sprint):

Define any new requirements and translate them to stories.

Decide which stories are going to be finished for the current sprint

Standup meeting:

Short meeting where people give updates, hold people accountable to their stories.

  • What was done since last meeting, progress towards completion
  • What is blocking me or if I need help
  • Assign new stories to be done if someone finishes

Scrum Processes - Example Sprint

Week 1:

Monday - Sprint planning

Wednesday - standup

Friday - standup

Week 2:

Monday - standup

Wednesday - standup

Friday - standup

Week 3: (and repeat)

Monday - sprint planning

Stories

Stories should be a small unit of work.

A collection of stories will comprise a feature of your project

If a story takes longer than 1 sprint to complete, you need to break it up into smaller chunks.

Story statuses:

  • To do
  • In Progress
  • Waiting/Blocked
  • Ready for review
  • Done

Stories - examples

Bad:

“Build a website”

Good:

“Design the UI”

“Implement HTML/CSS of front page”

“Implement JS of front page

“Backend routing to front page”

“Get website up and running in AWS”

“Get database server up and running”

“Simple login system with user and password”

“Modification of user profiles”

Use github projects to manage your work!

Questions?

Clean Code and How to Program - Google Slides