Wednesday, April 23, 2025

Centralized Exception Handling with Unique IDs in Java.

 

When working with a large codebase or complex applications, tracking and managing exceptions efficiently is crucial. Here's how you can centralize exception handling while adding unique IDs to each exception for better traceability and debugging.

What is it?

Using a SafeRunner utility, we can wrap risky operations in a centralized exception handler, attaching a unique ID to each exception. This makes it easy to trace and manage exceptions across your application.

How it Works:

  • SafeRunner.run(): Wraps a task in a Callable, catches exceptions, and passes them to a centralized handler.

  • Exception IDs: Each exception is tagged with an ID to help track the specific context of errors.

  • Custom IdentifiedException: This custom exception class stores the exception ID for logging or reporting.

Code Example:

import java.util.concurrent.Callable;

// SafeRunner with exception ID
public class SafeRunner {
    public static <T> T run(Callable<T> task, String exceptionId) {
        try {
            return task.call();
        } catch (Exception ex) {
            handleGlobally(ex, exceptionId);
            return null;
        }
    }

    private static void handleGlobally(Exception ex, String exceptionId) {
        ex = new IdentifiedException(exceptionId, ex.getMessage(), ex);
        System.err.printf("Exception ID: %s\n", exceptionId);
        System.err.println("Global handler caught: " + ex);
        ex.printStackTrace(System.err);
    }
}

// Custom exception class with ID
class IdentifiedException extends RuntimeException {
    private final String id;

    public IdentifiedException(String id, String message, Throwable cause) {
        super(message, cause);
        this.id = id;
    }

    public String getId() {
        return id;
    }
}

// Example usage
public class App {
    public static void main(String[] args) {
        Integer result = SafeRunner.run(() -> divide(10, 0), "DIV-1234");
        SafeRunner.run(() -> { doSomethingRisky(); return null; }, "TASK-5678");
    }

    static int divide(int a, int b) {
        return a / b;  // Will throw ArithmeticException if b == 0
    }

    static void doSomethingRisky() throws Exception {
        throw new Exception("Oh no!");
    }
}

Benefits:

  • Traceability: Easily track which operation caused the exception.

  • Centralized Handling: Consolidates error handling logic in one place.

  • Better Debugging: Use exception IDs to correlate logs and errors across modules.

Use Cases:

  • Large Systems: Track errors across services and components.

  • Distributed Applications: Easily propagate error IDs across multiple services.

  • Debugging: Quickly trace specific issues through logs and support tickets.


This approach helps streamline exception management, providing a clear way to track and resolve issues quickly while keeping your codebase clean and maintainable.
-
git : https://github.com/stark9000/Centralized-Exception-Handling


Saturday, January 18, 2025

Code Smells තේරුම් ගැනීම සහ ඒවා වළක්වා ගන්නේ කෙසේද?

 

Code Smells තේරුම් ගැනීම සහ ඒවා වළක්වා ගන්නේ කෙසේද?
-
Code smells
යනු ඔබේ කේතයේ ගැඹුරු ගැටළු පිළිබඳ දර්ශක වේ. දෝෂ අවශ්‍යයෙන්ම නොවුනත්, ඒවා නඩත්තු දුෂ්කරතා, කියවීමේ හැකියාව අඩු වීම සහ තාක්ෂණික ණය වැඩි වීමට හේතු විය හැකි දුර්වල නිර්මාණ තේරීම් යෝජනා කරයි. කේත සුවඳ කලින් හඳුනා ගැනීම සහ ආමන්ත්‍රණය කිරීම කේත ගුණාත්මකභාවය වැඩි දියුණු කරන අතර දිගුකාලීන ව්‍යාපෘති සාර්ථකත්වය සහතික කරයි.

-

මෙම ලිපිය Code smells යනු කුමක්දැයි පැහැදිලි කරයි, උදාහරණ සපයයි, සහ සාමාන්‍ය ක්‍රමලේඛනයේදී සහ Arduino සංවර්ධනයේදී ඒවා වළක්වා ගැනීමට උපදෙස් ලබා දෙයි.
-
Code smells
යනු කුමක්ද?

Code smells යනු ඔබේ කේතය සැලසුම් කිරීමේදී හෝ ක්‍රියාත්මක කිරීමේදී යම් වැරැද්දක් සිදුවිය හැකි බවට ලකුණකි. මෙම ගැටළු කේතය තේරුම් ගැනීමට, දිගු කිරීමට හෝ දෝෂහරණය කිරීමට අපහසු කරයි. දුර්වල සැලසුම් කිරීම, කඩිනම් සංවර්ධනය හෝ කේතනය කිරීමේ හොඳම පිළිවෙත් පිළිපැදීම නොමැතිකම පොදු හේතු අතර වේ.
-
පොදු Code smells සහ උදාහරණ

1. දිගු ශ්‍රිත

Smell: අධික ලෙස දිගු ශ්‍රිත තේරුම් ගැනීමට සහ දෝෂහරණය කිරීමට අපහසු වේ.

උදාහරණය:
void handleSensorAndDisplayData() {

  // Read sensor

  // Process data

  // Update display

  // Handle errors

  // Log data

  // Send over network

}
-
විසඳුම: ශ්‍රිතය කුඩා, නාභිගත ශ්‍රිතවලට කඩා දමන්න.
-
void handleSensorAndDisplayData() {

  readSensor();

  processData();

  updateDisplay();

}
-
2.
අනුපිටපත් කේතය

Smell: එකම කේතය ස්ථාන කිහිපයක පිටපත් කර ඇලවීම නඩත්තු බර වැඩි කරයි.

උදාහරණය:
if (sensorValue > 100) {

  digitalWrite(ledPin, HIGH);

} else {

  digitalWrite(ledPin, LOW);

}

 

if (buttonState == HIGH) {

  digitalWrite(ledPin, HIGH);

} else {

  digitalWrite(ledPin, LOW);

}
-
3.
අදහස් ඕනෑවට වඩා

Smell: අදහස් ඕනෑවට වඩා අවශ්‍ය කරන කේතය ස්වයං පැහැදිලි කිරීමක් නොවිය හැකිය.

උදාහරණය:
// Turn on the LED

digitalWrite(ledPin, HIGH); 

// Wait for 1000ms

delay(1000); 

// Turn off the LED

digitalWrite(ledPin, LOW);
-
විසඳුම: අදහස් දැක්වීමේ අවශ්‍යතාවය අඩු කිරීම සඳහා පැහැදිලි, අර්ථවත් විචල්‍ය සහ ශ්‍රිත නම් භාවිතා කරන්න.
-
turnOnLED();

delay(1000);

turnOffLED();
-
4. දෘඪ-කේතගත අගයන්

Smell: කේතය තුළ කෙලින්ම මැජික් අංක හෝ නූල් භාවිතා කිරීම එය අඩු නම්‍යශීලී කරයි.

උදාහරණය:
if (temperature > 37.5) { 

  alert(); 

}
-
විසඳුම: පැහැදිලි බව සහ නඩත්තු කිරීමේ හැකියාව සඳහා නියතයන් හෝ විචල්‍යයන් භාවිතා කරන්න.
-
const float feverThreshold = 37.5; 

if (temperature > feverThreshold) { 

  alert(); 

}
-
5. විශාල පන්ති(Classes)

Smell: ඕනෑවට වඩා වගකීම් ඇති පන්ති තනි වගකීම් මූලධර්මය (SRP) උල්ලංඝනය කරයි.

උදාහරණය:
class SmartHome {

  void controlLights();

  void controlThermostat();

  void monitorSecurity();

  void manageSchedules();

}
-
විසඳුම: කුඩා, අරමුණු-නිශ්චිත පන්තිවලට බෙදන්න.
class LightController {};

class ThermostatController {};

class SecurityMonitor {};

class ScheduleManager {};
-
6. අධික If-Else දාම

Smell: if-else හෝ switch ප්‍රකාශනවල දිගු දාමයන් කේතය අනුගමනය කිරීම දුෂ්කර කරයි.

උදාහරණය:
if (command == "start") startMotor();

else if (command == "stop") stopMotor();

else if (command == "pause") pauseMotor();

else if (command == "resume") resumeMotor();
-
විසඳුම: ශ්‍රිත දර්ශක හෝ ශබ්දකෝෂයක් වැනි සිතියම්කරණ ප්‍රවේශයක් භාවිතා කරන්න.
-
typedef void (*CommandFunc)();

std::map<String, CommandFunc> commands = {

  {"start", startMotor},

  {"stop", stopMotor},

  {"pause", pauseMotor},

  {"resume", resumeMotor}

};

commands[command]();
-
7.
අධික සංකීර්ණ තර්කනය

Smell: සංකීර්ණ, කැදැලි තර්කනය කියවීමට සහ දෝෂහරණය කිරීමට අපහසු විය හැකිය.

උදාහරණය:
if ((temp > 30 && humidity < 40) || (rainDetected && windSpeed > 20)) {

  takeAction();

}
-
විසඳුම: කොන්දේසි සරල කිරීම සඳහා උපකාරක ශ්‍රිත භාවිතා කරන්න.
-
if (isHotAndDry() || isStormy()) {

  takeAction();

}
-
Code Smells වළක්වා ගන්නේ කෙසේද?

ඔබේ කේතය සැලසුම් කරන්න: ලිවීමට පෙර නිර්මාණය කිරීමට කාලය ගත කරන්න. විශාල කාර්යයන් කුඩා, කළමනාකරණය කළ හැකි සංරචක වලට බෙදන්න.

හොඳම පිළිවෙත් අනුගමනය කරන්න: DRY (ඔබම නැවත නොකරන්න) සහ SRP (තනි වගකීම් මූලධර්මය) වැනි මූලධර්මවලට ඇලී සිටින්න.

නිතිපතා ප්‍රතිනිර්මාණය කරන්න: තර්කනය සරල කිරීමෙන් සහ අතිරික්ත කොටස් ඉවත් කිරීමෙන් කේතය අඛණ්ඩව වැඩි දියුණු කරන්න.

ස්වයං පැහැදිලි කිරීමේ කේතය ලියන්න: කේතය බුද්ධිමය කිරීමට විස්තරාත්මක විචල්‍ය සහ ශ්‍රිත නම් භාවිතා කරන්න.

උත්තෝලන මෙවලම්: code smell ස්වයංක්‍රීයව හඳුනා ගැනීමට Lint සහ ස්ථිතික විශ්ලේෂණ මෙවලම් භාවිතා කරන්න.
-
නිගමනය

පිරිසිදු, නඩත්තු කළ හැකි සහ පරිමාණය කළ හැකි කේතයක් ලිවීම සඳහා code smells හඳුනා ගැනීම සහ ආමන්ත්‍රණය කිරීම ඉතා වැදගත් වේ. ඔබේ කේතය නිතිපතා සමාලෝචනය කිරීමෙන්, නැවත සකස් කිරීමෙන් සහ කේතීකරණ මූලධර්මවලට අනුගත වීමෙන්, ඔබට ගැටළු වළක්වා ගත හැකි අතර ඔබේ ව්‍යාපෘති ශක්තිමත් සහ අනාගතයට ඔරොත්තු දෙන බව සහතික කළ හැකිය.
-
තාමත් අපේ group එකේ නැත්තන් group එකට සෙට් වෙන්න :

https://www.facebook.com/groups/paperclipx

මේ group එකේ දාන දේවල් හොඳයි කියල හිතෙනවනම් ඕගොල්ලොන් ගේ යාලුවන්වත් group එකට එකතු කරන්න !

Comprehensive R&D Workflow for Arduino and Electronics Projects

 

Comprehensive R&D Workflow for Arduino and Electronics Projects

When embarking on an electronics project, having a clear and systematic Research and Development (R&D) workflow is essential. This guide outlines a step-by-step process to help hobbyists, engineers, and makers turn ideas into reliable, working products.

1. Concept Development

  • Begin with thorough research. Gather relevant information about the project, including theoretical principles and mathematical models.
  • Visualize how the concept can be implemented in the real world, ensuring practicality and feasibility.

2. Circuit Design

  • Develop an initial schematic and circuit diagram. This provides a blueprint for the project and ensures all components are correctly interconnected.
  • Use software like KiCad, EasyEDA, or Fritzing for professional-grade schematics.

3. Component Sourcing

  • Identify all components required for the project.
  • Source the components from reliable suppliers, keeping in mind the project budget and quality standards.

4. Prototyping

  • Build a prototype using a breadboard or dot board to validate the initial design.
  • Ensure all connections are secure and components are functioning as expected.

5. Firmware Development

  • Write and upload the firmware for your microcontroller or hardware.
  • Ensure the code is modular and well-documented for ease of debugging and updates.

6. Testing and Debugging

  • Test the prototype under various conditions to identify potential issues.
  • Debug the circuit and firmware iteratively, refining the schematic and software until the design is stable and reliable.

7. Finalizing the Design

  • Once the prototype performs consistently, finalize the schematic and firmware.
  • Verify the design through simulations and thorough testing to ensure no oversights.

8. DIY PCB Development

  • Design a PCB using the finalized schematic and software like KiCad or Eagle.
  • Create the PCB using DIY methods such as toner transfer, UV exposure, or CNC engraving.
  • Assemble and test the DIY PCB with the firmware to confirm functionality.

9. Professional PCB Design (Optional)

  • If a high-quality, polished PCB is required, create a professional design using the same finalized schematic.
  • Use services like JLCPCB, PCBWay, or OshPark to manufacture the PCB.
  • This step ensures the design is bug-free, reduces waste, and provides a robust final product.

10. Documentation (Highly Recommended)

  • Document every step of the process, including:Schematics and PCB layouts. Firmware code with comments. Test results and troubleshooting notes.
  • This documentation can serve as a reference for future projects and help others replicate or improve the design.

11. Simulation (Optional but Valuable)

  • Use simulation tools like LTSpice, Proteus, or Tinkercad to test the circuit virtually before building the prototype.
  • This can save significant time and resources by catching errors early in the process.

12. Version Control

  • Use version control systems like Git to manage firmware development.
  • Track changes, collaborate efficiently, and roll back to earlier versions if needed.

13. Enclosure Design (If Applicable)

  • Design an enclosure to house the final product, ensuring it is functional, durable, and aesthetically pleasing.
  • Tools like Fusion 360 or SolidWorks can help with 3D modeling and prototyping.

Key Takeaways

This structured approach minimizes trial-and-error and ensures a reliable final product. By combining technical best practices with thorough testing and documentation, you can streamline the R&D process and create high-quality electronics projects. - තාමත් අපේ group එකේ නැත්තන් group එකට සෙට් වෙන්න :⁣
https://www.facebook.com/groups/paperclipx
මේ group එකේ දාන දේවල් හොඳයි කියල හිතෙනවනම් ඕගොල්ලොන් ගේ යාලුවන්වත් group එකට එකතු කරන්න !⁣