Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #60

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e71a80d
refactor: change the lesson type to "lesson" and add placeholders
GirZ0n Nov 3, 2023
a312fa1
refactor: change the lesson type to "lesson" and add placeholders
GirZ0n Nov 3, 2023
e48f49e
fix: remove an empty main function
GirZ0n Nov 3, 2023
4b0fbf8
fix: union the placeholders
GirZ0n Nov 3, 2023
96e023f
refactor: change the lesson type to "lesson" and add placeholders
GirZ0n Nov 3, 2023
ebcf282
refactor: change the lesson type to "lesson" and add placeholders
GirZ0n Nov 3, 2023
0a9e8dd
refactor: change the lesson type to "lesson" and add placeholders
GirZ0n Nov 3, 2023
c6784e9
fix: fix the framework lesson final step
GirZ0n Nov 6, 2023
63472fa
fix: return the `custom_name` field
GirZ0n Nov 6, 2023
b4a92b0
fix: fix a typo
GirZ0n Nov 6, 2023
4ba7dc7
fix: add a reference solution
GirZ0n Nov 6, 2023
982e5ab
fix: add a reference solution
GirZ0n Nov 6, 2023
2aceb19
fix: fix naming
GirZ0n Nov 6, 2023
b77a984
fix: fix main files
GirZ0n Nov 6, 2023
d106e66
feat: add the Gradle Test config
GirZ0n Nov 6, 2023
dd62a57
bump: update the kotlin test framework version
GirZ0n Nov 7, 2023
4e95946
style: fix formatting
GirZ0n Nov 7, 2023
0e8a67b
fix: add a package name
GirZ0n Nov 7, 2023
b0eafad
style: add a missing question mark
GirZ0n Nov 7, 2023
0391369
Add clarifications to the Extract magic numbers task
onewhl Nov 7, 2023
53bbf58
Add clarification to the Find more appropriate classes for method task
onewhl Nov 7, 2023
57299ab
Add clarification to the Factory Method task
onewhl Nov 7, 2023
5346de6
fix: partially revert 0391369569748b74572a1d1ca87b49b11215b06f
GirZ0n Nov 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/gradle-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Gradle Test

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-20.04

steps:
- uses: actions/[email protected]
- name: Set up JDK 11
uses: actions/[email protected]
with:
java-version: 11
distribution: liberica
- name: Gradle Wrapper Validation
uses: gradle/[email protected]
- uses: gradle/[email protected]
with:
arguments: test
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ fun funWithCodeStyleIssues() {
println("Please, fix me!")
}
}

fun main() {
}

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions CodeStyleAndFormatting/WhatIsFormatting/Quiz/task-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,4 @@ message_incorrect: "Incorrect solution. Explanation: Consistent code formatting
files:
- name: src/main/kotlin/jetbrains/refactoring/course/formatting/Main.kt
visible: true
- name: src/main/kotlin/jetbrains/refactoring/course/formatting/Task1.kt
visible: true
- name: src/main/kotlin/jetbrains/refactoring/course/formatting/Task2.kt
visible: true
local_check: true
1 change: 1 addition & 0 deletions CodeStyleAndFormatting/WhatIsFormatting/Quiz/task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Task 2/4: What is the primary benefit of following consistent code formatting?

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
type: edu
custom_name: Reformat the code
files:
- name: src/main/kotlin/jetbrains/refactoring/course/formatting/Task1.kt
visible: true
- name: test/Tests.kt
visible: false
- name: src/main/kotlin/jetbrains/refactoring/course/formatting/Task2.kt
- name: src/main/kotlin/jetbrains/refactoring/course/formatting/Task.kt
visible: true
placeholders:
- offset: 49
length: 101
placeholder_text: |-
fun funWithLittleFormattingIssues() {
println ("This function definitely has formatting issues" )
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Task 3/4: Reformat the code

### Task

In this task, you need to correct formatting in a file without using IDE features.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class ReformatCodeWithoutIDETest : BaseIjTestClass() {
fun initialize() {
val taskDirectoryPath = System.getProperty("user.dir")
val sourceCodeFile =
File("$taskDirectoryPath/src/main/kotlin/jetbrains/refactoring/course/formatting/Task1.kt")
File("$taskDirectoryPath/src/main/kotlin/jetbrains/refactoring/course/formatting/Task.kt")
sourceText = sourceCodeFile.readText()
}
}

@Test
fun testSolutionIsFormatted() {
setUp()
myFixture.configureByText("Task1.kt", sourceText)
myFixture.configureByText("Task.kt", sourceText)
myFixture.file.checkIfFormattingRulesWereApplied()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
type: edu
custom_name: Reformat the code using IDE
files:
- name: src/main/kotlin/jetbrains/refactoring/course/formatting/Task2.kt
visible: true
- name: test/Tests.kt
visible: false
- name: src/main/kotlin/jetbrains/refactoring/course/formatting/Task.kt
visible: true
placeholders:
- offset: 49
length: 227
placeholder_text: |-
import java.io.File

fun funWithFormattingIssues() {
println ("This function definitely has formatting issues" )
println( "... that could be easily fixed using one shortcut")
for ( i in 1 ..10) {
println("Please, format me!")
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Task
# Task 4/4: Reformat the code using IDE

#### Theory
### Theory

To perform any refactoring in the IDE, you can use the Refactoring Menu or a specific shortcut.
It’s also possible to configure custom shortcuts.
Expand All @@ -18,7 +18,7 @@ In this dialog, you can specify the scope of formatting changes and select the o
<img src="../../../util/src/main/resources/images/CodeStyleAndFormatting/WhatIsFormatting/reformat_dialog.png" alt="Reformat dialog" width="400"/>
</p>

#### Task
### Task

In this task, you need to reformat the provided code fragment using the shortcut.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ class ReformatCodeTest : BaseIjTestClass() {
fun initialize() {
val taskDirectoryPath = System.getProperty("user.dir")
val sourceCodeFile =
File("$taskDirectoryPath/src/main/kotlin/jetbrains/refactoring/course/formatting/Task2.kt")
File("$taskDirectoryPath/src/main/kotlin/jetbrains/refactoring/course/formatting/Task.kt")
sourceText = sourceCodeFile.readText()
}
}

@Test
fun testSolutionIsFormatted() {
setUp()
myFixture.configureByText("Task2.kt", sourceText)
myFixture.configureByText("Task.kt", sourceText)
myFixture.file.checkIfFormattingRulesWereApplied()
}

@Test
fun testUnusedImportsAreRemoved() {
setUp()
myFixture.configureByText("Task2.kt", sourceText)
myFixture.configureByText("Task.kt", sourceText)
myFixture.file.checkIfOptimizeImportsWereApplied()
}
}

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions CodeStyleAndFormatting/WhatIsFormatting/Theory/task-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@ custom_name: What is formatting?
files:
- name: src/main/kotlin/jetbrains/refactoring/course/formatting/Main.kt
visible: true
- name: src/main/kotlin/jetbrains/refactoring/course/formatting/Task1.kt
visible: false
- name: src/main/kotlin/jetbrains/refactoring/course/formatting/Task2.kt
visible: false
2 changes: 2 additions & 0 deletions CodeStyleAndFormatting/WhatIsFormatting/Theory/task.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Task 1/4: What is formatting?

**_Code style_** is a set of guidelines used in a development team on how to write code.
Usually, the code style depends on the programming language and developers’ preferences and includes rules about formatting,
documentation, naming conventions, and other aspect that affect code readability.
Expand Down
4 changes: 1 addition & 3 deletions CodeStyleAndFormatting/WhatIsFormatting/lesson-info.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
type: framework
custom_name: What is formatting
custom_name: What is formatting?
content:
- Theory
- Quiz
- ReformatTheCode
- ReformatTheCodeUsingIDE
is_template_based: false
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,29 @@ custom_name: Extract duplicated code
files:
- name: src/main/kotlin/jetbrains/refactoring/course/extracting/Task.kt
visible: true
placeholders:
- offset: 107
length: 504
placeholder_text: |-
fun wordCount(fileName: String): Int {
val file = File(fileName)
if (!file.exists() || !file.isFile) {
throw FileNotFoundException()
}
var wordCount = 0
file.forEachLine { line ->
wordCount += line.split("\\s+".toRegex()).count()
}
return wordCount
}

fun copyFile(srcName: String, destName: String) {
val file = File(srcName)
val to = File(destName)
if (!file.exists() || !file.isFile) {
throw FileNotFoundException()
}
file.copyTo(to)
}
- name: test/Tests.kt
visible: false
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Task 2/2: Extract duplicated code

### Task

In this task, you need to identify duplicated code and extract it into a separate function.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package jetbrains.refactoring.course.extracting

fun main() {
// Write your solution here
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type: theory
custom_name: What is Extract Method refactoring?
files:
- name: src/main/kotlin/jetbrains/refactoring/course/extracting/Task.kt
- name: src/main/kotlin/jetbrains/refactoring/course/extracting/Main.kt
visible: true
2 changes: 2 additions & 0 deletions ExtractingCode/WhatIsExtractMethodRefactoring/Theory/task.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Task 1/2: What is Extract Method refactoring?

As we mentioned before, **duplicated code can lead to undesirable effects**, such as when you fix a bug in one place
but forget to fix it consistently in duplicates.
The bug will still remain, and now it is even harder to find.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
type: framework
custom_name: What is Extract Method refactoring?
content:
- Theory
- ExtractDuplicatedCode
is_template_based: false
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,25 @@ custom_name: Extract magic numbers
files:
- name: src/main/kotlin/jetbrains/refactoring/course/extracting/Task.kt
visible: true
placeholders:
- offset: 49
length: 586
placeholder_text: |-
fun calculatePhotonEnergy(waveLength: Double): Double {
val frequency = 299792458.0 / waveLength
return 6.62607015e-34 * frequency
}

fun calculatePhotonMass(energy: Double): Double {
return energy / (299792458.0 * 299792458.0)
}

fun main() {
val photonEnergy = calculatePhotonEnergy(0.5e-6)
val photonMass = calculatePhotonMass(photonEnergy)

println("Photon energy: $photonEnergy Joules")
println("Photon mass: $photonMass kg")
}
- name: test/Tests.kt
visible: false
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Task 2/2: Extract magic numbers

### Task

In this task, you need to identify magic numbers and create constant values for them.
To calculate the **energy of a photon**, we use a simple formula: multiply the Planck constant, which is a very small
number (`6.62607015 × 10^-34`), by the light's frequency (the number of waves that pass a point in one second). To find
the frequency of light, divide the speed of light (which is `299,792,458` meters per second) by the wave length of
the light. The greater the frequency, the higher the energy of the photon.

To calculate the **mass of a photon**, take the energy of the photon and divide it by the speed of
light squared. However, in practice, photons are considered to have zero mass.

In this task, you need to identify magic numbers in the code and create constant values for them.

### Hints

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package jetbrains.refactoring.course.extracting

fun main() {
// Write your solution here
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type: theory
custom_name: What is Extract Variable refactoring?
files:
- name: src/main/kotlin/jetbrains/refactoring/course/extracting/Task.kt
- name: src/main/kotlin/jetbrains/refactoring/course/extracting/Main.kt
visible: true
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Task 1/2: What is Extract Variable refactoring?

A *magic number* is a numerical value used in the code without a comment explaining its purpose.
Using magic numbers in code is considered bad practice because it makes the code prone to errors and less maintainable and readable.

Expand Down
Loading
Loading