Part of the software developer job is to find new and better ways to solve problems. Writing code using his/her wits, intelligent and creativity.
However, sometimes being too clever can lead to hard to track bugs, maintainability issues and impossible to understand code. Is all cleverly written code good code, or is it a problem just waiting to happen?
In this session, I will show you real world examples of cleverly written code. And show you how we can use clean code principles, refactoring and design patterns, to transform that code from clever code to good code, code one that your peers - and future self - would thank you for writing.
16. public class Tree<T> {
public T Value { get; }
public Tree<T> Left { get; }
public Tree<T> Right { get; }
public void Find(T value)
{
if(Value.Equals(value))
throw new FoundElementException<T>(this);
Left?.Find(value);
Right?.Find(value);
}
}
20. Optimizations
void calculate(struct salesinfo* sales)
{
jmp_buf buffer;
int i=setjmp(buffer);
if (!(i<sales->count))
RETURN_NOTHING;
addToSubtotal(sales->values[i]);
if (i<sales->count)
{
longjmp(buffer,i+1);
}
}
void calculate(struct salesinfo*sales)
{
for(int i = 0; i < sales->count; i++)
{
addToSubtotal(sales->values[i]);
}
}
21. Real optimizations
float InvSqrt(float x)
{
float xhalf = 0.5f * x;
int i = *(int*)&x;
i = 0x5f375a86 - (i >> 1);
x = *(float*)&i;
x = x * (1.5f - xhalf * x * x);
return x;
}
When asked developers why they wrote a specific piece of code they might come with one of several reasons…
But the truth is simpler than that – let’s try a little experiment
It makes us feel smart and powerful when we write clever code and it also makes us feel great when we read and understand it as well.
Although you have to pass through the part in which you want to chock the guy who wrote the code
Clever code is not dumb code or bad code in itself,
It’s a unique and cool implementation done in a way that you haven’t thought of before.
But there are instances where the author of the clever code has gone too far – and I like to show you some of those cases
So
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
Clever code could be a must, but there’s a price to pay – it is impossible to maintain
When you need to extend or update you have no choice but to completely re-write it
If I had to write the code I’ve used I would have to write a lot of boilerplate code
My conclusion is that Clever code is not always good or bad, but it does have a cost,
Maintenance would be harder, extending you code will be difficult or even impossible so you need to make sure you’ve got a good reason to pay that price.
In other words - don’t write write clever cod just for the sake of being clever write it only when you have to because there is no better way to solve a complex problem, create a user friendly API or
Because real clever code is not about finding a dark corner of your programming language to show how smart you are but rather on making life simpler for whoever is going to us it – including your future self.