<?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: yash agarwal</title>
    <description>The latest articles on DEV Community by yash agarwal (@yash_agarwal_4faf140fcd68).</description>
    <link>https://dev.to/yash_agarwal_4faf140fcd68</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%2F3571587%2Fc1b74c50-f239-4b13-9461-2b70336e1a8d.png</url>
      <title>DEV Community: yash agarwal</title>
      <link>https://dev.to/yash_agarwal_4faf140fcd68</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yash_agarwal_4faf140fcd68"/>
    <language>en</language>
    <item>
      <title>🧠 What Is Immutability?</title>
      <dc:creator>yash agarwal</dc:creator>
      <pubDate>Fri, 17 Oct 2025 18:19:13 +0000</pubDate>
      <link>https://dev.to/yash_agarwal_4faf140fcd68/what-is-immutability-3ehh</link>
      <guid>https://dev.to/yash_agarwal_4faf140fcd68/what-is-immutability-3ehh</guid>
      <description>&lt;p&gt;An immutable object is one whose state cannot change after creation.&lt;br&gt;
Once you create it, that’s it — no more modifications.&lt;/p&gt;

&lt;p&gt;For example, Java’s String class is immutable:&lt;/p&gt;

&lt;p&gt;String name = "Yash";&lt;br&gt;
name.concat(" Agarwal");&lt;br&gt;
System.out.println(name); // Still prints "Yash"&lt;/p&gt;

&lt;p&gt;concat() creates a new String instead of modifying the original.&lt;/p&gt;

&lt;p&gt;⚙ Why Immutability Matters&lt;/p&gt;

&lt;p&gt;Here’s why immutable objects are a developer’s best friend:&lt;/p&gt;

&lt;p&gt;✅ Thread-Safety – Immutable objects can be shared across threads safely without synchronization.&lt;/p&gt;

&lt;p&gt;🧩 Predictable Behavior - Their state never changes, so debugging is easier.&lt;/p&gt;

&lt;p&gt;🧠 Simpler Design – You don’t have to worry about side effects.&lt;/p&gt;

&lt;p&gt;💡 Cache Friendly – Immutable objects can be cached or reused without fear of modification.&lt;/p&gt;

&lt;p&gt;🧱 How to Make a Class Immutable&lt;/p&gt;

&lt;p&gt;To make your own class immutable, follow these 5 golden rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Mark the class as final&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So no one can subclass and modify its behavior.&lt;/p&gt;

&lt;p&gt;public final class Employee {&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make all fields private and final&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This ensures they can’t be reassigned after construction.&lt;/p&gt;

&lt;p&gt;private final String name;&lt;br&gt;
private final int id;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initialize fields through the constructor only&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All values should be set once, during object creation.&lt;/p&gt;

&lt;p&gt;public Employee(String name, int id) {&lt;br&gt;
    this.name = name;&lt;br&gt;
    this.id = id;&lt;br&gt;
}&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;No setters!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Avoid any method that can modify the internal state.&lt;/p&gt;

&lt;p&gt;❌ Bad:&lt;/p&gt;

&lt;p&gt;public void setName(String name) { this.name = name; }&lt;/p&gt;

&lt;p&gt;✅ Good:&lt;/p&gt;

&lt;p&gt;public String getName() { return name; }&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Return deep copies of mutable objects&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If your class holds references to mutable objects, protect them.&lt;/p&gt;

&lt;p&gt;private final Date joiningDate;&lt;/p&gt;

&lt;p&gt;public Date getJoiningDate() {&lt;br&gt;
    return new Date(joiningDate.getTime()); // Defensive copy&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;This ensures outside code can’t modify your object indirectly.&lt;/p&gt;

&lt;p&gt;🚀 Example: Immutable Class in Action&lt;br&gt;
public final class Employee {&lt;br&gt;
    private final String name;&lt;br&gt;
    private final int id;&lt;br&gt;
    private final Date joiningDate;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public Employee(String name, int id, Date joiningDate) {
    this.name = name;
    this.id = id;
    this.joiningDate = new Date(joiningDate.getTime());
}

public String getName() { return name; }
public int getId() { return id; }

public Date getJoiningDate() {
    return new Date(joiningDate.getTime());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;✅ Thread-safe&lt;br&gt;
✅ No side effects&lt;br&gt;
✅ Clean and predictable&lt;/p&gt;

&lt;p&gt;🧩 Bonus: Records (Java 14+)&lt;/p&gt;

&lt;p&gt;From Java 14 onward, records make creating immutable data classes effortless.&lt;/p&gt;

&lt;p&gt;public record Employee(String name, int id, LocalDate joiningDate) {}&lt;/p&gt;

&lt;p&gt;Records automatically:&lt;/p&gt;

&lt;p&gt;Make fields final&lt;/p&gt;

&lt;p&gt;Generate constructors &amp;amp; accessors&lt;/p&gt;

&lt;p&gt;Prevent setters&lt;/p&gt;

&lt;p&gt;This is the modern Java way to achieve immutability with minimal boilerplate.&lt;/p&gt;

&lt;p&gt;🧭 Conclusion&lt;/p&gt;

&lt;p&gt;Immutability isn’t just a design principle — it’s a mindset.&lt;br&gt;
When you stop mutating state and start creating predictable, unchangeable objects, your code becomes:&lt;/p&gt;

&lt;p&gt;Easier to reason about&lt;/p&gt;

&lt;p&gt;Safer in concurrent environments&lt;/p&gt;

&lt;p&gt;More reliable and bug-resistant&lt;/p&gt;

&lt;p&gt;“Immutable code is clean code. And clean code scales.”&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>java</category>
    </item>
    <item>
      <title>⚙ Handling Nulls the Smart Way in Java</title>
      <dc:creator>yash agarwal</dc:creator>
      <pubDate>Fri, 17 Oct 2025 18:14:38 +0000</pubDate>
      <link>https://dev.to/yash_agarwal_4faf140fcd68/handling-nulls-the-smart-way-in-java-4ehm</link>
      <guid>https://dev.to/yash_agarwal_4faf140fcd68/handling-nulls-the-smart-way-in-java-4ehm</guid>
      <description>&lt;p&gt;If you’ve written Java for a while, you’ve definitely seen it:&lt;/p&gt;

&lt;p&gt;💥 NullPointerException: Cannot invoke method because "object" is null&lt;/p&gt;

&lt;p&gt;It’s one of the most common and most annoying runtime errors in Java.&lt;br&gt;
But the truth is, nulls themselves aren’t evil; the way we handle them usually is.&lt;/p&gt;

&lt;p&gt;Let’s look at a few smart, modern ways to deal with nulls gracefully in Java 👇&lt;/p&gt;

&lt;p&gt;🧩 1. Understand Why Nulls Exist&lt;/p&gt;

&lt;p&gt;Java’s creator, Tony Hoare, once called null his “billion-dollar mistake.”&lt;br&gt;
But nulls exist for a reason — to represent the absence of value.&lt;/p&gt;

&lt;p&gt;The problem is when we forget to handle that absence and assume data is always there.&lt;br&gt;
The goal isn’t to eliminate null it’s to control it safely.&lt;/p&gt;

&lt;p&gt;✅ 2. Use Objects.requireNonNull() Early&lt;/p&gt;

&lt;p&gt;If a method should never accept nulls, fail fast and loud.&lt;/p&gt;

&lt;p&gt;public void sendEmail(String email) {&lt;br&gt;
    this.email = Objects.requireNonNull(email, "Email cannot be null");&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;This instantly catches the problem where it happens instead of breaking your app 50 lines later.&lt;/p&gt;

&lt;p&gt;🧠 3. Use Optional (Smartly)&lt;/p&gt;

&lt;p&gt;Java 8 introduced Optional to help represent “maybe present, maybe not” values safely.&lt;/p&gt;

&lt;p&gt;✅ Good usage:&lt;/p&gt;

&lt;p&gt;Optional userOpt = userRepository.findById(id);&lt;br&gt;
userOpt.ifPresent(user -&amp;gt; sendEmail(user.getEmail()));&lt;/p&gt;

&lt;p&gt;❌ Bad usage:&lt;/p&gt;

&lt;p&gt;Optional user = Optional.ofNullable(null);&lt;/p&gt;

&lt;p&gt;👉 Tip: Don’t use Optional for fields or parameters only for method return types.&lt;/p&gt;

&lt;p&gt;🔍 4. Chain Safely with map() and flatMap()&lt;/p&gt;

&lt;p&gt;Instead of writing multiple null checks, you can chain operations elegantly:&lt;/p&gt;

&lt;p&gt;userRepository.findById(id)&lt;br&gt;
    .map(User::getAddress)&lt;br&gt;
    .map(Address::getCity)&lt;br&gt;
    .ifPresent(System.out::println);&lt;/p&gt;

&lt;p&gt;This avoids deep nesting like:&lt;/p&gt;

&lt;p&gt;if (user != null &amp;amp;&amp;amp; user.getAddress() != null &amp;amp;&amp;amp; user.getAddress().getCity() != null) ...&lt;/p&gt;

&lt;p&gt;🧰 5. Use Default Values with orElse() and orElseGet()&lt;/p&gt;

&lt;p&gt;When something’s missing, define a fallback value instead of breaking your flow.&lt;/p&gt;

&lt;p&gt;User user = userRepository.findById(id)&lt;br&gt;
                .orElseGet(() -&amp;gt; new User("Guest"));&lt;/p&gt;

&lt;p&gt;🧩 Difference:&lt;/p&gt;

&lt;p&gt;orElse() → always executes the fallback&lt;/p&gt;

&lt;p&gt;orElseGet() → executes it only if needed&lt;/p&gt;

&lt;p&gt;🔒 6. Leverage @NonNull and @Nullable Annotations&lt;/p&gt;

&lt;p&gt;Modern IDEs (like IntelliJ) can warn you about possible nulls if you annotate your code properly.&lt;/p&gt;

&lt;p&gt;public void processOrder(@NonNull Order order) { ... }&lt;/p&gt;

&lt;p&gt;Helps catch issues before you run the code.&lt;/p&gt;

&lt;p&gt;🧹 7. Defensive Coding Mindset&lt;/p&gt;

&lt;p&gt;When designing APIs or writing methods:&lt;/p&gt;

&lt;p&gt;Be explicit about what can be null.&lt;/p&gt;

&lt;p&gt;Validate inputs early.&lt;/p&gt;

&lt;p&gt;Return empty lists instead of nulls when possible.&lt;/p&gt;

&lt;p&gt;✅ Good practice:&lt;/p&gt;

&lt;p&gt;return users != null ? users: Collections.emptyList();&lt;/p&gt;

&lt;p&gt;🧭 Conclusion&lt;/p&gt;

&lt;p&gt;Handling nulls smartly is all about predictability and intent.&lt;br&gt;
Your code should never leave others guessing —“Can this be null or not?”&lt;/p&gt;

&lt;p&gt;When you start using Optional, annotations, and defensive checks wisely,&lt;br&gt;
You’ll find your code is cleaner, safer, and way easier to maintain.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>code</category>
    </item>
    <item>
      <title>How to Write Clean, Readable, and Maintainable Java Code</title>
      <dc:creator>yash agarwal</dc:creator>
      <pubDate>Fri, 17 Oct 2025 18:11:59 +0000</pubDate>
      <link>https://dev.to/yash_agarwal_4faf140fcd68/how-to-write-clean-readable-and-maintainable-java-code-3klg</link>
      <guid>https://dev.to/yash_agarwal_4faf140fcd68/how-to-write-clean-readable-and-maintainable-java-code-3klg</guid>
      <description>&lt;p&gt;As Java developers, we often focus on getting things to work. But over time, we realize that getting it to work and making it maintainable are two very different goals.&lt;br&gt;
Clean code is not just about syntax, it’s about writing software that is easy to read, easy to change, and easy to trust.&lt;/p&gt;

&lt;p&gt;Here’s how you can start writing cleaner, more maintainable Java code that even your future self will thank you for.&lt;/p&gt;

&lt;p&gt;🧠 1. Follow Consistent Naming Conventions&lt;/p&gt;

&lt;p&gt;Good naming makes your code self-explanatory.&lt;br&gt;
Avoid using short or vague names like 'tmp', 'data', or 'val'. Instead, use meaningful and descriptive names that accurately reflect your intent.&lt;/p&gt;

&lt;p&gt;✅ Good Example:&lt;/p&gt;

&lt;p&gt;int retryCount = 3;&lt;br&gt;
String customerEmail = "&lt;a href="mailto:user@example.com"&gt;user@example.com&lt;/a&gt;";&lt;/p&gt;

&lt;p&gt;❌ Bad Example:&lt;/p&gt;

&lt;p&gt;int r = 3;&lt;br&gt;
String e = "&lt;a href="mailto:user@example.com"&gt;user@example.com&lt;/a&gt;";&lt;/p&gt;

&lt;p&gt;A rule of thumb: if someone can guess what a variable does without reading comments, you’ve done well.&lt;/p&gt;

&lt;p&gt;🧩 2. Keep Methods Small and Focused&lt;/p&gt;

&lt;p&gt;A method should do one thing and do it well.&lt;br&gt;
If your method exceeds 20–30 lines, chances are it’s handling multiple responsibilities.&lt;/p&gt;

&lt;p&gt;✅ Better:&lt;/p&gt;

&lt;p&gt;void sendEmailNotification(User user) { ... }&lt;br&gt;
void logEmailStatus(User user) { ... }&lt;/p&gt;

&lt;p&gt;❌ Worse:&lt;/p&gt;

&lt;p&gt;void processUserEmail(User user) { &lt;br&gt;
    // send email + log status + update DB + error handling&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Breaking down logic into smaller methods improves reusability and testability.&lt;/p&gt;

&lt;p&gt;📦 3. Use Meaningful Comments (or None)&lt;/p&gt;

&lt;p&gt;Comments should explain why, not what.&lt;br&gt;
If your code is self-explanatory, extra comments often add noise.&lt;/p&gt;

&lt;p&gt;✅ Good Comment:&lt;/p&gt;

&lt;p&gt;// Retry three times because the email service is unstable&lt;br&gt;
for (int i = 0; i &amp;lt; 3; i++) { ... }&lt;/p&gt;

&lt;p&gt;❌ Bad Comment:&lt;/p&gt;

&lt;p&gt;// increment i&lt;br&gt;
i++;&lt;/p&gt;

&lt;p&gt;If you need too many comments, consider refactoring instead.&lt;/p&gt;

&lt;p&gt;🧰 4. Stick to SOLID Principles&lt;/p&gt;

&lt;p&gt;These five principles are the backbone of maintainable object-oriented design:&lt;/p&gt;

&lt;p&gt;Single Responsibility&lt;/p&gt;

&lt;p&gt;Open/Closed&lt;/p&gt;

&lt;p&gt;Liskov Substitution&lt;/p&gt;

&lt;p&gt;Interface Segregation&lt;/p&gt;

&lt;p&gt;Dependency Inversion&lt;/p&gt;

&lt;p&gt;For example, instead of one giant UserService doing everything, split it into smaller services — each handling a single concern.&lt;/p&gt;

&lt;p&gt;⚙ 5. Use Proper Exception Handling&lt;/p&gt;

&lt;p&gt;Avoid blanket exception catches like catch (Exception e) unless necessary.&lt;br&gt;
Be specific and log meaningful messages.&lt;/p&gt;

&lt;p&gt;✅ Good:&lt;/p&gt;

&lt;p&gt;try {&lt;br&gt;
    userRepository.save(user);&lt;br&gt;
} catch (DataIntegrityViolationException e) {&lt;br&gt;
    log.error("Duplicate email: {}", user.getEmail());&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;❌ Bad:&lt;/p&gt;

&lt;p&gt;try {&lt;br&gt;
    // risky code&lt;br&gt;
} catch (Exception e) {&lt;br&gt;
    e.printStackTrace();&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Specific exceptions make debugging faster and logs more useful.&lt;/p&gt;

&lt;p&gt;🧩 6. Format and Organize Your Code&lt;/p&gt;

&lt;p&gt;Use consistent indentation, spacing, and line breaks.&lt;br&gt;
Follow a Java style guide (like Google Java Style or your company’s standards).&lt;br&gt;
Also, leverage your IDE’s built-in formatter (Ctrl + Alt + L in IntelliJ).&lt;/p&gt;

&lt;p&gt;Clean formatting doesn’t affect performance — but it massively affects readability.&lt;/p&gt;

&lt;p&gt;🚦 7. Write Unit Tests&lt;/p&gt;

&lt;p&gt;Well-tested code tends to be cleaner because tests force you to think modularly.&lt;br&gt;
Use JUnit and Mockito to test small units of logic.&lt;br&gt;
If your class is hard to test, it’s often a sign that it needs refactoring.&lt;/p&gt;

&lt;p&gt;🧭 8. Refactor Regularly&lt;/p&gt;

&lt;p&gt;Refactoring is not rewriting — it’s improving structure without changing behavior.&lt;br&gt;
Small, regular refactors prevent big technical debt later.&lt;/p&gt;

&lt;p&gt;Even five minutes a day of refactoring improves the overall health of your codebase.&lt;/p&gt;

&lt;p&gt;✨ Conclusion&lt;/p&gt;

&lt;p&gt;Clean code is not about perfection — it’s about clarity, simplicity, and empathy for other developers.&lt;br&gt;
When your code reads like a story, you build trust with your team and with your future self.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>java</category>
    </item>
  </channel>
</rss>
