Skip to content

Commit 71b6cf0

Browse files
anchoulsonewhl
authored andcommitted
Added сourse theory
1 parent 03673e2 commit 71b6cf0

File tree

67 files changed

+616
-68
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+616
-68
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
IntelliJ IDEA provides two ways to comply with the project’s code style guidelines:
2+
**_code schemas_** and **_editor configs_**.
3+
In this lesson, we will talk about both of them.
4+
5+
### Code schemas
6+
7+
There are two predefined code schemas:
8+
9+
- **Project scheme**: you configure settings for one specific project, they’re stored in the codeStyles folder
10+
under **.idea** and are shared through the VCS together with the project.
11+
- **Default scheme**: you configure the settings that could be applied to all the projects you work on in the IDE that have the Default
12+
schema selected.
13+
14+
Note that code schemas are language-specific.
15+
16+
To configure the **code schema**,
17+
1. Press `⌘,` (macOS) or `Ctrl+Alt+S` (Linux/Windows) to open the IDE settings and select Editor | Code Style.
18+
2. Select `Java` programming language.
19+
3. Select the code schema you would like to configure (Project or Default).
20+
4. Configure code style settings, such as tabs and indents, import settings (using single name import or import with `*`), and many others.
21+
22+
<p align="center">
23+
<img src="../../../common/src/main/resources/images/CodeStyleAndFormatting/CodeSchemasAndEditorConfig/code_style.png" alt="Code Style" width="400"/>
24+
</p>
25+
26+
### EditorConfig
27+
28+
Another way to configure code style settings is to define the **EditorConfig.**
29+
Adding an EditorConfig to your project is pretty easy – you just need to add the `.editorconfig` file to the directory you
30+
would like to apply code style settings to.
31+
32+
It is also possible to have several `.editorconfig` files in the project,
33+
for example, one for the source code and another for tests. Just put them in the corresponding folders, and all settings
34+
will be applied to all sub-folders.
35+
36+
There are two types of settings in `.editorconfig` files:
37+
- Standard options from EditorConfig, such as `tabs_width` or `indent_size`. The full list of options
38+
is available [here](https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties).
39+
- IntelliJ IDEA-specific options, their names start with the `ij_` prefix. For example, `ij_smart_tabs`.
40+
41+
To better understand how EditorConfig works and see examples, please read its
42+
specification [here](https://spec.editorconfig.org/).
43+
If some property is not configured in `.editorconfig`, the IDE takes it from the code schema.
44+
45+
**See Also**: [Detailed overview of formatting features by Trisha Gee](https://blog.jetbrains.com/idea/2020/06/code-formatting/)

CodeStyleAndFormatting/Introduction/CodeStyleAndFormattingIntroduction/task.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ In this section, we will cover the following topics:
44
- The concepts of code style and formatting
55
- Code style schema and EditorConfig
66
- Automatic formatting using the IDE
7+
8+
<p align="center">
9+
<img src="../../../common/src/main/resources/images/CodeStyleAndFormatting/Introduction/code_style_and_formatting_intro.png" alt="Code style and formatting" width="400"/>
10+
</p>

CodeStyleAndFormatting/WhatIsFormatting/ReformatTheCodeUsingIDEPractice/task.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22

33
### Theory
44

5+
To perform any refactoring in the IDE, you can use the Refactoring Menu or a specific shortcut.
6+
It’s also possible to configure custom shortcuts.
7+
Use the **&shortcut:ReformatCode;** (macOS) or **Ctrl + Alt + L** (Windows/Linux) shortcut to make the IDE automatically reformat your code.
8+
You can run it on some specific code fragment if you first select it and then type the shortcut, but you can also run it on the whole file.
9+
10+
You can specify reformatting settings in the **Reformat dialog**, which is available by pressing
11+
**&shortcut:ShowReformatFileDialog;** (macOS) or `Ctrl+Alt+Shift+L` (Windows/Linux) shortcut.
12+
In this dialog, you can specify the scope of formatting changes and select the options to apply:
13+
- **Optimize imports** option will remove all unused imports in your file. Alternatively, you can invoke it using the **&shortcut:OptimizeImports;** or **Ctrl+Alt+O** (Windows/Linux) shortcut.
14+
- **Code cleanup** option runs code cleanup inspections.
15+
- **Do not keep line breaks** option reformats line breaks according to the code style settings.
16+
17+
<p align="center">
18+
<img src="../../../common/src/main/resources/images/CodeStyleAndFormatting/WhatIsFormatting/reformat_dialog.png" alt="Reformat dialog" width="400"/>
19+
</p>
20+
521
### Task
622

723
In this task, you need to reformat the provided code fragment using the shortcut.
Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,53 @@
1-
# Task 1/4: What is formatting?
1+
# Task 1/4: What is formatting?
2+
3+
**_Code style_** is a set of guidelines used in a development team on how to write code.
4+
Usually, the code style depends on the programming language and developers’ preferences and includes rules about formatting,
5+
documentation, naming conventions, and other aspect that affect code readability.
6+
7+
**_Code formatting_** refers to the arrangement of code, including indentation, spacing, line breaks, and other stylistic conventions.
8+
Consistent indentation, clear spacing, and logical line breaks help developers quickly understand the code structure,
9+
flow, and relationships between different code elements.
10+
11+
Let's take a look at the two simple code snippets.
12+
13+
**Before fixing formatting:**
14+
```java
15+
public class Main {
16+
public static void main(String[] args){
17+
double l = 5.0; double w = 3.0; double area = calculateRectangleArea(l, w);
18+
System.out.println("Rectangle area: " + area);
19+
}
20+
21+
public static double calculateRectangleArea(double length,double width){
22+
return length*width; }
23+
}
24+
```
25+
This code snippet doesn't have consistent indentation and proper spacing, making the code difficult to read and understand.
26+
27+
**After fixing formatting:**
28+
```java
29+
public class Main {
30+
public static void main(String[] args) {
31+
double l = 5.0;
32+
double w = 3.0;
33+
double area = calculateRectangleArea(l, w);
34+
System.out.println("Rectangle area: " + area);
35+
}
36+
37+
public static double calculateRectangleArea(double length, double width) {
38+
return length * width;
39+
}
40+
}
41+
```
42+
The **refactored version** demonstrates improved formatting and naming, leading to enhanced readability and maintainability.
43+
44+
<div class="hint" title="Code Formatting's Role in Code Review">
45+
46+
One of the examples that shows how a lack of consistency might be confusing is the code review process.
47+
In real life, developers use version control systems to share their code with colleagues, and **inconsistent formatting
48+
makes the code review process more complicated**.
49+
Imagine you and your colleague use different formatting styles, and your colleague makes a minor change in the code but
50+
then formats the code according to their style.
51+
It will lead to the situation when you see lots of code line changes, but in fact, only a couple of lines contain functional changes.
52+
It is one of the examples of why you should pay attention to formatting.
53+
</div>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
# Section 8: Conclusion
22

33
Congratulations! 🎉
4+
5+
<p align="center">
6+
<img src="../../../common/src/main/resources/images/Conclusion/Conclusion/conclusion_conclusion.png" alt="Conclusion" width="400"/>
7+
</p>
8+
9+
You've completed the **Introduction to IDE Java Code Refactoring** course.
10+
You've dived into the art of enhancing code quality, improving software design, and mastering techniques
11+
to make your codebase more readable, maintainable, and flexible.
12+
13+
As you continue on your programming journey, here are a few recommended next steps:
14+
- **Practice makes perfect**: Keep refining your skills by applying the concepts you've learned to real-world projects. The more you refactor and apply design patterns, the more intuitive and effective these techniques will become.
15+
- **Explore advanced patterns**: While this course introduced you to several fundamental design patterns, there are many more out there waiting for you to explore. Patterns like Observer, Singleton, and Decorator are just a few examples. Dive into resources and books dedicated to design patterns to expand your knowledge.
16+
- **Read widely**: Stay curious and keep reading articles, blogs, and books on software development, best practices, and emerging technologies.
17+
18+
Here's a **list of recommended books and resource**s for further learning on refactorings and design patterns:
19+
- [Refactoring: Improving the Design of Existing Code by Martin Fowler](https://www.oreilly.com/library/view/refactoring-improving-the/9780134757681/)
20+
- [Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides](https://www.oreilly.com/library/view/design-patterns-elements/0201633612/)
21+
22+
Stay curious, stay inspired, and keep coding! 🚀
23+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
# Feedback survey
2+
3+
Thank you for taking our Introduction to IDE Java Code Refactoring course!
4+
We would really appreciate it if you could take a few minutes to
5+
answer [our survey]().
6+
Your feedback will help us improve this course and make it relevant for future students.

ExtractingCode/Introduction/ExtractingCodeIntroduction/task.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ In this section, we will cover the following topics:
44
- Long method code smell
55
- Extract refactoring
66
- Magic numbers
7+
8+
<p align="center">
9+
<img src="../../../common/src/main/resources/images/ExtractingCode/Introduction/extracting_code_intro.png" alt="Extracting code" width="400"/>
10+
</p>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
11
# Task 1/2: What is Extract Method refactoring?
2+
3+
As we mentioned before, **duplicated code can lead to undesirable effects**, such as when you fix a bug in one place
4+
but forget to fix it consistently in duplicates.
5+
The bug will still remain, and now it is even harder to find.
6+
7+
We recommend avoiding duplicated code, but what can you do if it already exists?
8+
The solution is to **extract the duplicated code into a new separate function** and replace all the duplicated code fragments
9+
with calls to the newly introduced function.
10+
It is called an **Extract Method**.
11+
12+
Also, we apply Extract Method refactoring when we encounter **long methods**.
13+
While there's no fixed limit on the number of lines a method should have, **consider extracting
14+
a block of code statements into a separate method** if those statements are cohesive
15+
and expose a piece of functionality that can be reused in other places.
16+
This also improves code readability and organization.
17+
18+
Smaller functions make the code easier to read and understand, as each method has one single responsibility,
19+
making the overall flow more evident.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
# Task 1/2: What is Extract Variable refactoring?
2+
3+
A *magic number* is a numerical value used in the code without a comment explaining its purpose.
4+
Using magic numbers in code is considered bad practice because it makes the code prone to errors and less maintainable and readable.
5+
6+
To resolve this problem, you can press the &shortcut:IntroduceConstant; (macOS) or `Ctrl+Alt+C` (Windows/Linux) shortcut to create a constant variable
7+
with a meaningful name and use it in the code.
8+
9+
If there are duplicated values in the code (string values or just lines of code),
10+
you can use the &shortcut:IntroduceVariable; (macOS) or `Ctrl+Alt+V` (Windows/Linux) shortcut to introduce a variable.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
# Task 1/2: Inline Method refactoring
2+
3+
Apply the Inline Method refactoring when a method's behavior is simple and its purpose is clear, but it is called from only one place.
4+
If the method has become redundant or does not add significant value, it can be safely inlined.
5+
6+
To apply the Inline Method refactoring, select the code you want to inline and press the &shortcut:Inline; (macOS) or `Ctrl+Alt+N` (Windows/Linux) shortcut.

0 commit comments

Comments
 (0)