Tip no. |
Language |
Title |
Description |
More info |
32 |
PHP |
PHP - Use strict types to avoid unexpected type coercion |
At the beginning of your PHP files, add:
(Code in the "Code example")
This enforces strict type checking, preventing PHP from automatically converting types (e.g., converting a string "5" to an integer 5). It helps catch potential bugs early and improves code reliability.
|
|
30 |
Java |
Java - Use Optional to Avoid Null Checks |
In Java, dealing with null values can often lead to bugs, especially NullPointerExceptions. Java's Optional class (introduced in Java 8) helps handle null values more elegantly and reduces the need for multiple null checks.
While Optional is powerful, avoid using it in places where it might introduce overhead, such as:
- Fields in classes (it’s generally better for return types).
- Code that requires frequent access to a variable that may be null. |
|
29 |
C# |
C# tip: use a custom using alias to disambiguate and enhance readability for complex code structures |
This is particularly useful in large projects where multiple classes might have the same name, or if you’re using multiple libraries with similar method names or concepts.
This technique can keep your code clean and enhance readability.
Let’s say you have two different libraries that both define a class named Button (maybe one is a Windows Forms Button, and the other is for a web framework): |
|
28 |
Go |
Go - Use Goroutines and Channels for Concurrent Processing |
Goroutines and channels are Go’s way of handling concurrency, making it easy to execute tasks concurrently without the complexity of thread management. When tasks can run independently (e.g., fetching data from different sources), using goroutines can improve performance and responsiveness. |
|
24 |
C# |
C# tip - Prefer var for Local Variables When the Type is Obvious |
In C#, you can declare local variables either by explicitly stating their type or using the var keyword to let the compiler infer the type. Using var can make your code more concise and easier to read, but it’s important to use it only when the type is obvious from the context.
Benefits of using var:
Improves code readability by removing redundant type declarations.
Encourages type inference, making the code less verbose. |
|
23 |
JavaScript |
JavaScript tip - use const and let instead of var |
In the past, JavaScript developers used the var keyword to declare variables. However, with the introduction of ES6, it's best practice to use const and let, which offer more predictable and safer behavior.
|
|
22 |
C++ |
Use const Correctly to Improve Code Safety and Readability |
In C++, using const can enhance the clarity and correctness of your code. It prevents accidental modification of variables and communicates the intent to others (and to yourself) that something should not change. |
|
21 |
Java |
Java programming tip: Use try-with-resources for Automatic Resource Management |
Context:
Managing resources like files, streams, or database connections can be tricky in Java, especially when exceptions occur. Traditionally, you might use a try-catch-finally block to ensure resources are closed, but this can lead to verbose and error-prone code.
Tip:
Use the try-with-resources statement to automatically close resources when they are no longer needed. This ensures that resources are closed correctly and simplifies your code. |
|
20 |
Go |
Use Go Modules |
Go modules are the standard way to manage dependencies in Go projects. Make sure to initialize your project with go mod init and use go get to add dependencies. This ensures consistent dependency management across environments.
|
|
18 |
C++ |
C++ Avoid Including Multiple Libraries |
Generally, we include libraries at the start of the C++ code to perform certain operations as shown below.
But, we have a better approach to replace these many libraries with just one library i.e, #include bits/stdc++.h> to include all standard libraries without adding them one at a time. It is especially useful in programming competitions where time is limited.
This includes all the standard libraries required in the program. So, we can avoid adding these many libraries separately to keep code as efficient and effective as possible.
|
|