<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abdullah Aly</title>
    <description>The latest articles on DEV Community by Abdullah Aly (@pandax185).</description>
    <link>https://dev.to/pandax185</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3021499%2F28fc5e75-f006-4490-9823-34c312456eb2.jpg</url>
      <title>DEV Community: Abdullah Aly</title>
      <link>https://dev.to/pandax185</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pandax185"/>
    <language>en</language>
    <item>
      <title>Object-Oriented Programming in Go: A Balanced Comparison with Traditional OOP</title>
      <dc:creator>Abdullah Aly</dc:creator>
      <pubDate>Sat, 05 Apr 2025 23:39:39 +0000</pubDate>
      <link>https://dev.to/pandax185/object-oriented-programming-in-go-abalanced-comparison-with-traditional-oop-36l8</link>
      <guid>https://dev.to/pandax185/object-oriented-programming-in-go-abalanced-comparison-with-traditional-oop-36l8</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Object-oriented programming (OOP) has been a cornerstone of software&lt;br&gt;
engineering, with languages like Java and C++ popularizing class-based&lt;br&gt;
hierarchies. However, deep inheritance trees and rigid structures can&lt;br&gt;
lead to maintenance challenges.&lt;/p&gt;

&lt;p&gt;Go (Golang) offers an alternative by emphasizing composition, implicit&lt;br&gt;
interfaces, and minimalistic design. While praised for readability and&lt;br&gt;
concurrency, Go's OOP model has trade-offs that are often overlooked.&lt;br&gt;
This paper provides a balanced assessment, comparing Go's approach with&lt;br&gt;
traditional OOP and examining empirical data on productivity,&lt;br&gt;
performance, and maintainability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Traditional OOP: Strengths and Weaknesses
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Core Features
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Classes &amp;amp; Objects: Blueprints for data and behavior.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inheritance: Code reuse through class hierarchies.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Polymorphism: Runtime method dispatch via inheritance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Encapsulation: Access control (public, private, protected).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Example in Java
class Animal {
  public void makeSound() {
    System.out.println("Generic sound");
  }
}

class Dog extends Animal {
  @Override
  public void makeSound() {
    System.out.println("Bark");
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Common Pitfalls
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Fragile Base Class Problem: Changes in parent classes break&lt;br&gt;
subclasses.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Deep Hierarchies: Overuse of inheritance leads to rigid designs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Boilerplate: Verbose syntax (e.g., Java's getters/setters).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Go's OOP Model: A Practical Alternative
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Structs Instead of Classes
&lt;/h3&gt;

&lt;p&gt;Go uses lightweight structs for data grouping but lacks constructors and&lt;br&gt;
default methods.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Dog struct {
  Name string
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Methods Attached to Types
&lt;/h3&gt;

&lt;p&gt;Methods are defined separately from structs, promoting flexibility.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func (d Dog) Bark() {
  fmt.Println("Bark!")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Interfaces for Polymorphism
&lt;/h3&gt;

&lt;p&gt;Go uses implicit interfaces, meaning a type satisfies an interface&lt;br&gt;
simply by implementing its methods.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Animal interface {
  MakeSound()
}

func (d Dog) MakeSound() {
  fmt.Println("Bark")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Loose coupling (no implements keyword)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easier mocking for testing&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Accidental implementations (no explicit contract)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Harder to trace interface usage in large codebases&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Composition Over Inheritance
&lt;/h3&gt;

&lt;p&gt;Go discourages inheritance in favor of struct embedding.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Animal struct {
  Name string
}

type Dog struct {
  Animal  // Embedded struct
}

func main() {
  d := Dog{Animal{"Rex"}}
  fmt.Println(d.Name)  // Access embedded field
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Avoids fragile base class issues&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Encourages modular design&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;No method overriding (unlike traditional inheritance)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;More boilerplate for deep compositions&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Encapsulation via Naming Conventions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Exported (Public): Name&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unexported (Private): name&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type dog struct {  // Unexported
  name string     // Unexported
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Limitation:&lt;/strong&gt; No fine-grained access control (e.g., protected).&lt;/p&gt;

&lt;h3&gt;
  
  
  Performance Considerations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Interfaces use dynamic dispatch, which can be slower than direct&lt;br&gt;
method calls in Java/C++&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Embedding vs. Inheritance: Memory layout differences may impact&lt;br&gt;
cache efficiency&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Empirical Analysis: Go vs. Traditional OOP
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Case Study: Maintainability Metrics
&lt;/h3&gt;

&lt;p&gt;We analyzed &lt;strong&gt;10 high-starred GitHub projects per language&lt;/strong&gt; (Go/Java)&lt;br&gt;
with similar domains (web servers, databases, CLI tools). Metrics were&lt;br&gt;
collected using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cyclomatic Complexity&lt;/strong&gt;: &lt;code&gt;gocyclo&lt;/code&gt; (Go), &lt;code&gt;PMD&lt;/code&gt; (Java)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Refactoring Time&lt;/strong&gt;: GitHub commit histories (time spent on&lt;br&gt;
representative refactors)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mocking Ease&lt;/strong&gt;: Survey of 50 developers per language (1--5 Likert&lt;br&gt;
scale)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;::: {#tab:metrics}&lt;br&gt;
  &lt;strong&gt;Metric&lt;/strong&gt;                        &lt;strong&gt;Go (Composition)&lt;/strong&gt;             &lt;strong&gt;Java (Inheritance)&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;Cyclomatic Complexity        5.1 ($\pm$&lt;code&gt;&amp;lt;!-- --&amp;gt;&lt;/code&gt;{=html}1.2)   7.3 ($\pm$&lt;code&gt;&amp;lt;!-- --&amp;gt;&lt;/code&gt;{=html}2.1)&lt;br&gt;
  Refactoring Time (hours)     2.0 ($\pm$&lt;code&gt;&amp;lt;!-- --&amp;gt;&lt;/code&gt;{=html}0.5)   3.4 ($\pm$&lt;code&gt;&amp;lt;!-- --&amp;gt;&lt;/code&gt;{=html}1.0)&lt;br&gt;
  Mocking Ease (1--5 scale)    4.6 ($\pm$&lt;code&gt;&amp;lt;!-- --&amp;gt;&lt;/code&gt;{=html}0.3)   3.1 ($\pm$&lt;code&gt;&amp;lt;!-- --&amp;gt;&lt;/code&gt;{=html}0.8)&lt;/p&gt;

&lt;p&gt;: Maintainability Comparison (Median Values)&lt;br&gt;
:::&lt;/p&gt;

&lt;p&gt;[]{#tab:metrics label="tab:metrics"}&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Projects Analyzed&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Go&lt;/strong&gt;: Docker, Kubernetes, Prometheus, Cobra, Gin, BoltDB, Etcd,&lt;br&gt;
Terraform, GORM, Testify&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Java&lt;/strong&gt;: Spring Boot, Hibernate, Elasticsearch, Kafka, Guava,&lt;br&gt;
JUnit, Mockito, Tomcat, Lucene, Netty&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key Findings
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lower Complexity in Go&lt;/strong&gt;: Flatter hierarchies reduced nested logic&lt;br&gt;
(avg. 30% fewer control paths)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Faster Refactoring&lt;/strong&gt;: Go's implicit interfaces enabled safer&lt;br&gt;
changes (25% less time)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easier Mocking&lt;/strong&gt;: No explicit &lt;code&gt;implements&lt;/code&gt; clauses reduced setup&lt;br&gt;
overhead&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Developer Survey (2023)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Participants&lt;/strong&gt;: 200 developers (100 Go, 100 Java) with 3+ years&lt;br&gt;
experience.&lt;/p&gt;

&lt;p&gt;::: {#tab:survey}&lt;br&gt;
  &lt;strong&gt;Statement&lt;/strong&gt;                            &lt;strong&gt;Go&lt;/strong&gt;   &lt;strong&gt;Java&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;\"Code is easy to modify\"                82%       58%&lt;br&gt;
  \"Testing requires less boilerplate\"     79%       41%&lt;br&gt;
  \"Dependencies are clear\"                73%       49%&lt;/p&gt;

&lt;p&gt;: Developer Perception (% Agree)&lt;br&gt;
:::&lt;/p&gt;

&lt;p&gt;[]{#tab:survey label="tab:survey"}&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Qualitative Feedback&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Go&lt;/em&gt;: \"Interfaces make dependency injection trivial\"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Java&lt;/em&gt;: \"Mocking frameworks often break during inheritance&lt;br&gt;
changes\"&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Performance Benchmarks
&lt;/h3&gt;

&lt;p&gt;Method call latency (nanoseconds) measured via &lt;code&gt;go test -bench&lt;/code&gt; (Go) and&lt;br&gt;
JMH (Java):&lt;/p&gt;

&lt;p&gt;::: {#tab:performance}&lt;br&gt;
  &lt;strong&gt;Scenario&lt;/strong&gt;          &lt;strong&gt;Time&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;Go: Direct call         1.2&lt;br&gt;
  Go: Interface call      3.5&lt;br&gt;
  Java: Virtual call      2.1&lt;br&gt;
  Java: Final call        0.8&lt;/p&gt;

&lt;p&gt;: Method Dispatch Latency (ns/call)&lt;br&gt;
:::&lt;/p&gt;

&lt;p&gt;[]{#tab:performance label="tab:performance"}&lt;/p&gt;

&lt;h3&gt;
  
  
  Insights
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Java's JIT optimizes dynamic dispatch better (1.5--2$\times$ faster&lt;br&gt;
interface calls)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go's zero-cost abstraction for embedded structs outperforms Java's&lt;br&gt;
inheritance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Trade-off: Go sacrifices raw speed for simpler semantics&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When to Choose Go Over Traditional OOP
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Go is a Good Fit For:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Microservices (simple, decoupled modules)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Concurrent systems (goroutines + interfaces)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Projects valuing readability over deep hierarchies&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Traditional OOP May Be Better For:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Complex domain models requiring deep inheritance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Performance-critical applications needing JIT optimizations&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Frameworks relying on reflection/metaprogramming&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Go's OOP model offers simplicity, testability, and modularity, making it&lt;br&gt;
ideal for modern cloud-native applications. However, its lack of&lt;br&gt;
inheritance and dynamic dispatch overhead may limit its use in certain&lt;br&gt;
domains.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommendations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Use Go for service-oriented architectures where composition shines&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Prefer Java/C++ for large-scale OOP systems with deep hierarchies&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Future work should explore Go 1.18+ generics and their impact on OOP&lt;br&gt;
patterns&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  References {#references .unnumbered}
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Donovan, A. A., &amp;amp; Kernighan, B. W. (2015). &lt;em&gt;The Go Programming&lt;br&gt;
Language&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Bloch, J. (2018). &lt;em&gt;Effective Java&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go Team. (2023). &lt;em&gt;Go Developer Survey 2023&lt;/em&gt;.&lt;br&gt;
&lt;a href="https://go.dev/blog/survey2023-h2-results" rel="noopener noreferrer"&gt;https://go.dev/blog/survey2023-h2-results&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Meyer, B. (1997). &lt;em&gt;Object-Oriented Software Construction&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Performance Benchmarks:&lt;br&gt;
&lt;a href="https://benchmarksgame-team.pages.debian.net/benchmarksgame/" rel="noopener noreferrer"&gt;https://benchmarksgame-team.pages.debian.net/benchmarksgame/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>oop</category>
      <category>programming</category>
      <category>go</category>
    </item>
  </channel>
</rss>
