"Hi, Amigo! Today I'm going to tell you about code styles and the importance of code style."

"I'll start with what matters most. Java code should be easy to read. The general approach to the code is this: code is written once but read a hundred times."

"Suppose you and 10 other programmers are writing an application. You work on the application for three years, with intermediate releases every three months."

"That long?"

"This is Java, my young grasshopper! "How about an enterprise system running on a dozen servers and written by 100 people over more than 6 years? That happens too sometimes."


"Anyway, the main rule, the main requirement for code is that it must be easy for other developers to read."

"In other programming languages, people often work as small teams on small tasks, so they may have another main rule, such as 'It works? Excellent'."

"Over the course of a couple of years, all of your team members will make several changes to code that you've written. And each time they'll have to understand how the code works."

"And incomprehensible code that works perfectly is difficult to change. They'll discard it and rewrite it their own way. So, write code that others can understand. If you can improve your code, then improve it. If it can be improved, then it needs to be improved!"

"If you write code for 15 minutes and then spend two hours improving it, you're doing it right. How much time are you saving the team?"

"'2 hours to understand your code' x 'the 100 times when people will need to understand it' = 200 hours."

"I pulled these figures out of thin air, but I want you to understand the problem and its scope. Your code is created to be read by other programmers. Everything else is secondary."

"Is the code not working correctly? We'll fix it. Not optimized? We'll optimize it. Not documented? We'll add comments."

"Is the code hard to read? Throw that crap in the trash and write everything again from scratch!"

"I didn't think it was such a big deal."

"One of the reasons Java is a leading programming language is that all Java code is written to be read by other programmers."

"Now let's move on to the second question: how do you make your code as easy to read as possible?"

"Anyone can understand when someone speaks familiar words in his or her native language. The same is true here. Code is easy to read when a programmer can easily guess:

A) What each method does

B) The purpose of each class

C) Exactly what each variable stores.

All of this is communicated in names: class names, method names, and variable names. Additionally, there is style when it comes to naming variables. And there's code style."

"I'm ready to listen."

"Programming is based on good English! A well-written program reads like ordinary technical documentation."

"Let's start with names."

"A method name should briefly describe what the method does. Then the code can be read like simple prose."

public String downloadPhoto(String url)
 String resultFileName = TempHelper.createTempFileName();

 Downloader downloader = new SingleFileDownloader(new Url(url));

 if (downloader.hasError())
  return null;

 return resultFileName;

"Here's how such a program is read."

Line 1.

"The method is called 'downloadPhoto'. It seems that it downloads a photo file from the Internet. Where does it download to? We don't know yet. From where? The method has a parameter named url — that's probably the URL for the download."

Line 3.

"The variable resultFileName is declared and assigned a value by TempHelper.createTempFileName();"

So this must be the local path to the file where we'll save our downloaded file.

"The name 'TempHelper' doesn't tell us anything. The 'Helper' suffix says that this is a kind of utility class that doesn't contain important business logic, but rather is used to simplify routine tasks that occur frequently."

"The method name 'createTempFileName' indicates that this method creates and returns the name of a temporary file (temp file). A temp file is a temporary file that is created for a while and then usually deleted by the time the program is closed."

Line 5.

"A SingleFileDownloader object is created and assigned to the variable downloader."

This is the object that will download our file from the Internet.

"A SingleFileDownloader object is assigned to the variable downloader. From the name, we may assume that the program has several types of downloader classes. One was written for downloading single files, and we can probably expect to encounter other downloaders in the code for groups of files with names like: MultiFileDownloader, FileGroupDownloader, or DirectoryDownloader"

Line 6.

"We set the downloader object's resultFileName property equal to the value of the variable resultFileName. In other words, we tell the loader where to save the downloaded file. As you would expect. So, we're basically predicting the code!"

Line 7.

"We call the start method. The download starts. That makes sense. I wonder how the download happens: in parts, on a separate thread, or the whole thing right here? If we download the whole thing right here, it could take a long time and have consequences."

Lines 8-11.

"Ah. Here we see the standard loop written by someone waiting for a download to finish. The downloder object has a done property, which is returned by the isDone() method. Because the method is called isDone(), rather than getDone(), we conclude that the done variable is a boolean or perhaps a Boolean."

Lines 13-14.

"If an error occurs during the download, then the downloadPhoto method returns null. It's good that it handles errors. It's bad that it just returns null—it's not clear what the error is. It would be better to throw an exception with information about the error."

Line 16.

"We return the path to the local file that contains the downloaded file."


"This program's code makes it is absolutely clear what it does. You can even make guesses about how the program is organized and what other classes/methods we'll find."

"Now I understand how important names are."

"More about names. You can often guess which methods an object/class has. For example, if an object is a collection, then it will most likely have a size() or count() method to get the number of elements. Also, it will probably have an add() or insert() method. Elements are retrieved from collection classes using get/getItem/getElement methods."

"If a variable is called i, j, or k, then it is most likely a loop counter."

"If a variable is called m or n, then it is most likely the size of an array/collection."

"If a variable is called name, then it is most likely a String containing someone's name."

"If a class is called FileInputStream, then it is simultaneously a file and an input stream."

"The more code you see, the easier it is to read others' code."

"But sometimes there's code that is very difficult to read. In this case, here's a very practical piece of advice:"

Write code as if it will be maintained by a violent psychopath who knows where you live.

"That's funny and not funny at the same time."

"Now a little bit about the styles used to name variables."

"Java developers try to give highly informative names to variables and methods. As a result, names often consist of several words. There are 4 styles for the capitalization of compound names."

1) Lowercase – All words are written with lowercase letters. For example:

'Green house' becomes 'greenhouse'

'Hollywood girl' becomes 'hollywoodgirl'

This style is used for package names.

2) Uppercase – All words are written with uppercase letters and separated by an underscore. For example:

'Max value' becomes MAX_VALUE

'Cat count' becomes CAT_COUNT

"This style is used for the names of constants (final static fields)."

3) CamelCase – All words are written with lowercase letters, except the first letter of each word is uppercase. For example:

'Green house' becomes 'GreenHouse'

'Hollywood girl' becomes 'HollywoodGirl'

This style is used for the names of classes and interfaces.

4) Lower CamelCase (mixed case) – All words are written using lowercase letters, except the first letter of each word except the first is uppercase. For example:

'Get width' becomes 'getWidth'

'Get Hollywood girl name' becomes 'getHollywoodGirlName'

"This style is used for the names of variables and methods."

"So, there aren't too many rules."

1) Everything is written in Lower CamelCase.

2) Names of classes and interfaces are always capitalized.

3) Package names are always lowercase.

4) Constants are always uppercase.

"There are couple of nuances, but in general that's what it is."

"Now about methods. "Method names almost always start with a verb! 'count' is a bad name for a method. It's better to call it getCount(). A method performs some action on the object: startDownload, interrupt , sleep , loadPirateMusic."

"As you already know, there are getters and setters for working with an object's properties/fields: getName/setName, getCount/setCount, etc."

"The only exception is for booleans. For booleans, getter names use 'is', not 'get', e.g. isDone, isEmpty. This way it's closer to ordinary speech."

"How about working for two hours a day instead of 8? Tempted?"


"As you should be. For a junior Java developer, the basic requirement is an excellent understanding of the basics of Java, i.e. Java Core."

"I have another question. Why do we have these different methods for getting the number of elements?"

Class Method/property for getting the number of elements
String length()
Array length
ArrayList size()
ThreadGroup activeCount()

"First of all, Java was invented more than 20 years ago, before requirements like setCount/getCount had been established, and there was a common approach taken from the C language to 'make it as short as possible'."

"Second, semantics plays a role here. When talking about an array, we speak of its length. When talking about a collection, we speak of its size."

"What an interesting lesson."

"I'd like to tell you more, but I'm afraid that you won't remember it all at once. It's better to dish it out to you in small servings."

"But I do want to touch on style regarding the use of curly brackets: {}. There are two approaches:"

1) The bracket goes on a new line every time

2) The opening bracket goes at the end of the previous line, while the closing bracket goes on a new line. This style is called 'Egyptian braces'.

"Honestly, you get to choose how to code. Many people use an opening brace on the same line. Many people put it on a new line. It's like the debate over which end of the egg to break: the little end or the big end."

"The only thing I can recommend is to stick to whatever style is being used in the project your working on. Don't change someone else's code to match your preferred style. People are imperfect. I'm telling you this as Doctor Bilaabo."

"Thanks for the interesting lesson, Bilaabo. I'll go to reflect on what you've said."