Skip to content

Instantly share code, notes, and snippets.

View drawers's full-sized avatar
🟩
In greenish twilight at the bottom of the Rhine

David Rawson drawers

🟩
In greenish twilight at the bottom of the Rhine
View GitHub Profile
View VisitKtFile.kt
override fun visit(root: KtFile) {
super.visit(root)
val classVisitor = UnusedClassVisitor()
root.accept(classVisitor)
classVisitor.privateClasses
.filterNot<KtNamedDeclaration> { it.isUsed() }
.forEach {
report(CodeSmell(issue, Entity.from(it), "Private class ${it.nameAsSafeName.identifier} is unused."))
View KtNamedDeclaration.kt
private fun KtNamedDeclaration.isUsed(): Boolean {
if (nameAsSafeName.identifier in namedClasses) return true
val pathSegments = fqName?.pathSegments().orEmpty()
return pathSegments.isNotEmpty() && importedFqNames.any { importedFqName ->
importedFqName.pathSegments().zip(pathSegments).all { it.first == it.second }
}
}
@drawers
drawers / MajorityElementClean.kt
Last active September 30, 2023 07:44
Clean Code-style refactor of Majority Element solution
View MajorityElementClean.kt
class Solution {
fun majorityElement(nums: IntArray): Int {
var count = getInitialCount()
var candidate = getInitialCandidate()
for (num in nums) {
candidate = updateCandidate(count, candidate, num)
count = updateCount(count, candidate, num)
}
View MajorityElement2.kt
if (count == 0) {
candidate = num
}
if (num == candidate) {
count++
} else {
count--
}
@drawers
drawers / MajorityElement1.kt
Last active September 30, 2023 06:36
Deficient solution to Majority Element
View MajorityElement1.kt
class Solution {
fun majorityElement(nums: IntArray): Int {
var count = 0
var candidate: Int? = null
for (num in nums) {
if (count == 0) {
candidate = num
count++
} else {
@drawers
drawers / TestableHtmlRefactored.java
Created September 24, 2023 03:49
TestableHtml refactor from Clean Code
View TestableHtmlRefactored.java
private void includeSetupAndTeardownPages() throws Exception {
includeSetupPages();
includePageContent();
includeTeardownPages();
updatePageContent();
}
private void includeSetupPages() throws Exception {
if (isSuite)
includeSuiteSetupPage();
@drawers
drawers / TestableHtml.java
Created September 24, 2023 03:44
TestableHtml method to be refactored from Clean Code
View TestableHtml.java
public static String testableHtml(PageData pageData, boolean includeSuiteSetup) throws Exception {
WikiPage wikiPage = pageData.getWikiPage();
StringBuffer buffer = new StringBuffer();
if (pageData.hasAttribute("Test")) {
if (includeSuiteSetup) {
WikiPage suiteSetup = PageCrawlerImpl.getInheritedPage(SuiteResponder.SUITE_SETUP_NAME, wikiPage);
if (suiteSetup != null) {
WikiPagePath pagePath = suiteSetup.getPageCrawler().getFullPath(suiteSetup);
String pagePathName = PathParser.render(pagePath);
buffer.append("!include -setup .").append(pagePathName).append("\n");
View DoubleNegativeTakeUnlessTest.kt
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import io.gitlab.arturbosch.detekt.test.setupKotlinEnvironment
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
class DoubleNegativeTakeUnlessTest {
View DoubleNegativeTakeUnless.kt
import io.gitlab.arturbosch.detekt.api.*
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
class DoubleNegativeTakeUnless(config: Config = Config.empty) : Rule(config) {
override val issue = Issue(
"DoubleNegativeTakeUnless",
Severity.Style,
"Using double negatives with `takeUnless` can make the code harder to read.",
View KSPTest.kt
@Test
fun `target is a data class`() {
val kotlinSource = SourceFile.kotlin(
"file1.kt",
"""
package com.tests.summable
import com.tsongkha.kspexample.annotation.IntSummable
@IntSummable