General Concepts in Code Writing Flashcards
(30 cards)
Why clean code?
- Code readability is a problem.
- An unreadable code is hard to maintain.
“Code is clean if it can be understood easily –by
everyone on the team. With understandability comes
readability, changeability, extensibility and
maintainability…without accumulating up a large
amount of technical debt.”
According to Martin Fowler (2003), ______ is
metaphor by which “doing things the quick and dirty
way sets us up with a technical debt, which is similar to
a financial debt.”
technical debt
is a surface indication that usually
corresponds to a deeper problem in the system.
code smell
is a disciplined technique for restructuring
an existing body of code, altering its internal structure
without changing its external behavior.
Refactoring
In simple words, not writing the best implementation
from the very beginning may cost a lot in the latter
parts of the development.
Code rewriting!
- “A source code with a complex and tangled control
structure” (Programming Lore). - Anti-pattern; difficult to understand and maintain
Spaghetti code
Code smells Examples
DLLC IUC LEP
- Duplicated Code
- Long Methods/Functions
- Large Classes/Modules
- Comments that indicate a code smell
- Inconsistent Naming Conventions
- Unused Code
- Complex Conditionals
- Lack of Error Handling
- Excessive Nesting
- Primitive Obsession
“When the same code is repeated in
multiple places, it can lead to maintenance problems
and increase the likelihood of introducing bugs”.
Duplicated Code
float area_of_triangle(float base, float height) {
return 0.5 * base * height;
}
float area_of_rectangle(float width, float height) {
return width * height;
}
float area_of_square(float side) {
return side * side;
}
Duplicated Code Sample
“Functions that are too long
can make it difficult to understand the code and can be
a sign that the function is doing too many things”.
Long Methods/Functions
“Similarly, large classes or
modules can make it difficult to understand the code
and can be a sign that the class/module is doing too
many things”.
Large Classes/Modules
“Sometimes,
developers leave comments in their code that indicate a problem that should be addressed”.
Comments that indicate a code smell
- “Inconsistent
naming of variables, functions, or classes can make it
difficult to understand the code and can lead to
confusion”.
Inconsistent Naming Conventions
“Code that is not being used should be
removed to avoid confusion and clutter”.
Unused Code
“Complex conditionals that are
difficult to read and understand can be a sign that the
logic is too convoluted and could be refactored for
clarity”.
Complex Conditionals
“Excessive nesting of loops,
conditionals, or functions can make the code difficult to
read and understand”.
Excessive Nesting
“Overuse of primitive data types
can lead to code that is difficult to maintain and can
make it more difficult to add new features”.
Primitive Obsession
Styles of Writing and General Tips
On comments:
Explain yourself in code.
* Good comment
* Legal comment
* Clarification comment
* Warning of consequence
* TODO comments
Bad comments include:
mumbling, redundant,
misleading, mandated, journal comments, noise
comments, and scary noise.
Styles of Writing and General Tips
On Functions:
Functions should only do one thing!
Flag arguments in functions are not a good idea!
On Functions:
Arguments
zero arguments
Niladic
On Functions:
Arguments
one argument
Monadic
On Functions:
Arguments
two arguments
dyadic
On Functions:
Arguments
three arguments
triads