Tech Debt - Do you avoid 'clever' code?
Updated by Brady Stroud [SSW] 1 year ago. See history
123
No component provided for introEmbed
var totalMoved = sortedChannels.Where((channel, i) => channel.Position != i).Count();
❌ Figure: Bad example - Smart code, that could be even simpler! (Although, not bad by any means)
var totalMoved = 0;for (var i = 0; i < sortedChannels.Count; i++){var channel = sortedChannels[i];if (channel.Position != i){totalMoved++}}
✅ Figure: Good example - Simple code, while it has more lines, it is easier to read!
When is Simple Code Bad?
Let's take a moment to digest this more generic example:
[ProducesResponseType(StatusCodes.Status404NotFound)][ProducesResponseType(StatusCodes.Status204NoContent)][HttpDelete("{id:guid}")]public async Task<IActionResult> Delete(Guid id){var model = await _context.Styles.FirstOrDefaultAsync(x => x.Id == id);if (model is null){return NotFound();}_context.Styles.Remove(model);await _context.SaveChangesAsync();return NoContent();}
At first glance, this is pretty simple - almost what you would find on an intro to EF Core & ASP.NET Core Web API on the Microsoft documentation!
As code scales, sometimes we need to write more 'clever' code, to abstract away concerns (like data access, or application logic).
So depending on the context, this is both good & bad code at the same time!
Related rules
Need help?
SSW Consulting has over 30 years of experience developing awesome software solutions.