Code Smell examples
Last active
November 27, 2023 11:50
-
-
Save kenpower/bf607bf054266759789d4d00f68477c6 to your computer and use it in GitHub Desktop.
Example Code smells for exam
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Customer { | |
private String name; | |
private Address address; | |
public Customer(String name, Address address) { | |
this.name = name; | |
this.address = address; | |
} | |
public Address getAddress() { | |
return address; | |
} | |
public String getName() { | |
return name; | |
} | |
} | |
public class Address { | |
private String street; | |
private String city; | |
private String zipCode; | |
public Address(String street, String city, String zipCode) { | |
this.street = street; | |
this.city = city; | |
this.zipCode = zipCode; | |
} | |
public String getFullAddress() { | |
return street + ", " + city + ", " + zipCode; | |
} | |
} | |
public class Invoice { | |
private Customer customer; | |
public Invoice(Customer customer) { | |
this.customer = customer; | |
} | |
public String getCustomerAddress() { | |
Address address = customer.getAddress(); | |
return address.getStreet() + ", " + address.getCity() + ", " + address.getZipCode(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
class Employee { | |
public: | |
void setEmployeeType(int type) { | |
if (type == 1) { | |
//a full-time employee | |
std::cout << "Setting employee as full-time." << std::endl; | |
} else if (type == 2) { | |
//a part-time employee | |
std::cout << "Setting employee as part-time." << std::endl; | |
} else if (type == 3) { | |
// 3 represents a contractor | |
std::cout << "Setting employee as contractor." << std::endl; | |
} | |
// ... other code ... | |
} | |
}; | |
int main() { | |
Employee emp; | |
emp.setEmployeeType(1); | |
emp.setEmployeeType(3); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmployeeManagementSystem { | |
private: | |
std::vector<Employee> employees; | |
std::vector<Department> departments; | |
// Employee-related data | |
// ... | |
// Department-related data | |
// ... | |
public: | |
// Employee-related methods | |
void addEmployee(const Employee& employee); | |
void removeEmployee(int employeeId); | |
void updateEmployee(int employeeId, const Employee& employee); | |
Employee getEmployee(int employeeId); | |
void printEmployeeDetails(int employeeId); | |
// ... many more employee-related methods | |
// Department-related methods | |
void addDepartment(const Department& department); | |
void removeDepartment(int departmentId); | |
void updateDepartment(int departmentId, const Department& department); | |
Department getDepartment(int departmentId); | |
void printDepartmentDetails(int departmentId); | |
// ... many more department-related methods | |
// Report generation methods | |
void generateEmployeeReport(); | |
void generateDepartmentReport(); | |
// ... other report-related methods | |
// Utility methods | |
// ... | |
// ... potentially more methods related to other aspects of employee management | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
class Order { | |
public: | |
std::string customerName; | |
std::string customerAddress; | |
std::string orderDate; // Format: YYYY-MM-DD | |
std::vector<std::string> orderItems; // Item names | |
std::vector<int> orderQuantities; // Corresponding quantities | |
// Constructor | |
Order(const std::string& name, const std::string& address, const std::string& date) : | |
customerName(name), customerAddress(address), orderDate(date) {} | |
// Add item to the order | |
void addItem(const std::string& itemName, int quantity) { | |
orderItems.push_back(itemName); | |
orderQuantities.push_back(quantity); | |
} | |
// ... Other methods ... | |
}; | |
int main() { | |
// Creating an order | |
Order order("John Doe", "123 Main St", "2023-03-15"); | |
order.addItem("Widget", 3); | |
order.addItem("Gadget", 2); | |
// ... Further processing ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
class Order { | |
public: | |
void processOrder(const std::string& customerName, const std::string& customerAddress, const std::string& customerEmail, int orderID, double orderAmount) { | |
std::cout << "Processing order for " << customerName << std::endl; | |
// ...logic using customer details and order info... | |
} | |
void shipOrder(const std::string& customerName, const std::string& customerAddress, const std::string& customerEmail, int orderID) { | |
std::cout << "Shipping order " << orderID << " to " << customerAddress << std::endl; | |
// ...logic using customer details and order info... | |
} | |
}; | |
int main() { | |
Order order; | |
order.processOrder("John Doe", "123 Main St", "[email protected]", 1001, 59.99); | |
order.shipOrder("John Doe", "123 Main St", "[email protected]", 1001); | |
// ...other operations... | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
class ReportGenerator { | |
public: | |
void generateSalesReport(const std::vector<int>& sales) { | |
int totalSales = 0; | |
for (int sale : sales) { | |
totalSales += sale; | |
} | |
std::cout << "Total Sales: " << totalSales << std::endl; | |
} | |
void generateExpenseReport(const std::vector<int>& expenses) { | |
int totalExpenses = 0; | |
for (int expense : expenses) { | |
totalExpenses += expense; | |
} | |
std::cout << "Total Expenses: " << totalExpenses << std::endl; | |
} | |
// Other similar methods... | |
}; | |
int main() { | |
ReportGenerator reportGen; | |
std::vector<int> sales = {100, 150, 200}; | |
std::vector<int> expenses = {80, 90, 50}; | |
reportGen.generateSalesReport(sales); | |
reportGen.generateExpenseReport(expenses); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
class ReportGenerator { | |
public: | |
void generateReport(const std::string& title, const std::string& author, const std::string& content, | |
const std::string& date, const std::string& format, const std::string& font, | |
int fontSize, const std::string& header, const std::string& footer, int margin) { | |
// Generate the report | |
std::cout << "Generating report ..." << std::endl; | |
// ... Report generation logic ... | |
} | |
}; | |
int main() { | |
ReportGenerator reportGen; | |
reportGen.generateReport("Annual Report", "John Doe", "Report Content", "2023-03-15", | |
"PDF", "Arial", 12, "Report Header", "Report Footer", 1); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <string> | |
#include <algorithm> | |
class SalesReport { | |
public: | |
void generateReport(const std::vector<int>& sales, const std::string& month) { | |
int totalSales = 0; | |
int highestSale = 0; | |
int lowestSale = INT_MAX; | |
int averageSale = 0; | |
// Print report header | |
std::cout << "Sales Report for " << month << std::endl; | |
std::cout << "----------------------------------" << std::endl; | |
// Process each sale | |
for (int sale : sales) { | |
totalSales += sale; | |
highestSale = std::max(highestSale, sale); | |
lowestSale = std::min(lowestSale, sale); | |
std::cout << "Sale Amount: " << sale << std::endl; | |
} | |
// Calculate average sales | |
if (!sales.empty()) { | |
averageSale = totalSales / sales.size(); | |
} | |
// Print report summary | |
std::cout << "----------------------------------" << std::endl; | |
std::cout << "Total Sales: " << totalSales << std::endl; | |
std::cout << "Highest Sale: " << highestSale << std::endl; | |
std::cout << "Lowest Sale: " << lowestSale << std::endl; | |
std::cout << "Average Sale: " << averageSale << std::endl; | |
} | |
}; | |
int main() { | |
SalesReport report; | |
std::vector<int> monthlySales = {150, 200, 250, 300, 350}; | |
report.generateReport(monthlySales, "March"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment