+ All Categories
Home > Documents > Some coding issues Statement groups, semi-colons and ifs.

Some coding issues Statement groups, semi-colons and ifs.

Date post: 19-Dec-2015
Category:
View: 217 times
Download: 0 times
Share this document with a friend
24
Some coding issues Statement groups, semi-colons and ifs
Transcript
Page 1: Some coding issues Statement groups, semi-colons and ifs.

Some coding issues

Statement groups, semi-colons and ifs

Page 2: Some coding issues Statement groups, semi-colons and ifs.

If you can keep your head when all about you are losing theirs, it's just possible you haven't grasped the situation.

Page 3: Some coding issues Statement groups, semi-colons and ifs.

Some coding issues ...

Be careful with Be careful with {{statement grouping bracketsstatement grouping brackets}} and and semi-colonssemi-colons;;

Every Every statementstatement and and declarationdeclaration ends with a ends with a semi-colonsemi-colon;;

;;

In Java, In Java, writing nothingwriting nothing also counts as a also counts as a statementstatement;;

This lineThis line;; is a sequence is a sequence;;;;;; of six of six;; ““statements”statements”;;

1 2 3 4 5 6

Page 4: Some coding issues Statement groups, semi-colons and ifs.

An An if-constructif-construct starts with an starts with an ifif, then a , then a ((condition in round bracketscondition in round brackets)), then a statement, then a statement; ; or or {{group of statementsgroup of statements}}

An An if-constructif-construct starts with an starts with an ifif, then a , then a ((condition in round bracketscondition in round brackets)), then a statement, then a statement; ; or or {{group of statementsgroup of statements}}

Optionally, an Optionally, an if-constructif-construct may continue with an may continue with an elseelse, followed by a statement, followed by a statement; ; or or {{group of group of statementsstatements}}

Optionally, an Optionally, an if-constructif-construct may continue with an may continue with an elseelse, followed by a statement, followed by a statement; ; or or {{group of group of statementsstatements}}

In Java:

Page 5: Some coding issues Statement groups, semi-colons and ifs.

An An if-constructif-construct starts with an starts with an ifif, then a , then a ((condition in round bracketscondition in round brackets)), then a , then a {{group of group of statementsstatements}}

An An if-constructif-construct starts with an starts with an ifif, then a , then a ((condition in round bracketscondition in round brackets)), then a , then a {{group of group of statementsstatements}}

Optionally, an Optionally, an if-constructif-construct may continue with an may continue with an elseelse, followed by a , followed by a {{group of statementsgroup of statements}}Optionally, an Optionally, an if-constructif-construct may continue with an may continue with an elseelse, followed by a , followed by a {{group of statementsgroup of statements}}

However, we recommend always to use a {{group of statements}} for the statement parts … i.e.

… even if the {{group of statements}} contains only a single statement;;

Page 6: Some coding issues Statement groups, semi-colons and ifs.

/**/** * Print out notebook info (number of entries). * Print out notebook info (number of entries). */ */public void showStatus()public void showStatus(){{ if(notes.size() == 0); { if(notes.size() == 0); { System.out.println("Notebook is empty"); System.out.println("Notebook is empty"); } } else { else { System.out.print("Notebook holds "); System.out.print("Notebook holds "); System.out.println(notes.size() + " notes"); System.out.println(notes.size() + " notes"); } }}}

What’s wrong here?What’s wrong here?What’s wrong here?What’s wrong here?

Page 7: Some coding issues Statement groups, semi-colons and ifs.

/**/** * Print out notebook info (number of entries). * Print out notebook info (number of entries). */ */public void showStatus()public void showStatus(){{ if(notes.size() == 0); { if(notes.size() == 0); { System.out.println("Notebook is empty"); System.out.println("Notebook is empty"); } } else { else { System.out.print("Notebook holds "); System.out.print("Notebook holds "); System.out.println(notes.size() + " notes"); System.out.println(notes.size() + " notes"); } }}}

This is a complete This is a complete if-constructif-construct with an with an emptyempty statement, statement, ..

Code layout is Code layout is necessarynecessary to aid to aid readabilityreadability by reflecting code by reflecting code structurestructure – unfortunately, the above layout reflects – unfortunately, the above layout reflects only our only our intentionintention … and … and not the actual structurenot the actual structure written! written!

Code layout is Code layout is necessarynecessary to aid to aid readabilityreadability by reflecting code by reflecting code structurestructure – unfortunately, the above layout reflects – unfortunately, the above layout reflects only our only our intentionintention … and … and not the actual structurenot the actual structure written! written!

Page 8: Some coding issues Statement groups, semi-colons and ifs.

This is a complete This is a complete if-constructif-construct with an with an emptyempty statement, statement, ..

If the condition is If the condition is truetrue (i.e. the notebook is empty), an (i.e. the notebook is empty), an emptyempty statement is executed (which does nothing). So, nothing is statement is executed (which does nothing). So, nothing is done, regardless of whether the condition is done, regardless of whether the condition is truetrue or not! or not!

If the condition is If the condition is truetrue (i.e. the notebook is empty), an (i.e. the notebook is empty), an emptyempty statement is executed (which does nothing). So, nothing is statement is executed (which does nothing). So, nothing is done, regardless of whether the condition is done, regardless of whether the condition is truetrue or not! or not!

/**/** * Print out notebook info (number of entries). * Print out notebook info (number of entries). */ */public void showStatus()public void showStatus(){{ if(notes.size() == 0); { if(notes.size() == 0); { System.out.println("Notebook is empty"); System.out.println("Notebook is empty"); } } else { else { System.out.print("Notebook holds "); System.out.print("Notebook holds "); System.out.println(notes.size() + " notes"); System.out.println(notes.size() + " notes"); } }}}

Page 9: Some coding issues Statement groups, semi-colons and ifs.

/**/** * Print out notebook info (number of entries). * Print out notebook info (number of entries). */ */public void showStatus()public void showStatus(){{ if(notes.size() == 0); { if(notes.size() == 0); { System.out.println("Notebook is empty"); System.out.println("Notebook is empty"); } } else { else { System.out.print("Notebook holds "); System.out.print("Notebook holds "); System.out.println(notes.size() + " notes"); System.out.println(notes.size() + " notes"); } }}}

Here is a Here is a statement-groupstatement-group … it has only one statement (which … it has only one statement (which is OK and happens quite a lot). It is executed after that is OK and happens quite a lot). It is executed after that (useless) (useless) if-constructif-construct, regardless of whether the condition was , regardless of whether the condition was truetrue or not! or not!

Here is a Here is a statement-groupstatement-group … it has only one statement (which … it has only one statement (which is OK and happens quite a lot). It is executed after that is OK and happens quite a lot). It is executed after that (useless) (useless) if-constructif-construct, regardless of whether the condition was , regardless of whether the condition was truetrue or not! or not!

Page 10: Some coding issues Statement groups, semi-colons and ifs.

/**/** * Print out notebook info (number of entries). * Print out notebook info (number of entries). */ */public void showStatus()public void showStatus(){{ if(notes.size() == 0); { if(notes.size() == 0); { System.out.println("Notebook is empty"); System.out.println("Notebook is empty"); } } else { else { System.out.print("Notebook holds "); System.out.print("Notebook holds "); System.out.println(notes.size() + " notes"); System.out.println(notes.size() + " notes"); } }}}

Here is an Here is an else-statement-groupelse-statement-group that is part of no that is part of no if-constructif-construct. . This is This is illegalillegal and will not compile! and will not compile!Here is an Here is an else-statement-groupelse-statement-group that is part of no that is part of no if-constructif-construct. . This is This is illegalillegal and will not compile! and will not compile!

Page 11: Some coding issues Statement groups, semi-colons and ifs.

/**/** * Print out notebook info (number of entries). * Print out notebook info (number of entries). */ */public void showStatus()public void showStatus(){{ if(notes.size() == 0); { if(notes.size() == 0); { System.out.println("Notebook is empty"); System.out.println("Notebook is empty"); } } else { else { System.out.print("Notebook holds "); System.out.print("Notebook holds "); System.out.println(notes.size() + " notes"); System.out.println(notes.size() + " notes"); } }}}

This is a complete This is a complete if-constructif-construct with an with an emptyempty statement, statement, ..

Get rid of the semi-colon …Get rid of the semi-colon …Get rid of the semi-colon …Get rid of the semi-colon …

Page 12: Some coding issues Statement groups, semi-colons and ifs.

/**/** * Print out notebook info (number of entries). * Print out notebook info (number of entries). */ */public void showStatus()public void showStatus(){{ if(notes.size() == 0) { if(notes.size() == 0) { System.out.println("Notebook is empty"); System.out.println("Notebook is empty"); } } else { else { System.out.print("Notebook holds "); System.out.print("Notebook holds "); System.out.println(notes.size() + " notes"); System.out.println(notes.size() + " notes"); } }}}

We now have a We now have a legallegal if-constructif-construct. The first . The first statement-groupstatement-group is is executed if the condition is executed if the condition is truetrue. The second . The second statement-groupstatement-group is executed if the condition is is executed if the condition is falsefalse. .

We now have a We now have a legallegal if-constructif-construct. The first . The first statement-groupstatement-group is is executed if the condition is executed if the condition is truetrue. The second . The second statement-groupstatement-group is executed if the condition is is executed if the condition is falsefalse. .

Page 13: Some coding issues Statement groups, semi-colons and ifs.

/**/** * Print out notebook info (number of entries). * Print out notebook info (number of entries). */ */public void showStatus()public void showStatus(){{ if(notes.size() = 0) { if(notes.size() = 0) { System.out.println("Notebook is empty"); System.out.println("Notebook is empty"); } } else { else { System.out.print("Notebook holds "); System.out.print("Notebook holds "); System.out.println(notes.size() + " notes"); System.out.println(notes.size() + " notes"); } }}}

This is an This is an (illegal)(illegal) assignmentassignment statement, statement, ..

Be careful not to confuse Be careful not to confuse ==== (which is the (which is the test-for-equalitytest-for-equality symbol) with symbol) with == (which is the (which is the assignmentassignment symbol). The above symbol). The above code has an code has an assignmentassignment statement where it should have a statement where it should have a conditioncondition. This is . This is illegalillegal..

Be careful not to confuse Be careful not to confuse ==== (which is the (which is the test-for-equalitytest-for-equality symbol) with symbol) with == (which is the (which is the assignmentassignment symbol). The above symbol). The above code has an code has an assignmentassignment statement where it should have a statement where it should have a conditioncondition. This is . This is illegalillegal..

Page 14: Some coding issues Statement groups, semi-colons and ifs.

/**/** * Print out notebook info (number of entries). * Print out notebook info (number of entries). */ */public void showStatus()public void showStatus(){{ if(notes.size() == 0) { if(notes.size() == 0) { System.out.println("Notebook is empty"); System.out.println("Notebook is empty"); } } else { else { System.out.print("Notebook holds "); System.out.print("Notebook holds "); System.out.println(notes.size() + " notes"); System.out.println(notes.size() + " notes"); } }}}

Back to our Back to our legallegal if-constructif-construct. There is another way to . There is another way to express the same logic …express the same logic …Back to our Back to our legallegal if-constructif-construct. There is another way to . There is another way to express the same logic …express the same logic …

Page 15: Some coding issues Statement groups, semi-colons and ifs.

/**/** * Print out notebook info (number of entries). * Print out notebook info (number of entries). */ */public void showStatus()public void showStatus(){{ if(notes.size() == 0) { if(notes.size() == 0) { System.out.println("Notebook is empty"); System.out.println("Notebook is empty"); return; return; } } System.out.print("Notebook holds "); System.out.print("Notebook holds "); System.out.println(notes.size() + " notes"); System.out.println(notes.size() + " notes");}}

This This if-constructif-construct has no has no else-partelse-part and is and is legallegal. If the condition is . If the condition is truetrue, the , the statement-groupstatement-group is executed … and this contains a is executed … and this contains a returnreturn statement (which immediately exits the method). If the statement (which immediately exits the method). If the condition is condition is falsefalse, the , the if-constructif-construct is finished and the statements is finished and the statements following it are executed.following it are executed.

This This if-constructif-construct has no has no else-partelse-part and is and is legallegal. If the condition is . If the condition is truetrue, the , the statement-groupstatement-group is executed … and this contains a is executed … and this contains a returnreturn statement (which immediately exits the method). If the statement (which immediately exits the method). If the condition is condition is falsefalse, the , the if-constructif-construct is finished and the statements is finished and the statements following it are executed.following it are executed.

Page 16: Some coding issues Statement groups, semi-colons and ifs.

However, this is a little However, this is a little cleverclever … on the whole, we prefer … on the whole, we prefer expressing the alternatives expressing the alternatives explicitlyexplicitly … …However, this is a little However, this is a little cleverclever … on the whole, we prefer … on the whole, we prefer expressing the alternatives expressing the alternatives explicitlyexplicitly … …

/**/** * Print out notebook info (number of entries). * Print out notebook info (number of entries). */ */public void showStatus()public void showStatus(){{ if(notes.size() == 0) { if(notes.size() == 0) { System.out.println("Notebook is empty"); System.out.println("Notebook is empty"); return; return; } } System.out.print("Notebook holds "); System.out.print("Notebook holds "); System.out.println(notes.size() + " notes"); System.out.println(notes.size() + " notes");}}

Page 17: Some coding issues Statement groups, semi-colons and ifs.

/**/** * Print out notebook info (number of entries). * Print out notebook info (number of entries). */ */public void showStatus()public void showStatus(){{ if(notes.size() == 0) { if(notes.size() == 0) { System.out.println("Notebook is empty"); System.out.println("Notebook is empty"); } } else { else { System.out.print("Notebook holds "); System.out.print("Notebook holds "); System.out.println(notes.size() + " notes"); System.out.println(notes.size() + " notes"); } }}}

… … as we had before. as we had before. … … as we had before. as we had before.

Page 18: Some coding issues Statement groups, semi-colons and ifs.

/**/** * Store a new note in the notebook. If the * Store a new note in the notebook. If the * notebook is full, save it and start a new one. * notebook is full, save it and start a new one. */ */public void addNote(String note)public void addNote(String note){{ if(notes.size() == 100) if(notes.size() == 100) notes.save(); notes.save(); // starting new notebook// starting new notebook notes = new ArrayList<String>(); notes = new ArrayList<String>(); notes.add(note); notes.add(note);}}

What’s wrong here?What’s wrong here?What’s wrong here?What’s wrong here?

Page 19: Some coding issues Statement groups, semi-colons and ifs.

This is a complete This is a complete if-constructif-construct with a with a singlesingle statement, statement, ..

/**/** * Store a new note in the notebook. If the * Store a new note in the notebook. If the * notebook is full, save it and start a new one. * notebook is full, save it and start a new one. */ */public void addNote(String note)public void addNote(String note){{ if(notes.size() == 100) if(notes.size() == 100) notes.save(); notes.save(); // starting new notebook// starting new notebook notes = new ArrayList<String>(); notes = new ArrayList<String>(); notes.add(note); notes.add(note);}}

Oh dear, we forgot the curly brackets … Oh dear, we forgot the curly brackets … … … correct correct layoutlayout for the above logic for the above logic (which we don’t want)(which we don’t want) would be: would be:Oh dear, we forgot the curly brackets … Oh dear, we forgot the curly brackets … … … correct correct layoutlayout for the above logic for the above logic (which we don’t want)(which we don’t want) would be: would be:

Page 20: Some coding issues Statement groups, semi-colons and ifs.

This is a complete This is a complete if-constructif-construct with a with a singlesingle statement, statement, ..

/**/** * Store a new note in the notebook. If the * Store a new note in the notebook. If the * notebook is full, save it and start a new one. * notebook is full, save it and start a new one. */ */public void addNote(String note)public void addNote(String note){{ if(notes.size() == 100) if(notes.size() == 100) notes.save(); notes.save();

// starting new notebook// starting new notebook notes = new ArrayList<String>(); notes = new ArrayList<String>(); notes.add(note); notes.add(note);}}

In the above, if the condition is In the above, if the condition is truetrue, save the notes. Whether , save the notes. Whether the condition is the condition is truetrue or or falsefalse, start a new notebook … , start a new notebook … In the above, if the condition is In the above, if the condition is truetrue, save the notes. Whether , save the notes. Whether the condition is the condition is truetrue or or falsefalse, start a new notebook … , start a new notebook …

Page 21: Some coding issues Statement groups, semi-colons and ifs.

This is a complete This is a complete if-constructif-construct with a with a singlesingle statement, statement, ..

/**/** * Store a new note in the notebook. If the * Store a new note in the notebook. If the * notebook is full, save it and start a new one. * notebook is full, save it and start a new one. */ */public void addNote(String note)public void addNote(String note){{ if(notes.size() == 100) if(notes.size() == 100) notes.save(); notes.save(); // starting new notebook// starting new notebook notes = new ArrayList<String>(); notes = new ArrayList<String>(); notes.add(note); notes.add(note);}}

Let’s try again … this time, we add in the curly brackets so that Let’s try again … this time, we add in the curly brackets so that the logical structure matches the layout:the logical structure matches the layout:Let’s try again … this time, we add in the curly brackets so that Let’s try again … this time, we add in the curly brackets so that the logical structure matches the layout:the logical structure matches the layout:

Page 22: Some coding issues Statement groups, semi-colons and ifs.

This is a complete This is a complete if-constructif-construct with a with a statement-groupstatement-group, , ..

/**/** * Store a new note in the notebook. If the * Store a new note in the notebook. If the * notebook is full, save it and start a new one. * notebook is full, save it and start a new one. */ */public void addNote(String note)public void addNote(String note){{ if(notes.size() == 100) { if(notes.size() == 100) { notes.save(); notes.save(); // starting new notebook// starting new notebook notes = new ArrayList<String>(); notes = new ArrayList<String>(); } } notes.add(note); notes.add(note);}}

Now, the logical structure of the Now, the logical structure of the if-constructif-construct matches its layout. matches its layout.

Now, the logical structure of the Now, the logical structure of the if-constructif-construct matches its layout. matches its layout.

Page 23: Some coding issues Statement groups, semi-colons and ifs.

Always write your code as if the guy who ends up maintaining it will be a violent psychopath who knows where you live …

Be careful with Be careful with {{statement grouping bracketsstatement grouping brackets}} and and semi-colonssemi-colons;;

Always use Always use {{statement-groupsstatement-groups}} for the actions for the actions dependent upon the dependent upon the ((conditioncondition)) in an in an if-constructif-construct. .

Page 24: Some coding issues Statement groups, semi-colons and ifs.

• Debugging

• Using the BlueJ debugger (section 3.12)

Concepts

Only when all else failsOnly when all else fails– i.e. not as a first resort!– i.e. not as a first resort!

First resort:First resort: read your code! Why should it work … read your code! Why should it work … not why doesn’t it work?!! Explain to a friend. not why doesn’t it work?!! Explain to a friend.


Recommended