Skip to main content

Posts

Showing posts with the label hints_and_hacks

Hints and Hacks: Mastering Go Project Structure and Generic Repositories with GORM

  Mastering Go Project Structure and Generic Repositories with GORM Establishing a robust project structure is paramount when building scalable and maintainable Go applications. Coupled with the flexibility of a generic repository, this approach ensures clean, reusable, and extendable code. In this article, we'll explore how to set up a well-structured Go project and implement a generic repository pattern with GORM, focusing on practical examples and best practices. Why Project Structure Matters A well-organized project structure is critical for: Scalability : Adding new features or components becomes seamless. Readability : Developers can easily navigate the codebase. Maintainability : Issues can be identified and resolved efficiently. Adhering to a modular structure, your application remains clean and manageable as it grows. Recommended Go Project Structure Here is a typical project layout for a Go application leveraging GORM and a generic repository: app_db_generic...

Hints and Hacks: Minimize using Exceptions .NET

Microsoft recommends against using exceptions for regular program flow, as they are slower than other control flow methods. Exceptions should be reserved for handling rare or unexpected situations, not the usual flow of the program. ( Microsoft Docs: Best Practices for Exceptions - .NET | Microsoft Learn ) A more efficient and elegant alternative is to use a Result object, which effectively represents the outcome of an operation. This approach communicates the result clearly without relying on exceptions, offering simplicity and clarity. Why use the Result object? Improved expressiveness:  The function signature clearly indicates possible outcomes, making the code more self-explanatory. Better performance:  This method avoids the performance overhead of throwing and catching exceptions, especially in scenarios where errors are anticipated. Consistent error handling:  Errors are turned into values that can be passed, modified, or logged easily, creating a more predictable ...

Hints and Hacks: Make your code better (Intro)!

This is my first post about Hints and Hacks . For this initial post, I’ve selected some simple and humorous tricks, which happen to be my ‘favorite’ types of code commonly seen by beginner or exhausted developers. I often find myself commenting on these during the code review process. One common situation is when you need to add a method to check if some data is valid. Here’s how it typically looks: private static bool IsValid(string name) { if (string.IsNullOrEmpty(name)) { return false; } else { return true; } } OR private static bool IsValid(string name) { return !string.IsNullOrEmpty(name) ? true : false; } Please DON’T do that. Just make your code as simple as possible and always think about the readability of your code. So, how it should be: private static bool IsValid(string name) { return !string.IsNullOrEmpty(name); } In this implementation, this method will be clean and understandable for everyone.