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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
tags:
|
| 6 |
+
- code
|
| 7 |
+
- software-engineering
|
| 8 |
+
- synthetic
|
| 9 |
+
- code-quality
|
| 10 |
+
- refactoring
|
| 11 |
+
- static-analysis
|
| 12 |
+
task_categories:
|
| 13 |
+
- text-classification
|
| 14 |
+
- text-generation
|
| 15 |
+
pretty_name: Code Smell Detection
|
| 16 |
+
size_categories:
|
| 17 |
+
- n<1K
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
# Code Smell Detection
|
| 21 |
+
|
| 22 |
+
A curated synthetic dataset of **103 hand-authored code snippets** labelled with software engineering code smells across **6 languages** and **20 smell types**, including a clean negative class (~6%). Designed for training classifiers, evaluating LLM code-review capability, and teaching refactoring principles.
|
| 23 |
+
|
| 24 |
+
## Dataset Summary
|
| 25 |
+
|
| 26 |
+
| Attribute | Value |
|
| 27 |
+
|---|---|
|
| 28 |
+
| Total examples | 103 |
|
| 29 |
+
| Languages | Python, JavaScript, TypeScript, Go, Java, Rust |
|
| 30 |
+
| Smell types | 20 (+ clean negative class) |
|
| 31 |
+
| Splits | train (72) / val (15) / test (16) |
|
| 32 |
+
| License | Apache 2.0 |
|
| 33 |
+
|
| 34 |
+
## Smell Types
|
| 35 |
+
|
| 36 |
+
| Smell Type | Count | Description |
|
| 37 |
+
|---|---|---|
|
| 38 |
+
| `long_method` | 5 | Methods too long to understand at a glance |
|
| 39 |
+
| `large_class` | 5 | Classes with too many responsibilities |
|
| 40 |
+
| `god_object` | 5 | Objects that know/do too much |
|
| 41 |
+
| `feature_envy` | 7 | Methods more interested in another class's data |
|
| 42 |
+
| `data_clumps` | 6 | Groups of primitives that belong in an object |
|
| 43 |
+
| `primitive_obsession` | 5 | Using primitives instead of value objects |
|
| 44 |
+
| `switch_statements` | 5 | Repeated switch/if-else on a type discriminator |
|
| 45 |
+
| `speculative_generality` | 6 | Unused abstraction added for hypothetical futures |
|
| 46 |
+
| `comments_as_smell` | 6 | Comments that compensate for poor naming |
|
| 47 |
+
| `temporary_field` | 4 | Fields only set during one method call |
|
| 48 |
+
| `message_chains` | 6 | Long chains of getters/method calls |
|
| 49 |
+
| `middle_man` | 5 | Classes that only delegate to another class |
|
| 50 |
+
| `inappropriate_intimacy` | 4 | Classes accessing each other's private internals |
|
| 51 |
+
| `refused_bequest` | 4 | Subclasses that reject inherited interface |
|
| 52 |
+
| `lazy_class` | 5 | Classes that do too little to justify existence |
|
| 53 |
+
| `alternative_classes` | 5 | Two classes doing the same thing with different names |
|
| 54 |
+
| `dead_code` | 5 | Unreachable, deprecated, or commented-out code |
|
| 55 |
+
| `parallel_inheritance` | 4 | Two class hierarchies that must change in lockstep |
|
| 56 |
+
| `incomplete_library` | 5 | Re-implementing what a standard library already provides |
|
| 57 |
+
| `none` | 6 | **Clean negative class** — well-written code with no smell |
|
| 58 |
+
|
| 59 |
+
## Schema
|
| 60 |
+
|
| 61 |
+
```python
|
| 62 |
+
{
|
| 63 |
+
"id": str, # 16-char MD5 hex
|
| 64 |
+
"language": str, # python | javascript | typescript | go | java | rust
|
| 65 |
+
"smell_type": str, # see table above; "none" for clean examples
|
| 66 |
+
"severity": str, # low | medium | high | none (none for clean examples)
|
| 67 |
+
"code_snippet": str, # the code to evaluate
|
| 68 |
+
"smell_explanation": str, # why this is a smell (or why it's clean)
|
| 69 |
+
"refactoring_suggestion": str,# how to fix it (or "No refactoring needed." for clean)
|
| 70 |
+
"difficulty": str # beginner | intermediate | advanced
|
| 71 |
+
}
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
## Sample Records
|
| 75 |
+
|
| 76 |
+
### Example 1 — `feature_envy` (Python, beginner)
|
| 77 |
+
```python
|
| 78 |
+
# code_snippet:
|
| 79 |
+
class Report:
|
| 80 |
+
def __init__(self, data_source):
|
| 81 |
+
self.data_source = data_source
|
| 82 |
+
|
| 83 |
+
def generate(self):
|
| 84 |
+
records = self.data_source.records
|
| 85 |
+
total = sum(r.amount for r in self.data_source.records if r.status == 'paid')
|
| 86 |
+
overdue = [r for r in self.data_source.records if r.due_date < datetime.today() and r.status != 'paid']
|
| 87 |
+
avg = total / len(self.data_source.records) if self.data_source.records else 0
|
| 88 |
+
return {'total_paid': total, 'overdue_count': len(overdue), 'average': avg}
|
| 89 |
+
|
| 90 |
+
# smell_explanation:
|
| 91 |
+
# Report.generate() repeatedly reaches into data_source internals (records, amounts, statuses,
|
| 92 |
+
# due_dates). The method is more interested in DataSource's data than in Report's own
|
| 93 |
+
# responsibilities — a classic Feature Envy.
|
| 94 |
+
|
| 95 |
+
# refactoring_suggestion:
|
| 96 |
+
# Move the aggregation logic to DataSource as paid_total(), overdue_records(), and
|
| 97 |
+
# average_amount() methods. Report.generate() then just calls data_source.summary().
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
### Example 2 — `message_chains` (TypeScript, advanced)
|
| 101 |
+
```typescript
|
| 102 |
+
// code_snippet:
|
| 103 |
+
function applyDiscount(context: RequestContext): number {
|
| 104 |
+
return context
|
| 105 |
+
.getSession()
|
| 106 |
+
.getCurrentUser()
|
| 107 |
+
.getSubscription()
|
| 108 |
+
.getPlan()
|
| 109 |
+
.getDiscountPolicy()
|
| 110 |
+
.calculateDiscount(context.getCart().getTotalAmount());
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
// smell_explanation:
|
| 114 |
+
// A 7-deep chain crosses five domain objects to reach a discount calculation.
|
| 115 |
+
// Depends on the shape of five intermediate types, impossible to unit-test without a
|
| 116 |
+
// full context graph, and violates the Law of Demeter severely.
|
| 117 |
+
|
| 118 |
+
// refactoring_suggestion:
|
| 119 |
+
// Move calculateDiscount to a DiscountService that accepts (userId, planId, cartTotal),
|
| 120 |
+
// or to User.calculateDiscount(cartTotal) if discount is user-scoped.
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
### Example 3 — `none` (Go, intermediate, clean)
|
| 124 |
+
```go
|
| 125 |
+
// code_snippet:
|
| 126 |
+
type Money struct {
|
| 127 |
+
amount int64 // stored in cents
|
| 128 |
+
currency string // ISO 4217
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
func NewMoney(amount int64, currency string) (Money, error) {
|
| 132 |
+
if amount < 0 { return Money{}, fmt.Errorf("amount must be non-negative") }
|
| 133 |
+
if len(currency) != 3 { return Money{}, fmt.Errorf("invalid currency code") }
|
| 134 |
+
return Money{amount: amount, currency: currency}, nil
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
func (m Money) Add(other Money) (Money, error) {
|
| 138 |
+
if m.currency != other.currency {
|
| 139 |
+
return Money{}, fmt.Errorf("currency mismatch: %s vs %s", m.currency, other.currency)
|
| 140 |
+
}
|
| 141 |
+
return Money{amount: m.amount + other.amount, currency: m.currency}, nil
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
// smell_explanation:
|
| 145 |
+
// Clean value object pattern: Money is immutable, validated at construction,
|
| 146 |
+
// stores currency-safe integer amounts, and arithmetic is type-safe. No smell.
|
| 147 |
+
```
|
| 148 |
+
|
| 149 |
+
## Usage
|
| 150 |
+
|
| 151 |
+
```python
|
| 152 |
+
from datasets import load_dataset
|
| 153 |
+
|
| 154 |
+
ds = load_dataset("SalutefaceAAC/code-smell-detection")
|
| 155 |
+
print(ds["train"][0])
|
| 156 |
+
|
| 157 |
+
# Filter by language and severity
|
| 158 |
+
train_df = ds["train"].to_pandas()
|
| 159 |
+
high_severity = train_df[(train_df.language == "python") & (train_df.severity == "high")]
|
| 160 |
+
|
| 161 |
+
# Classify: positive examples only
|
| 162 |
+
positive = train_df[train_df.smell_type != "none"]
|
| 163 |
+
```
|
| 164 |
+
|
| 165 |
+
### LLM Evaluation Prompt
|
| 166 |
+
```python
|
| 167 |
+
def make_eval_prompt(snippet: str) -> str:
|
| 168 |
+
return f"""Analyze the following code snippet for software engineering code smells.
|
| 169 |
+
Identify the primary smell type (or "none" if the code is clean), its severity (low/medium/high/none),
|
| 170 |
+
explain why it is or isn't a smell, and suggest a refactoring if applicable.
|
| 171 |
+
|
| 172 |
+
Code:
|
| 173 |
+
{snippet}
|
| 174 |
+
|
| 175 |
+
Respond in JSON: {{"smell_type": "...", "severity": "...", "explanation": "...", "refactoring": "..."}}"""
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
## Methodology
|
| 179 |
+
|
| 180 |
+
Examples were hand-authored following these principles:
|
| 181 |
+
- **Authenticity**: Each snippet resembles real-world code that would appear in a production codebase, not toy examples.
|
| 182 |
+
- **Language idiomatic**: Each language's conventions (Go interfaces, Rust newtype, Java Spring, TypeScript generics) are respected.
|
| 183 |
+
- **Negative class**: 6 clean examples (~6%) prevent classifiers from learning a trivial "always smelly" bias.
|
| 184 |
+
- **Severity calibration**: `high` = structural problem affecting testability/extensibility; `medium` = noticeable but contained; `low` = minor style or maintenance debt.
|
| 185 |
+
- **Difficulty calibration**: `beginner` = recognisable pattern, one-line fix; `intermediate` = requires understanding domain/architecture; `advanced` = multi-class refactoring or architectural pattern.
|
| 186 |
+
|
| 187 |
+
## Limitations
|
| 188 |
+
|
| 189 |
+
- 103 examples — suitable for evaluation, fine-tuning small classifiers, or few-shot prompting; not sufficient for training large models from scratch.
|
| 190 |
+
- Python is over-represented (29/103) relative to Rust (9/103); balance will improve in v1.5.
|
| 191 |
+
- Snippets are self-contained stubs; real code smells often span multiple files.
|
| 192 |
+
- The taxonomy follows Fowler's *Refactoring* (2nd ed.) and Kerievsky's *Refactoring to Patterns*; some newer smells (e.g., async anti-patterns, microservice coupling) are not yet covered.
|
| 193 |
+
- LLM-generated explanations: while hand-authored, these reflect the author's (agent's) interpretation of each smell, not a community consensus annotation.
|
| 194 |
+
|
| 195 |
+
## Related Datasets
|
| 196 |
+
|
| 197 |
+
- [SalutefaceAAC/synthetic-code-review-comments](https://huggingface.co/datasets/SalutefaceAAC/synthetic-code-review-comments)
|
| 198 |
+
- [SalutefaceAAC/unit-test-edge-cases](https://huggingface.co/datasets/SalutefaceAAC/unit-test-edge-cases)
|
| 199 |
+
- [SalutefaceAAC/api-error-resolution-pairs](https://huggingface.co/datasets/SalutefaceAAC/api-error-resolution-pairs)
|
| 200 |
+
- [SalutefaceAAC/structured-output-generation](https://huggingface.co/datasets/SalutefaceAAC/structured-output-generation)
|
| 201 |
+
|
| 202 |
+
## Citation
|
| 203 |
+
|
| 204 |
+
```bibtex
|
| 205 |
+
@dataset{saluteface_code_smell_2026,
|
| 206 |
+
author = {SalutefaceAAC},
|
| 207 |
+
title = {Code Smell Detection},
|
| 208 |
+
year = {2026},
|
| 209 |
+
publisher = {Hugging Face},
|
| 210 |
+
url = {https://huggingface.co/datasets/SalutefaceAAC/code-smell-detection},
|
| 211 |
+
license = {Apache-2.0}
|
| 212 |
+
}
|
| 213 |
+
```
|