class Boolean(Field):
default_error_messages = {
'invalid': 'Not a valid boolean.'
}
def __init__(self, **kwargs):
kwargs.setdefault('default', False)
super(Boolean, self).__init__(**kwargs)
class Raw(Field):
default_error_messages = {
'invalid': 'Not a valid python object'
}
def __init__(self, **kwargs):
super(Raw, self).__init__(**kwargs)
Description of the deficiency :
Duplicate Code
This code snippet shows two different field classes (Boolean and Raw) that have very similar constructor code blocks. They both define a default_error_messages dictionary and call the parent Field constructor with some default arguments. This violates the DRY (Don't Repeat Yourself) principle and leads to code duplication.
2) "Quote" of bad code:
class Integer(Field):
default_error_messages = {
'invalid': 'Not a valid integer.'
}
def __init__(self, **kwargs):
kwargs.setdefault('min', None)
kwargs.setdefault('max', None)
super(Integer, self).__init__(**kwargs)
class Float(Field):
default_error_messages = {
'invalid': 'Not a valid float.'
}
def __init__(self, **kwargs):
kwargs.setdefault('min', None)
kwargs.setdefault('max', None)
super(Float, self).__init__(**kwargs)
Description of the deficiency:
Incomplete Abstraction
This code snippet shows two different field classes (Integer and Float) that have exactly the same constructor code block except for the class name and the default_error_messages dictionary. This violates the DRY principle and leads to code duplication.
3) "Quote" of bad code:
class NestedMixin(object):
def __init__(self, nested, **kwargs):
if isinstance(nested, type):
nested = nested()
if not isinstance(nested, Namespace):
raise ValueError('`nested` must be a Namespace instance')
self.nested = nested
super(NestedMixin, self).__init__(**kwargs)
class List(NestedMixin, _UnsetValueMixin, Field):
...
class Nested(NestedMixin, _UnsetValueMixin, Raw):
...
Description of the deficiency:
Primitive Obsession
The List and Nested classes both inherit from the NestedMixin class, but they do not inherit any code from each other. This violates the DRY principle and leads to code duplication.
4) "Quote" of bad code:
class Method(Field):
...
class Function(Method):
...
class MarshallingMethod(Marshaller):
...
class MarshallingFunction(MarshallingMethod):
...
Description of the deficiency:
Anemic Domain Model
The Method and Function classes are very similar, with the only difference being that Function allows for a callable object to be used as the default value. Similarly, the MarshallingMethod and MarshallingFunction classes are also very similar, with the only difference being that MarshallingFunction takes a callable object instead of a method name. This violates the LSP (Liskov Substitution Principle) and leads to code duplication.
5) "Quote" of bad code:
class String(Field):
...
class FormattedString(Field):
...
class Email(String):
...
class Url(String):
...
class IP(String):
...
class IPv4(IP):
...
class IPv6(IP):
...
Description of the deficiency:
God Object
There are many different field classes (String, FormattedString, Email, Url, IP, IPv4, and IPv6) that all inherit from each other in various ways. This leads to multiple inheritance chains and a large class hierarchy, which can make the code harder to understand and maintain. Additionally, this violates the SRP (Single Responsibility Principle) and the OCP (Open-Closed Principle) because adding a new field type requires modifying the class hierarchy.
6) "Quote" of bad code:
class DateTime(Field):
...
class LocalDateTime(DateTime):
...
class NaiveDateTime(DateTime):
...
class AwareDateTime(DateTime):
...
Description of the deficiency:
Speculative Generality
The DateTime, LocalDateTime, NaiveDateTime, and AwareDateTime classes are all very similar, with the only difference being the behavior of the deserialize method. However, the deserialize method is not abstracted out into a separate class or method, which violates the DIP (Dependency Inversion Principle) and leads to code duplication. Additionally, it leads to code bloat and can make the class hierarchy harder to understand.
2 - it's just duplication code.
3 - it's not primitive obsession.
4 - it's not anemic domain model
5 - it's not God object
6 - it's not Speculative Generality
Please, check the definitions of these terms.
1 point
1) "Quote" of bad code:
Description of the deficiency : Duplicate Code This code snippet shows two different field classes (
Boolean
andRaw
) that have very similar constructor code blocks. They both define adefault_error_messages
dictionary and call the parentField
constructor with some default arguments. This violates the DRY (Don't Repeat Yourself) principle and leads to code duplication.2) "Quote" of bad code:
Description of the deficiency: Incomplete Abstraction
This code snippet shows two different field classes (
Integer
andFloat
) that have exactly the same constructor code block except for the class name and thedefault_error_messages
dictionary. This violates the DRY principle and leads to code duplication.3) "Quote" of bad code:
Description of the deficiency: Primitive Obsession
The
List
andNested
classes both inherit from theNestedMixin
class, but they do not inherit any code from each other. This violates the DRY principle and leads to code duplication.4) "Quote" of bad code:
Description of the deficiency: Anemic Domain Model The
Method
andFunction
classes are very similar, with the only difference being thatFunction
allows for a callable object to be used as the default value. Similarly, theMarshallingMethod
andMarshallingFunction
classes are also very similar, with the only difference being thatMarshallingFunction
takes a callable object instead of a method name. This violates the LSP (Liskov Substitution Principle) and leads to code duplication.5) "Quote" of bad code:
Description of the deficiency: God Object There are many different field classes (
String
,FormattedString
,Email
,Url
,IP
,IPv4
, andIPv6
) that all inherit from each other in various ways. This leads to multiple inheritance chains and a large class hierarchy, which can make the code harder to understand and maintain. Additionally, this violates the SRP (Single Responsibility Principle) and the OCP (Open-Closed Principle) because adding a new field type requires modifying the class hierarchy.6) "Quote" of bad code:
Description of the deficiency: Speculative Generality The
DateTime
,LocalDateTime
,NaiveDateTime
, andAwareDateTime
classes are all very similar, with the only difference being the behavior of thedeserialize
method. However, thedeserialize
method is not abstracted out into a separate class or method, which violates the DIP (Dependency Inversion Principle) and leads to code duplication. Additionally, it leads to code bloat and can make the class hierarchy harder to understand.