SlideShare a Scribd company logo
1 of 26
From Clever code
to Better code
Dror Helper | http://helpercode.com | Practical Software | @dhelper
Consultant & software architect @ Practical Software
Developing software since 2002
Clean Coder & Test Driven Developer
Pluralsight author
B: http://helpercode.com
T: @dhelper
About.ME
A long time ago,
in an office not that far from here . . . .
#define GET_VAL( val, type ) 
{ 
ASSERT( ( pIP + sizeof(type) ) <= pMethodEnd ); 
val = ( *((type *&)(pIP))++ ); 
}
What this code does now I understand, clever it is!
Why do we write Clever Code?
Because we can
Because we have to
We don’t know any better
Performance
void send(short* from, short* to, size_t count) {
int n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
}
12
n = 2
<-- 4
--->
--->
--->
--->
N = 1
--->
--->
--->
--->
REALITY CHECK:
We write Clever Code because it makes us feel good
* Most programmers love to be clever
There is more than
one way to write
Clever Code
public void int GetNextSize(int i)
{
return i > 0 ? i << 2 : ~(i << 2) + 1;
}
public void int GetNextSize(int i)
{
return Math.Abs(i * 4);
}
if((i & (i - 1) == 0)
{
...
}
if(IsPowerOfTwo(i))
{
...
}
public bool IsPowerOfTwo(int i)
{
return ((i & (i - 1) == 0;
}
Low level programming
Avoid when you can
Refactor to show intent
Compressed Code
a = y[0]>y[1] ? (b=1,0) : (b=0,1);
if(y[0] > y[1])
{
a = 0;
b = 1;
} else {
a = 1;
b = 0;
}
do{
// code goes here
if(some condition)
break;
// ...
if(other condition)
break;
// ...
} while(false)
// cleanup here
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);
}
}
Creative (ab)use
Don’t surprise the reader!
Think of a better way to express yourself
Writing < Reading < Debugging
What about performance?
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]);
}
}
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;
}
Is clever code
Good or Evil?
Is clever code
Good or Evil?
#define GET_VAL( val, type ) 
{ 
ASSERT( ( pIP + sizeof(type) ) <= pMethodEnd ); 
val = ( *((type *&)(pIP))++ ); 
}
void parseCode(BYTE* pIp, BYTE* pMethodEnd)
{
int valueType = getValueType(pIp)
while(pIp < pMethodEnd)
{
switch(valueType){
case ValueType::INT:
int value;
GET_VAL(value, int)
// do something with value
break;
case ValueType::BYTE:
...
}
}
}
Thank you 
Dror Helper | http://helpercode.com | @dhelper
Links
• https://stackoverflow.com/questions/178265/what-is-the-most-hard-to-understand-
piece-of-c-code-you-know
• https://www.simplethread.com/dont-be-clever/
• https://stackoverflow.com/questions/2338612/confusing-use-of-a-comma-in-
an-if-statement
• http://thedailywtf.com/articles/Longjmp--FOR-SPEED!!!
• http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf

More Related Content

What's hot

Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
Woody Pewitt
 
c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2
sajidpk92
 
Unit testing with Easymock
Unit testing with EasymockUnit testing with Easymock
Unit testing with Easymock
Ürgo Ringo
 

What's hot (20)

jq: JSON - Like a Boss
jq: JSON - Like a Bossjq: JSON - Like a Boss
jq: JSON - Like a Boss
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
 
The Ring programming language version 1.7 book - Part 85 of 196
The Ring programming language version 1.7 book - Part 85 of 196The Ring programming language version 1.7 book - Part 85 of 196
The Ring programming language version 1.7 book - Part 85 of 196
 
c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2
 
Unit testing with Easymock
Unit testing with EasymockUnit testing with Easymock
Unit testing with Easymock
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
Jason parsing
Jason parsingJason parsing
Jason parsing
 
The secret unit testing tools no one has ever told you about
The secret unit testing tools no one has ever told you aboutThe secret unit testing tools no one has ever told you about
The secret unit testing tools no one has ever told you about
 
Automatically Documenting Program Changes
Automatically Documenting Program ChangesAutomatically Documenting Program Changes
Automatically Documenting Program Changes
 
Navigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupNavigating the xDD Alphabet Soup
Navigating the xDD Alphabet Soup
 
C# 7.0, 7.1, 7.2
C# 7.0, 7.1, 7.2C# 7.0, 7.1, 7.2
C# 7.0, 7.1, 7.2
 
Algoritmos sujei
Algoritmos sujeiAlgoritmos sujei
Algoritmos sujei
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
 
Promise: async programming hero
Promise: async programming heroPromise: async programming hero
Promise: async programming hero
 
Golang dot-testing
Golang dot-testingGolang dot-testing
Golang dot-testing
 
The Ring programming language version 1.5.3 book - Part 27 of 184
The Ring programming language version 1.5.3 book - Part 27 of 184The Ring programming language version 1.5.3 book - Part 27 of 184
The Ring programming language version 1.5.3 book - Part 27 of 184
 
The Ring programming language version 1.5.2 book - Part 38 of 181
The Ring programming language version 1.5.2 book - Part 38 of 181The Ring programming language version 1.5.2 book - Part 38 of 181
The Ring programming language version 1.5.2 book - Part 38 of 181
 
Beautiful java script
Beautiful java scriptBeautiful java script
Beautiful java script
 
Property-Based Testing
Property-Based TestingProperty-Based Testing
Property-Based Testing
 

Similar to From clever code to better code

Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
Deepak Singh
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
Jussi Pohjolainen
 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans inc
nayakq
 

Similar to From clever code to better code (20)

From clever code to better code
From clever code to better codeFrom clever code to better code
From clever code to better code
 
C#, What Is Next?
C#, What Is Next?C#, What Is Next?
C#, What Is Next?
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
 
Practices For Becoming A Better Programmer
Practices For Becoming A Better ProgrammerPractices For Becoming A Better Programmer
Practices For Becoming A Better Programmer
 
Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code Smells
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit Testing
 
C tutorial
C tutorialC tutorial
C tutorial
 
Refactoring
RefactoringRefactoring
Refactoring
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
 
pointers 1
pointers 1pointers 1
pointers 1
 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans inc
 
Effective C#
Effective C#Effective C#
Effective C#
 
DDDesign Challenges
DDDesign ChallengesDDDesign Challenges
DDDesign Challenges
 
Priming Java for Speed at Market Open
Priming Java for Speed at Market OpenPriming Java for Speed at Market Open
Priming Java for Speed at Market Open
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
Lowering in C#: What really happens with your code?, from NDC Oslo 2019
Lowering in C#: What really happens with your code?, from NDC Oslo 2019Lowering in C#: What really happens with your code?, from NDC Oslo 2019
Lowering in C#: What really happens with your code?, from NDC Oslo 2019
 

More from Dror Helper

More from Dror Helper (20)

Debugging with visual studio beyond 'F5'
Debugging with visual studio beyond 'F5'Debugging with visual studio beyond 'F5'
Debugging with visual studio beyond 'F5'
 
A software developer guide to working with aws
A software developer guide to working with awsA software developer guide to working with aws
A software developer guide to working with aws
 
The role of the architect in agile
The role of the architect in agileThe role of the architect in agile
The role of the architect in agile
 
Harnessing the power of aws using dot net core
Harnessing the power of aws using dot net coreHarnessing the power of aws using dot net core
Harnessing the power of aws using dot net core
 
Developing multi-platform microservices using .NET core
 Developing multi-platform microservices using .NET core Developing multi-platform microservices using .NET core
Developing multi-platform microservices using .NET core
 
Harnessing the power of aws using dot net
Harnessing the power of aws using dot netHarnessing the power of aws using dot net
Harnessing the power of aws using dot net
 
Secret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you aboutSecret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you about
 
C++ Unit testing - the good, the bad & the ugly
C++ Unit testing - the good, the bad & the uglyC++ Unit testing - the good, the bad & the ugly
C++ Unit testing - the good, the bad & the ugly
 
Working with c++ legacy code
Working with c++ legacy codeWorking with c++ legacy code
Working with c++ legacy code
 
Visual Studio tricks every dot net developer should know
Visual Studio tricks every dot net developer should knowVisual Studio tricks every dot net developer should know
Visual Studio tricks every dot net developer should know
 
Secret unit testing tools
Secret unit testing toolsSecret unit testing tools
Secret unit testing tools
 
Electronics 101 for software developers
Electronics 101 for software developersElectronics 101 for software developers
Electronics 101 for software developers
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
 
Who’s afraid of WinDbg
Who’s afraid of WinDbgWho’s afraid of WinDbg
Who’s afraid of WinDbg
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent code
 
Designing with tests
Designing with testsDesigning with tests
Designing with tests
 
Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013
 
Writing clean code in C# and .NET
Writing clean code in C# and .NETWriting clean code in C# and .NET
Writing clean code in C# and .NET
 
Using FakeIteasy
Using FakeIteasyUsing FakeIteasy
Using FakeIteasy
 
Battle of The Mocking Frameworks
Battle of The Mocking FrameworksBattle of The Mocking Frameworks
Battle of The Mocking Frameworks
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Recently uploaded (20)

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 

From clever code to better code

  • 1. From Clever code to Better code Dror Helper | http://helpercode.com | Practical Software | @dhelper
  • 2. Consultant & software architect @ Practical Software Developing software since 2002 Clean Coder & Test Driven Developer Pluralsight author B: http://helpercode.com T: @dhelper About.ME
  • 3. A long time ago, in an office not that far from here . . . .
  • 4. #define GET_VAL( val, type ) { ASSERT( ( pIP + sizeof(type) ) <= pMethodEnd ); val = ( *((type *&)(pIP))++ ); }
  • 5. What this code does now I understand, clever it is!
  • 6. Why do we write Clever Code? Because we can Because we have to We don’t know any better Performance
  • 7. void send(short* from, short* to, size_t count) { int n = (count + 7) / 8; switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while (--n > 0); } } 12 n = 2 <-- 4 ---> ---> ---> ---> N = 1 ---> ---> ---> --->
  • 8. REALITY CHECK: We write Clever Code because it makes us feel good * Most programmers love to be clever
  • 9. There is more than one way to write Clever Code
  • 10. public void int GetNextSize(int i) { return i > 0 ? i << 2 : ~(i << 2) + 1; } public void int GetNextSize(int i) { return Math.Abs(i * 4); }
  • 11. if((i & (i - 1) == 0) { ... } if(IsPowerOfTwo(i)) { ... } public bool IsPowerOfTwo(int i) { return ((i & (i - 1) == 0; }
  • 12. Low level programming Avoid when you can Refactor to show intent
  • 13. Compressed Code a = y[0]>y[1] ? (b=1,0) : (b=0,1); if(y[0] > y[1]) { a = 0; b = 1; } else { a = 1; b = 0; }
  • 14.
  • 15. do{ // code goes here if(some condition) break; // ... if(other condition) break; // ... } while(false) // cleanup here
  • 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); } }
  • 17. Creative (ab)use Don’t surprise the reader! Think of a better way to express yourself
  • 18. Writing < Reading < Debugging
  • 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; }
  • 24. #define GET_VAL( val, type ) { ASSERT( ( pIP + sizeof(type) ) <= pMethodEnd ); val = ( *((type *&)(pIP))++ ); } void parseCode(BYTE* pIp, BYTE* pMethodEnd) { int valueType = getValueType(pIp) while(pIp < pMethodEnd) { switch(valueType){ case ValueType::INT: int value; GET_VAL(value, int) // do something with value break; case ValueType::BYTE: ... } } }
  • 25. Thank you  Dror Helper | http://helpercode.com | @dhelper
  • 26. Links • https://stackoverflow.com/questions/178265/what-is-the-most-hard-to-understand- piece-of-c-code-you-know • https://www.simplethread.com/dont-be-clever/ • https://stackoverflow.com/questions/2338612/confusing-use-of-a-comma-in- an-if-statement • http://thedailywtf.com/articles/Longjmp--FOR-SPEED!!! • http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf

Editor's Notes

  1. 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
  2. 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
  3. 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
  4. 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."
  5. 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
  6. 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.