Programmers who fail, as well as those who succeed, face a variety of challenges that can lead to problematic code. Some of the common problems include lack of experience, poor understanding of requirements, rushing to meet deadlines, lack of proper review and testing, among others. Here are some examples of code that can result from poor practices and the reasons why they are considered problematic. ## <br>Undocumented and disorganized code (Python). Code that is difficult to read and understand can be a big problem, especially when someone else needs to maintain it. ```py def f(x): return x ** 2 def g(y): return y + 5 a = 10 b = f(a) c = g(b) print(c) ``` **Problems**: - Functions f and g have poorly descriptive names. - There are no comments or documentation explaining what the code does. - Variables a, b, c are used in a confusing and undescriptive way. ## <br>SQL Injection (PHP) One of the most common security errors is vulnerability to SQL injection. ```php <?php $username = $_POST['username']; $password = $_POST['password']; $conn = new mysqli("localhost", "user", "password", "database"); $sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'"; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "Login successful!"; } else { echo "Invalid credentials."; } $conn->close(); ?> ``` **Problems**: - Vulnerable to SQL Injection. - Passwords are not hashed, exposing sensitive information. ## <br>Repetitive code without reuse (JavaScript) Code that repeats logic unnecessarily instead of using reusable functions. ```js let user1 = {name: "Alice", age: 30}; let user2 = {name: "Bob", age: 25}; let user3 = {name: "Charlie", age: 35}; console.log(user1.name + " is " + user1.age + " years old."); console.log(user2.name + " is " + user2.age + " years old."); console.log(user3.name + " is " + user3.age + " years old."); ``` **Problems**: - Repetitive code. - Lack of reusable functions to avoid redundancy. ## <br>Poor resource utilization (C++) Resources are not released correctly, leading to memory leaks. ```cpp #include <iostream> void leakMemory() { int* arr = new int[100]; // Forgetting to free the allocated memory } int main() { leakMemory(); return 0; } ``` **Problems**: - Memory allocated with new is not released with delete. - Can cause memory leaks. ## <br>Poorly implemented conditions (Lua) Conditions that don't cover all possible cases or are difficult to understand. ```lua function checkNumber(num) if num == 1 then print("Number is one") elseif num == 2 then print("Number is two") elseif num == 3 then print("Number is three") else print("Number is something else") end end checkNumber(4) ``` **Problems**: - Difficult to scale to a large number of conditions. - Does not use mapping tables to simplify the code. # <br>How to improve: 1. Clear Documentation and Names: Use descriptive variable and function names, and add comments to explain the code. 2. Security: Use prepared statements to prevent SQL Injection, and always hash passwords before storing them. 3. Code Reuse: Avoid code duplication by using reusable functions. 4. Resource Management: Always release the resources you have allocated. 5. Case Coverage and Clarity: Use tables or mappings to simplify the logic of complex conditions. Not being a member of the community (chat-to.dev) One of the biggest mistakes many programmers make is not joining other programmers and sharing information. Interaction between people in this group makes it easier to keep up to date with new tools and content. So register on the site today.
Dont forget that to improve you need to observe a good example. Read others code and see what they are doing that you an implement in your own code.