About this guide
This guide is an (informal) style guide for CSE 142 and 143 and contains a list of rules we expect you to follow when writing code.
If you are taking CSE 143 and have never taken CSE 142, we strongly suggest you read through the rules in the CSE 142 section. We will not review rules introduced in 142 in class and will assume you have already internalized them fully.
Disclaimer: Please note this guide may not be fully complete. We've done our best to be as comprehensive as possible, but it's always possible we may have missed a rule somewhere.
Note: If you are having difficulty locating a particular rule, you may want to try organizing the table of contents by category, rather then by homework assignments.
Table of Contents
CSE 142
- HW 1 - Song
- HW 2 - ASCII Art/Space Needle/Rocket Ship
- HW 3 - Doodle/Illusion/Cafe Wall
- HW 4 - Gradanator
- HW 5 - Guessing Game
- HW 6 - Mad Libs/Baby Names
-
HW 7 - DNA/Personality Test
- HW 8 - Critters
CSE 143
- HW 1 - LetterInventory
- HW 2 - Guitar Hero/HTML Manager
- HW 3 - AssassinManager
- HW 4 - Evil Hangman
- HW 5 - Grammar Solver
-
HW 6 - Anagrams
- HW 7 - 20 Questions
-
HW 8 - Huffman
- Chapter 1
- Chapter 2
- Chapter 3
- Chapter 4
- Chapter 5
- Chapter 6
- Chapter 7
- Chapter 8
- Chapter 9
- Chapter 11
- Chapter 12
- Chapter 14
- Chapter 15
- Chapter 16
- Chapter 17
- 14x specific
- Code organization
- Comments (142)
- Comments (143)
- Comments (inline)
- Control flow
- Data structures
- Data structurs
- Formatting and conventions
- Input/output
- Objects
- Other
- Recursion
- Variables
Why care about style?
We care about writing clean code and following conventions because it makes collaborating easier, reduces the chances of bugs, and because it prepares you for industry, where you are expected to produce clean code.
"Programs must be written for people to read, and only incidentally for machines to execute"
– Hal Abelson
This is a somewhat long section, so here is a short-ish summary of why style matters:
- Programming is a collaborative activity, so it is important to know how to write code easily understandable by other humans.
- Many programmers will distrust and judge you if you routinely churn out unreadable and ugly code.
- Writing clean code reduces the chances that our code is buggy. This is especially important because software is an important part of our lives and is used to help keep people healthy, safeguard private information, support infrastructure... It's important we get this right.
- Pragmatically, many people in industry and in open source projects will conduct code reviews on your code and will and block changes that do not match their internal standards. The way we grade homework mimics this industry best practice.
Many people believe that when writing code, the only thing that matters is if your code is working or not. This is false – while it's important to get your code working, it is just as important to make sure your code is understandable to other people.
Programming is a highly collaborative activity – it's fairly common to have to work together with other people, sometimes even hundreds of them at a time! As a result, it's very important to write code that is easy to work with to allow others to integrate seamlessly with your work. Another benefit is that since clean code is easier to understand, you'll work much faster and introduce fewer bugs.
While you should not collaborate on homework assignments in CSE 142 and 143, it's still important to practice programming with good style – it's the core difference between whether or not a program is pleasant or impossible to work with.
For example, take the following piece of poorly written code:
public void f(int n ){ for (int i=1;i<=n;i++){for(int j=i;j>0;--j)System.out.print("*") ;System.out.print("\n" ) ; } }
Can you tell what this code does? Not easily. Contrast that to the following code, which does the exact same thing, but is written to be much more readable:
public void printStaircase(int numSteps) { for (int i = 1; i <= numSteps; i++) { for (int j = 0; j <= i; j++) { System.out.print("*"); } System.out.println(); } }
This is much more readable. Even if we don't understand a single line of code, we can infer from the very first line that this chunk of code will likely print a staircase, and roughly how the code works from the variable names and indentation.
This is obviously an extreme example, but it does demonstrate in a nutshell the importance of writing clean code.
Somewhat more broadly, focusing on writing clean code forces you to acquire an eye for detail. Programming, and writing correct programs requires careful attention, so it's better to acquire that habit sooner, rather then later.
After all, code is now a part of everyday life – we use code to control cars, control medical equipment, moderate our infrastructure, perform legal analysis, hold private data, and so forth. Many of you will go on to help create the software that ends up becoming an integral part of our life.
If that's the case, we have a responsibility to write as clean and well-designed code as possible to minimize the chances of bugs and accidents that might sneak in due to carelessness. As a result, the sooner you can get into the habit of caring about detail and correctness, the better.
And finally, purely from a pragmatic standpoint, many companies and open source projects will conduct code reviews on any changes you want to make to their codebase.
Some companies are less stringent about code reviews and will rubber-stamp whatever code you give them, and others are more stringent and will block changes if you have errors as small as a misplaced comma in a comment.
Either way, we want to prepare you to be successful in whatever environment you end up finding yourself in, and we do that by having our grading mirror the kind of code reviews you'll encounter in a professional setting.
Understanding style
Good style is not about memorizing a list of rules. Instead, focus on understanding the core principles of clean code: focus on writing code that is readable, concise, modular, and efficient. If you understand these principles, you can independently predict and derive nearly every rule in this guide.
If you approach style as a list of somewhat arbitrary rules to memorize, you will find it difficult to internalize what is and is not good style.
Instead, try and understand that all style guides, whether you follow ours or another organization's, are created primarily to help coders write more readable and less buggy code. If a rule exists, it's because without it, it's very likely your code will be messier and buggier.
More specifically, you'll find that all style rules ultimately stem from four fundamental principles regarding style: we want to write code that is readable, concise, modular, and efficient:
-
Readable and consistent:
Write code that is easily read and understood. This will make it easier for others to modify, extend, and use your code.
Whenever you must make an arbitrary choice, consistently make the same choice to minimize confusion.
-
Concise and lacks redundancy:
Write code that contains no duplicate or copy-pasted code. This way, if someone needs to change something, you only need to change a single piece of your code, not every copied-and-pasted chunk.
-
Well-organized and modular:
Write code that is easily combined and reusable. Changing a single portion of your code should not cascade out and force you to rethink a large chunk of your codebase. Your code is modular and composes well, like legos or building blocks.
-
Efficient:
You write code that does not do extra work and does not consume unnecessary memory. The faster your code runs and the less resources it consumes, the better.
As a note, we find that most students will initially have a poor understanding of what does and does not constitute "efficient code". Until we formally introduce the concept of efficiency and Big-O analysis in 143, I would not hugely worry about efficiency on your homework assignments. Doing what seems to be reasonable is likely to be the best approach.
We will elaborate on why exactly these four sections are important throughout this style guide, but for now, keep them in the back of your head. When you're not sure whether or not something is good style or not, just ask yourself: "how does doing X impact readability, concision, modularity, and/or efficiency?"
All this said, you will run into the occasional rule
that seems to be largely arbitrary. For example, why do we name
our variables using camelCase
instead of using
snake_case
, like some other programming languages
do?
This is a fair question, and in cases like these, the reasoning behind rules largely comes down to historical happenstance. We've tried to pick rules that are followed by the majority of the Java community, but there are always going to be slight differences from organization to organization.
For example, compare our style guide to Google's style guide vs Twitter's style guide . There are a lot of similarities, but the guides are not identical.
And of course, if we switch to a different programming language, the style guidelines are going to be even more different – for example, see the Python community style guide .
In the end, what's important is that you develop the discipline needed to be able to consistently conform to the standard of whatever convention you are required to follow, whatever they might be.
Changelog
- November 28, 2016
- Edit Trees: Modifying trees to demonstrate using the 3-argument TreeNode constructor
- Add Trees: Modifying trees
- Add Trees: Looking ahead in trees
- Add Trees: Designing custom tree nodes
- October 24, 2016
- Add a searchbar
- Move section on sets and maps to HW 4
- Remove section about SortedSets and SortedMaps
- Add Sets and maps: Sets and random access
- Add Manipulating data structures: Using copy constructors
- Add Manipulating data structures: Manipulating strings
- Add Manipulating data structures: Do not leak internal state
- Add ListNode manipulation: Final fields and variables
- Tweak some wording in the Advanced loops section.
- October 17, 2016
- Add ListNode manipulation: Manipulating and creating ListNodes
- Add Writing elegant code: Simplify your code
- Add Writing elegant code: Re-implementing convenience methods
- Add Stacks and queues: Do not destroy client data structures
- Fix Collections and objects: Avoid creating unnecessary objects so the angle brackets are pointing in the right directions.
- Fix Commenting public vs private methods: Comment your exceptions so the second example matches the surrounding text and is labeled a good example.
- October 4, 2016
- Clarify that we don't care how exactly people comment methods in 143 as long as preconditions and postconditions are documented somewhere
- Explain that methods will sometimes have no preconditions, and how to comment such methods.
- Moved table of contents closer to the top.