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
@drawers
drawers / Destination.kt
Created March 29, 2024 01:56
A simple enum
enum class Destination {
HOME,
NOTIFICATIONS,
ACCOUNTS
}
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."))
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 October 7, 2023 23:16
Clean Code-style refactor of Majority Element solution
class Solution {
private var count = 0
private var candidate: Int? = null
fun majorityElement(nums: IntArray): Int {
for (num in nums) {
candidate = updateCandidateForZeroCount(num)
count = updateCountForCandidate(num)
}
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
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
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
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");
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 {
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.",