Indentation & Comments

Slides:



Advertisements
Similar presentations
Using Jeroo Dianne Meskauskas
Advertisements

13-Jun-14 OOP features of Jeroo. Overview In this presentation we will discuss these topics: OOP terminology Jeroo syntax constructors methods.
Copyright, Joseph Bergin
Internal Documentation Conventions. Kinds of comments javadoc (“doc”) comments describe the user interface: –What the classes, interfaces, fields and.
11-May-15 Control Structures part 2. Overview Control structures cause the program to repeat a section of code or choose between different sections of.
SIMPLE PROGRAMS Jeroo – Chapter 4 –. Basic Concepts Jeroo (Java/C++/object-oriented) programing style is case-sensative. Be consistent in coding Logic.
Your First Java Program: HelloWorld.java
Direction at your fingertips. A compass rose is a design on a map that shows directions. It shows north, south, east, west, northeast, northwest, southeast,
Movement. Fixed Movement setLocation (x, y) Makes the crab move to a fixed cell x,y Relative Movement The functions: getX() getY() Fetch the x and y coordinate.
Chapter 2: Algorithm Discovery and Design
CHAPTER 1: INTRODUCTION TO COMPUTER SCIENCE Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
5-Oct-15 Introduction and Code. Overview In this presentation we will discuss: What is Jeroo? Where can you get it? The story and syntax of Jeroo How.
BTEC Unit 06 – Lesson 08 Principals of Software Design Mr C Johnston ICT Teacher
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
25-Oct-15 Jeroo Code. Overview In this presentation we will discuss: How to write code in Jeroo How to run a Jeroo program.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
1 karel_part2_Inheritance Extending Robots Tired of writing turnRight every time you start a new karel project. How do we avoid re-writing code all the.
Print a copy of the Bookmark Template.. Select one of the templates and cut it out along the dotted lines. Cut all the way through to the edge on the.
Lab Zone Activity How many books can you lift with your little finger? Activity How many books can you lift with your little finger?
Higher Dimensions. x Let's say we use a pencil to mark a point on paper. x is this point. We pick a direction and move the pencil along this direction.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Cupid Shuffle.  Everyone find a line on the floor and face the stage.  Now put your arms stretched out to your sides, you should not be able to touch.
PYTHON WHILE LOOPS. What you know While something is true, repeat your action(s) Example: While you are not facing a wall, walk forward While you are.
Controlling Program Structures. Big Picture We are learning how to use structures to control the flow of our programs Last week we looked at If statements.
11-Jun-16 Algorithms 2.2. Overview In this presentation we will discuss: What is an algorithm? 5 steps in developing an algorithm A Jeroo example.
Boğaziçi Ünv Koç Ünv Darüşşafaka Lisesi
Computer Programming Your First Java Program: HelloWorld.java.
ICS 3UI - Introduction to Computer Science
Control Structures part 2
Lecture 1 Introduction Richard Gesick.
Principles of a Grid Reference
Jeroo Code 18-Jul-18.
2.2 Algorithms 21-Jul-18.
Introduction to Computational Thinking
A451 Theory – 7 Programming 7A, B - Algorithms.
OOP features of Jeroo 8-Nov-18.
Unit# 9: Computer Program Development
PROGRAMMING What is it?.
The Five Stages of Writing
Why Use Maps???.
Passing Parameters by value
COMPUTER PROGRAMMING PYTHON
Coding Concepts (Basics)
Position and Direction co-ordinates
Unit 6 Assignment 2 Chris Boardley.
Python 21 Mr. Husch.
adapted from Recursive Backtracking by Mike Scott, UT Austin
Programming.
Problem Solving Designing Algorithms.
Introduction and Code 18-Jan-19.
The PlayStation Example
Maintaining a Program In today’s lesson we will look at:
The Five Stages of Writing
Control Structures part 2
Control Structures 12-May-19.
Python While Loops.
WEEK 8 COURSE PROJECT PRESENTATION NAME: ALEXANDER WEISS CLASS: CIS115.
7X Monday The Civil War Begins
Python 12 Mr. Husch.
Creating Maintainable code
2.2 Algorithms 26-Jun-19.
OOP features of Jeroo 3-Jul-19.
Control Structures VB part 2
Question 1. Question 1 What is 10 x 40? A 40 B 400 C 4000 D 4.0.
Directions.
IF 1-Jul-19.
Jeroo Code 7-Sep-19.
Creating readable code
Introduction To Software Development Environment
Presentation transcript:

Indentation & Comments 8-Jul-19

Overview Indentation isn't important to the correctness of Java programs (the computer totally ignores it), but it can make a big difference to the readability of your programs. Note: In other languages, like Python, indentation is necessary

Version without indentation method main(){ Jeroo Sally = new Jeroo(); while(!Sally.isWater(AHEAD)) { Sally.hop();} Sally.turn(RIGHT); while(!Sally.isWater(AHEAD)) { Sally.hop();} Sally.turn(RIGHT); while(!Sally.isWater(AHEAD)) { Sally.hop();} Sally.turn(RIGHT); while(!Sally.isWater(AHEAD)) { Sally.hop();} Sally.turn(RIGHT);} What does it do???

with indentation the meaning is easier to see method main() {     Jeroo Sally = new Jeroo();     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     Sally.turn(RIGHT);     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     Sally.turn(RIGHT);     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     Sally.turn(RIGHT);     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     Sally.turn(RIGHT); } the meaning is easier to see

add in comments method main() {     Jeroo Sally = new Jeroo();     // Go along the top (north) edge of the island     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     Sally.turn(RIGHT);     // Go along the right (east) edge of the island     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     Sally.turn(RIGHT);     // Go along the bottom (south) edge of the island     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     Sally.turn(RIGHT);     // Go along the left (west) edge of the island     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     // Note that this last turn is needed to ensure that Sally is facing east     // again, as required in the problem statement.     Sally.turn(RIGHT); } now, anyone could look at the program and know exactly what it does.

Comments describe what is happening in the program method main() {     Jeroo Sally = new Jeroo();     // this is a loop     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     Sally.turn(RIGHT);     // this is a loop     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     Sally.turn(RIGHT);     // another loop     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     Sally.turn(RIGHT);     // still looping     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     // a turn     Sally.turn(RIGHT); } NOT stating the obvious These are lousy comments!

refining further… of course, there are areas of repeated code… perfect candidates for creating methods how would you rewrite it to be more efficient using methods? can you still create a program as readable as the example given here?

add in comments This could be a method method main() {     Jeroo Sally = new Jeroo();     // Go along the top (north) edge of the island     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     Sally.turn(RIGHT);     // Go along the right (east) edge of the island     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     Sally.turn(RIGHT);     // Go along the bottom (south) edge of the island     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     Sally.turn(RIGHT);     // Go along the left (west) edge of the island     while(!Sally.isWater(AHEAD)) {         Sally.hop();     }     // Note that this last turn is needed to ensure that Sally is facing east     // again, as required in the problem statement.     Sally.turn(RIGHT); } This could be a method Repeated code makes a good method Or would this be a better choice for a method?

The End The important point is to make a reasonable effort to communicate to your human readers as well as to the computer. Comments and indentation are the 2 tools for making a program readable, for others who may need to use your code, and for yourself later on when you may have forgotten the details of your program.