Top Banner
103

Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Jan 22, 2018

Download

Software

Kevlin Henney
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 2: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 3: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 4: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

/ WordFriday

Page 5: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

enterprise, noun

Page 6: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 7: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 8: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 9: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 10: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

enterprise, noun a project or undertaking that is especially bold,

complicated or arduous

readiness to engage in undertakings of difficulty, risk,

danger or daring

a design of which the execution is attempted

a commercial or industrial undertaking

a firm, company or business

a unit of economic organisation or activity

Concise Oxford English Dictionary ∙ Oxford English Dictionary ∙ Merriam-Webster's Collegiate Dictionary

Page 11: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

enterprise, noun a project or undertaking that is especially bold,

complicated or arduous

readiness to engage in undertakings of difficulty, risk,

danger or daring

a design of which the execution is attempted

a commercial or industrial undertaking

a firm, company or business

a unit of economic organisation or activity

Concise Oxford English Dictionary ∙ Oxford English Dictionary ∙ Merriam-Webster's Collegiate Dictionary

Page 12: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

code, noun a set of instructions for a computer

a computer program, or a portion thereof

a system of words, figures or symbols used to

represent others, especially for the purposes of secrecy

a set of conventions or principles governing behaviour

or activity in a particular domain

Concise Oxford English Dictionary ∙ Oxford English Dictionary ∙ Merriam-Webster's Collegiate Dictionary

Page 13: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 14: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Fizz buzz is a group word game for children to teach them about division.

http://en.wikipedia.org/wiki/Fizz_buzz

Page 15: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Players generally sit in a circle. The player designated to go first says the number "1", and each player thenceforth counts one number in turn. However, any number divisible by three is replaced by the word fizz and any divisible by five by the word buzz. Numbers divisible by both become fizz buzz. A player who hesitates or makes a mistake is eliminated from the game.

http://en.wikipedia.org/wiki/Fizz_buzz

Page 16: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Players generally sit in a circle. The player designated to go first says the number "1", and each player thenceforth counts one number in turn. However, any number divisible by three is replaced by the word fizz and any divisible by five by the word buzz. Numbers divisible by both become fizz buzz. A player who hesitates or makes a mistake is eliminated from the game.

http://en.wikipedia.org/wiki/Fizz_buzz

Page 17: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Players generally sit in a circle. The player designated to go first says the number "1", and each player thenceforth counts one number in turn. However, any number divisible by three is replaced by the word fizz and any divisible by five by the word buzz. Numbers divisible by both become fizz buzz. A player who hesitates or makes a mistake is eliminated from the game.

http://en.wikipedia.org/wiki/Fizz_buzz

Page 18: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Adults may play Fizz buzz as a drinking game, where making a mistake leads to the player having to make a drinking-related forfeit.

http://en.wikipedia.org/wiki/Fizz_buzz

[citation needed]

Page 19: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Fizz buzz has been used as an interview screening device for computer programmers.

http://en.wikipedia.org/wiki/Fizz_buzz

Page 20: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

FizzBuzz was invented to avoid the awkwardness of realising that nobody in the room can binary search an array.

https://twitter.com/richardadalton/status/591534529086693376

Page 21: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

// Define constants #define FIZZ "fizz" #define BUZZ "buzz" #define NULL 0 #define PI 3.14 std::string FizzBuzzCalculator(int n) { // Declarations char buffer[1024]; memset(buffer, 0, 1024); std::string retval; auto isDivisibleBy3 = [&]() { if (n % 3 == 0) { strcpy(buffer, FIZZ); return true; } return false; }; auto isDivisibleBy5 = [&]() { if (n % 5 == 0) { strcat(buffer, BUZZ); return true; } return false; }; // Check to see if argument is between 1 and 100, otherwise log and throw if (n < 1 || n > 100) { sprintf(buffer, "FizzBuzzCalculator, argument must be in range: %i", n); Log::Instance().Write(buffer); throw std::invalid_argument(buffer); } // Check if need to write number if (!isDivisibleBy3() & !isDivisibleBy5()) { sprintf(buffer, "%i", n); } // Assign return value (retval) and return retval.assign(buffer, strlen(buffer)); return retval; }

Page 22: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

// Define constants #define FIZZ "fizz" #define BUZZ "buzz" #define NULL 0 #define PI 3.14 std::string FizzBuzzCalculator(int n) { // Declarations char buffer[1024]; memset(buffer, 0, 1024); std::string retval; auto isDivisibleBy3 = [&]() { if (n % 3 == 0) { strcpy(buffer, FIZZ); return true; } return false; }; auto isDivisibleBy5 = [&]() { if (n % 5 == 0) { strcat(buffer, BUZZ); return true; } return false; }; // Check to see if argument is between 1 and 100, otherwise log and throw if (n < 1 || n > 100) { sprintf(buffer, "FizzBuzzCalculator, argument must be in range: %i", n); Log::Instance().Write(buffer); throw std::invalid_argument(buffer); } // Check if need to write number if (!isDivisibleBy3() & !isDivisibleBy5()) { sprintf(buffer, "%i", n); } // Assign return value (retval) and return retval.assign(buffer, strlen(buffer)); return retval; }

Page 23: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

// Define constants #define FIZZ "fizz" #define BUZZ "buzz" #define NULL 0 #define PI 3.14 std::string FizzBuzzCalculator(int n) { // Declarations char buffer[1024]; memset(buffer, 0, 1024); std::string retval; auto isDivisibleBy3 = [&]() { if (n % 3 == 0) { strcpy(buffer, FIZZ); return true; } return false; }; auto isDivisibleBy5 = [&]() { if (n % 5 == 0) { strcat(buffer, BUZZ); return true; } return false; }; // Check to see if argument is between 1 and 100, otherwise log and throw if (n < 1 || n > 100) { sprintf(buffer, "FizzBuzzCalculator, argument must be in range: %i", n); Log::Instance().Write(buffer); throw std::invalid_argument(buffer); } // Check if need to write number if (!isDivisibleBy3() & !isDivisibleBy5()) { sprintf(buffer, "%i", n); } // Assign return value (retval) and return retval.assign(buffer, strlen(buffer)); return retval; }

Page 24: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

// Define constants #define FIZZ "fizz" #define BUZZ "buzz" #define NULL 0 #define PI 3.14 std::string FizzBuzzCalculator(int n) { // Declarations char buffer[1024]; memset(buffer, 0, 1024); std::string retval; auto isDivisibleBy3 = [&]() { if (n % 3 == 0) { strcpy(buffer, FIZZ); return true; } return false; }; auto isDivisibleBy5 = [&]() { if (n % 5 == 0) { strcat(buffer, BUZZ); return true; } return false; }; // Check to see if argument is between 1 and 100, otherwise log and throw if (n < 1 || n > 100) { sprintf(buffer, "FizzBuzzCalculator, argument must be in range: %i", n); Log::Instance().Write(buffer); throw std::invalid_argument(buffer); } // Check if need to write number if (!isDivisibleBy3() & !isDivisibleBy5()) { sprintf(buffer, "%i", n); } // Assign return value (retval) and return retval.assign(buffer, strlen(buffer)); return retval; }

Page 25: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

// Define constants #define FIZZ "fizz" #define BUZZ "buzz" #define NULL 0 #define PI 3.14 std::string FizzBuzzCalculator(int n) { // Declarations char buffer[1024]; memset(buffer, 0, 1024); std::string retval; auto isDivisibleBy3 = [&]() { if (n % 3 == 0) { strcpy(buffer, FIZZ); return true; } return false; }; auto isDivisibleBy5 = [&]() { if (n % 5 == 0) { strcat(buffer, BUZZ); return true; } return false; }; // Check to see if argument is between 1 and 100, otherwise log and throw if (n < 1 || n > 100) { sprintf(buffer, "FizzBuzzCalculator, argument must be in range: %i", n); Log::Instance().Write(buffer); throw std::invalid_argument(buffer); } // Check if need to write number if (!isDivisibleBy3() & !isDivisibleBy5()) { sprintf(buffer, "%i", n); } // Assign return value (retval) and return retval.assign(buffer, strlen(buffer)); return retval; }

Page 26: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

// Define constants #define FIZZ "fizz" #define BUZZ "buzz" #define NULL 0 #define PI 3.14 std::string FizzBuzzCalculator(int n) { // Declarations char buffer[1024]; memset(buffer, 0, 1024); std::string retval; auto isDivisibleBy3 = [&]() { if (n % 3 == 0) { strcpy(buffer, FIZZ); return true; } return false; }; auto isDivisibleBy5 = [&]() { if (n % 5 == 0) { strcat(buffer, BUZZ); return true; } return false; }; // Check to see if argument is between 1 and 100, otherwise log and throw if (n < 1 || n > 100) { sprintf(buffer, "FizzBuzzCalculator, argument must be in range: %i", n); Log::Instance().Write(buffer); throw std::invalid_argument(buffer); } // Check if need to write number if (!isDivisibleBy3() & !isDivisibleBy5()) { sprintf(buffer, "%i", n); } // Assign return value (retval) and return retval.assign(buffer, strlen(buffer)); return retval; }

Page 27: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

// Define constants #define FIZZ "fizz" #define BUZZ "buzz" #define NULL 0 #define PI 3.14 std::string FizzBuzzCalculator(int n) { // Declarations char buffer[1024]; memset(buffer, 0, 1024); std::string retval; auto isDivisibleBy3 = [&]() { if (n % 3 == 0) { strcpy(buffer, FIZZ); return true; } return false; }; auto isDivisibleBy5 = [&]() { if (n % 5 == 0) { strcat(buffer, BUZZ); return true; } return false; }; // Check to see if argument is between 1 and 100, otherwise log and throw if (n < 1 || n > 100) { sprintf(buffer, "FizzBuzzCalculator, argument must be in range: %i", n); Log::Instance().Write(buffer); throw std::invalid_argument(buffer); } // Check if need to write number if (!isDivisibleBy3() & !isDivisibleBy5()) { sprintf(buffer, "%i", n); } // Assign return value (retval) and return retval.assign(buffer, strlen(buffer)); return retval; }

Page 28: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

I have yet to see any problem,

however complicated, which,

when you looked at it in the

right way, did not become still

more complicated.

Anderson's Law

Page 29: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 30: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 31: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

http://www.adampetersen.se/articles/fizzbuzz.htm

Page 32: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

http://www.adampetersen.se/articles/fizzbuzz.htm

Page 33: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

enum class fizzbuzzed

{

fizz = -2,

buzz = -1,

fizzbuzz = 0,

first = 1,

last = 100

};

constexpr fizzbuzzed fizzbuzz(int n)

{

return

n % 3 == 0 && n % 5 == 0 ? fizzbuzzed::fizzbuzz :

n % 3 == 0 ? fizzbuzzed::fizz :

n % 5 == 0 ? fizzbuzzed::buzz :

fizzbuzzed(n);

}

Page 34: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

enum class fizzbuzzed

{

fizz = -2,

buzz = -1,

fizzbuzz = 0,

first = 1,

last = 100

};

constexpr fizzbuzzed fizzbuzz(int n)

{

return

n % 15 == 0 ? fizzbuzzed::fizzbuzz :

n % 3 == 0 ? fizzbuzzed::fizz :

n % 5 == 0 ? fizzbuzzed::buzz :

fizzbuzzed(n);

}

Page 35: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Maciej Piróg

"FizzBuzz in Haskell by Embedding a Domain-Specific Language"

Page 36: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

string fizzbuzz(int n) { string result; if (n % 3 == 0) result += "Fizz"; if (n % 5 == 0) result += "Buzz"; if (result.empty()) result = to_string(n); return result; }

Page 37: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

string fizzbuzz(int n) { static const string fizzed[] { "", "Fizz" }; static const string buzzed[] { "", "Buzz" }; const string result = fizzed[n % 3 == 0] + buzzed[n % 5 == 0]; return result.empty() ? to_string(n) : result; }

Page 38: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Maciej Piróg

"FizzBuzz in Haskell by Embedding a Domain-Specific Language"

Page 39: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

string fizzbuzz(int n) { auto fizz = [=](function<string(string)> f) { return n % 3 == 0 ? [=](auto) { return "Fizz" + f(""); } : f; }; auto buzz = [=](function<string(string)> f) { return n % 5 == 0 ? [=](auto) { return "Buzz" + f(""); } : f; }; auto id = [](auto s) { return s; }; return fizz(buzz(id))(to_string(n)); }

Page 40: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

string fizzbuzz(int n) { auto test = [=](int d, string s, function<string(string)> f) { return n % d == 0 ? [=](string) { return s + f(""); } : f; }; auto fizz = bind(test, 3, "Fizz", _1); auto buzz = bind(test, 5, "Buzz", _1); auto id = [](auto s) { return s; }; return fizz(buzz(id))(to_string(n)); }

Page 41: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

A work of art is the unique result of a unique temperament.

Oscar Wilde

Page 42: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Cargo cult programming is a style of computer programming characterized by the ritual inclusion of code or program structures that serve no real purpose.

http://en.wikipedia.org/wiki/Cargo_cult_programming

Page 43: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Signal-to-noise ratio (often abbreviated SNR or

S/N) is a measure used in science and engineering

that compares the level of a desired signal to the

level of background noise.

Signal-to-noise ratio is sometimes used informally

to refer to the ratio of useful information to false or

irrelevant data in a conversation or exchange.

http://en.wikipedia.org/wiki/Signal_to_noise_ratio

Page 44: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

https://twitter.com/ExpertBeginner1/status/603188084725981185

Page 45: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

A common fallacy is to assume authors of incomprehensible code will somehow be able to express themselves lucidly and clearly in comments.

Kevlin Henney https://twitter.com/KevlinHenney/status/381021802941906944

Page 46: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Cargo cult programming is a style of computer programming characterized by the ritual inclusion of code or program structures that serve no real purpose.

Cargo cult programming can also refer to the results of applying a design pattern or coding style blindly without understanding the reasons behind that design principle.

http://en.wikipedia.org/wiki/Cargo_cult_programming

Page 47: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 48: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

http://www.bonkersworld.net/object-world/

Page 49: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

http://www.bonkersworld.net/object-world/

OBJECT-ORIENTED

VenetianBlind

Door

Television

Picture Glass

Sofa TelevisionRemoteControl

Peephole

Page 50: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Naomi Epel The Observation Deck

Page 51: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 52: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

People will be using the words

you choose in their conversation

for the next 20 years. You want

to be sure you do it right.

Unfortunately, many people get

all formal [...]. Just calling it what

it is isn't enough.

Page 53: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

They have to tack on a flowery,

computer science-y, impressive

sounding, but ultimately

meaningless word, like Object,

Thing, Component, Part,

Manager, Entity, or Item.

Page 54: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

class ConditionChecker { public: virtual bool CheckCondition() const = 0; ... };

Page 55: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

class Condition { public: virtual bool IsTrue() const = 0; ... };

Page 56: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 57: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 58: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Refactoring (noun): a change made to

the internal structure of software to

make it easier to understand and

cheaper to modify without changing

its observable behavior.

Refactor (verb): to restructure

software by applying a series of

refactorings without changing the

observable behavior of the software.

Page 59: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 60: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Functional

Operational

Developmental

Page 61: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { // Declarations char buffer[1024]; std::string cfgAddress; unsigned long address; std::string cfgPort; unsigned short port; Connection * result; // Get address and check that its OK (throw an exception if its not) cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) { sprintf(buffer, "Configuration value missing: %s", "address"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } // Convert adress to bytes and check that its OK (throw an exception if its not) address = inet_addr(cfgAddress.data()); if (address == -1) { sprintf(buffer, "Invalid address: %s", cfgAddress.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } // Get port and check that its OK (throw an exception if its not) cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) { sprintf(buffer, "Configuration value missing: %s", "port"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } // Convert port too bytes port = htons(atoi(cfgPort.data())); // Creation connection and check that its OK (throw an exception if its not) result = new Connection(address, port); if (!result || !result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.data(), cfgPort.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } // Return the connection return result; }

Page 62: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { // Declarations char buffer[1024]; std::string cfgAddress; unsigned long address; std::string cfgPort; unsigned short port; Connection * result; ... }

Page 63: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { ... // Get address and check that its OK (throw an exception if its not) cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) { sprintf(buffer, "Configuration value missing: %s", "address"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } ... }

Page 64: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { ... // Convert adress to bytes and check that its OK (throw an exception if its not) address = inet_addr(cfgAddress.data()); if (address == -1) { sprintf(buffer, "Invalid address: %s", cfgAddress.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } ... }

Page 65: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { ... // Get port and check that its OK (throw an exception if its not) cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) { sprintf(buffer, "Configuration value missing: %s", "port"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } ... }

Page 66: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { ... // Convert port too bytes port = htons(atoi(cfgPort.data())); ... }

Page 67: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { ... // Creation connection and check that its OK (throw an exception if its not) result = new Connection(address, port); if (!result || !result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.data(), cfgPort.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } ... }

Page 68: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { ... // Return the connection return result; }

Page 69: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { // Declarations ... // Get address and check that its OK (throw an exception if its not) ... // Convert adress to bytes and check that its OK (throw an exception if its not) ... // Get port and check that its OK (throw an exception if its not) ... // Convert port too bytes ... // Creation connection and check that its OK (throw an exception if its not) ... // Return the connection ... }

Page 70: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { // Declarations ... // Get address and check that it's OK (throw an exception if it's not) ... // Convert address to bytes and check that it's OK (throw an exception if it's not) ... // Get port and check that it's OK (throw an exception if it's not) ... // Convert port to bytes ... // Creation connection and check that it's OK (throw an exception if it's not) ... // Return the connection ... }

Page 71: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { ... ... ... ... ... ... ... }

Page 72: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { char buffer[1024]; std::string cfgAddress; unsigned long address; std::string cfgPort; unsigned short port; Connection * result; cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) { sprintf(buffer, "Configuration value missing: %s", "address"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } address = inet_addr(cfgAddress.data()); if (address == -1) { sprintf(buffer, "Invalid address: %s", cfgAddress.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) { sprintf(buffer, "Configuration value missing: %s", "port"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } port = htons(atoi(cfgPort.data())); result = new Connection(address, port); if (!result || !result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.data(), cfgPort.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } return result; }

Page 73: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { char buffer[1024]; std::string cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) { sprintf(buffer, "Configuration value missing: %s", "address"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } unsigned long address = inet_addr(cfgAddress.data()); if (address == -1) { sprintf(buffer, "Invalid address: %s", cfgAddress.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } std::string cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) { sprintf(buffer, "Configuration value missing: %s", "port"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } unsigned short port = htons(atoi(cfgPort.data())); Connection * result = new Connection(address, port); if (!result || !result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.data(), cfgPort.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } return result; }

Page 74: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { char buffer[1024]; auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) { sprintf(buffer, "Configuration value missing: %s", "address"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto address = inet_addr(cfgAddress.data()); if (address == -1) { sprintf(buffer, "Invalid address: %s", cfgAddress.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) { sprintf(buffer, "Configuration value missing: %s", "port"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto port = htons(atoi(cfgPort.data())); Connection * result = new Connection(address, port); if (!result || !result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.data(), cfgPort.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } return result; }

Page 75: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { ... Connection * result = new Connection(address, port); if (!result || !result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.data(), cfgPort.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } return result; }

Page 76: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { ... Connection * result = new Connection(address, port); if (!result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.data(), cfgPort.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } return result; }

Page 77: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

std::auto_ptr<Connection> CreateServerConnection() { ... std::auto_ptr<Connection> result(new Connection(address, port)); if (!result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.data(), cfgPort.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } return result; }

Page 78: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

std::unique_ptr<Connection> CreateServerConnection() { ... auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.data(), cfgPort.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } return result; }

Page 79: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { ... auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.data(), cfgPort.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } return result.release(); }

Page 80: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { char buffer[1024]; auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) { sprintf(buffer, "Configuration value missing: %s", "address"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto address = inet_addr(cfgAddress.data()); if (address == -1) { sprintf(buffer, "Invalid address: %s", cfgAddress.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) { sprintf(buffer, "Configuration value missing: %s", "port"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto port = htons(atoi(cfgPort.data())); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.data(), cfgPort.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } return result.release(); }

Page 81: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { char buffer[1024]; auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) { sprintf(buffer, "Configuration value missing: %s", "address"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto address = inet_addr(cfgAddress.data()); if (address == -1) { sprintf(buffer, "Invalid address: %s", cfgAddress.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) { sprintf(buffer, "Configuration value missing: %s", "port"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto port = htons(atoi(cfgPort.data())); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.data(), cfgPort.data()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } return result.release(); }

Page 82: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { char buffer[1024]; auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) { sprintf(buffer, "Configuration value missing: %s", "address"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto address = inet_addr(cfgAddress.c_str()); if (address == -1) { sprintf(buffer, "Invalid address: %s", cfgAddress.c_str()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) { sprintf(buffer, "Configuration value missing: %s", "port"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto port = htons(atoi(cfgPort.c_str())); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.c_str(), cfgPort.c_str()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } return result.release(); }

Page 83: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { char buffer[1024]; auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) { sprintf(buffer, "Configuration value missing: %s", "address"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto address = inet_addr(cfgAddress.c_str()); if (address == -1) { sprintf(buffer, "Invalid address: %s", cfgAddress.c_str()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) { sprintf(buffer, "Configuration value missing: %s", "port"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto port = htons(stoi(cfgPort)); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.c_str(), cfgPort.c_str()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } return result.release(); }

Page 84: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { char buffer[1024]; auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) { sprintf(buffer, "Configuration value missing: %s", "address"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto address = inet_addr(cfgAddress.c_str()); if (address == -1) { sprintf(buffer, "Invalid address: %s", cfgAddress.c_str()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) { sprintf(buffer, "Configuration value missing: %s", "port"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto port = htons(stoi(cfgPort)); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) { sprintf(buffer, "Failed to connect: %s:%s", cfgAddress.c_str(), cfgPort.c_str()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } return result.release(); }

Page 85: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { char buffer[1024]; auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) { snprintf(buffer, sizeof buffer, "Configuration value missing: %s", "address"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto address = inet_addr(cfgAddress.c_str()); if (address == -1) { snprintf(buffer, sizeof buffer, "Invalid address: %s", cfgAddress.c_str()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) { snprintf(buffer, sizeof buffer, "Configuration value missing: %s", "port"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } auto port = htons(stoi(cfgPort)); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) { snprintf(buffer, sizeof buffer, "Failed to connect: %s:%s", cfgAddress.c_str(), cfgPort.c_str()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } return result.release(); }

Page 86: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { char buffer[1024]; ... if (cfgAddress.empty()) { snprintf(buffer, sizeof buffer, "Configuration value missing: %s", "address"); Log::Instance().Write(buffer); throw ConnectionException(buffer); } ... if (address == -1) { snprintf(buffer, sizeof buffer, "Invalid address: %s", cfgAddress.c_str()); Log::Instance().Write(buffer); throw ConnectionException(buffer); } ... }

Page 87: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { ... if (cfgAddress.empty()) { std::stringstream buffer; buffer << "Configuration value missing: " << "address"; Log::Instance().Write(buffer.str()); throw ConnectionException(buffer.str()); } ... if (address == -1) { std::stringstream buffer; buffer << "Invalid address: " << cfgAddress; Log::Instance().Write(buffer.str()); throw ConnectionException(buffer.str()); } ... }

Page 88: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { ... if (cfgAddress.empty()) { static const char * logMessage = "Configuration value missing: address"; Log::Instance().Write(logMessage); throw ConnectionException(logMessage); } ... if (address == -1) { auto logMessage = "Invalid address: " + cfgAddress; Log::Instance().Write(logMessage); throw ConnectionException(logMessage); } ... }

Page 89: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) { static const char * logMessage = "Configuration value missing: address"; Log::Instance().Write(logMessage); throw ConnectionException(logMessage); } auto address = inet_addr(cfgAddress.c_str()); if (address == -1) { auto logMessage = "Invalid address: " + cfgAddress; Log::Instance().Write(logMessage); throw ConnectionException(logMessage); } auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) { static const char * logMessage = "Configuration value missing: port"); Log::Instance().Write(logMessage); throw ConnectionException(logMessage); } auto port = htons(stoi(cfgPort)); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) { auto logMessage = "Failed to connect: " + cfgAddress + ":" + cfgPort; Log::Instance().Write(logMessage); throw ConnectionException(logMessage); } return result.release(); }

Page 90: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) { FailedToConnect("Configuration value missing: address"); } auto address = inet_addr(cfgAddress.c_str()); if (address == -1) { FailedToConnect("Invalid address: " + cfgAddress); } auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) { FailedToConnect("Configuration value missing: port"); } auto port = htons(stoi(cfgPort)); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) { FailedToConnect("Failed to connect: " + cfgAddress + ":" + cfgPort); } return result.release(); }

Page 91: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) FailedToConnect("Configuration value missing: address"); auto address = inet_addr(cfgAddress.c_str()); if (address == -1) FailedToConnect("Invalid address: " + cfgAddress); auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) FailedToConnect("Configuration value missing: port"); auto port = htons(stoi(cfgPort)); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) FailedToConnect("Failed to connect: " + cfgAddress + ":" + cfgPort); return result.release(); }

Page 92: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) FailedToConnect("Configuration value missing: address"); auto address = inet_addr(cfgAddress.c_str()); if (address == -1) FailedToConnect("Invalid address: " + cfgAddress); auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) FailedToConnect("Configuration value missing: port"); auto port = htons(stoi(cfgPort)); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) FailedToConnect("Failed to connect: " + cfgAddress + ":" + cfgPort); return result.release(); }

Page 93: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

Connection * CreateServerConnection() { auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) FailedToConnect("Configuration value missing: address"); auto address = inet_addr(cfgAddress.c_str()); if (address == -1) FailedToConnect("Invalid address: " + cfgAddress); auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) FailedToConnect("Configuration value missing: port"); auto port = htons(stoi(cfgPort)); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) FailedToConnect("Failed to connect: " + cfgAddress + ":" + cfgPort); return result.release(); }

Page 94: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

std::unique_ptr<Connection> CreateServerConnection() { auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) FailedToConnect("Configuration value missing: address"); auto address = inet_addr(cfgAddress.c_str()); if (address == -1) FailedToConnect("Invalid address: " + cfgAddress); auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) FailedToConnect("Configuration value missing: port"); auto port = htons(stoi(cfgPort)); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) FailedToConnect("Failed to connect: " + cfgAddress + ":" + cfgPort); return result; }

Page 95: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

std::unique_ptr<Connection> ConnectToServer() { auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) FailedToConnect("Configuration value missing: address"); auto address = inet_addr(cfgAddress.c_str()); if (address == -1) FailedToConnect("Invalid address: " + cfgAddress); auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) FailedToConnect("Configuration value missing: port"); auto port = htons(stoi(cfgPort)); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) FailedToConnect("Failed to connect: " + cfgAddress + ":" + cfgPort); return result; }

Page 96: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

std::unique_ptr<Connection> ConnectToServer() { auto cfgAddress = ConfigurationManager::Instance().GetValue("address"); if (cfgAddress.empty()) FailedToConnect("Configuration value missing: address"); auto address = inet_addr(cfgAddress.c_str()); if (address == -1) FailedToConnect("Invalid address: " + cfgAddress); auto cfgPort = ConfigurationManager::Instance().GetValue("port"); if (cfgPort.empty()) FailedToConnect("Configuration value missing: port"); auto port = htons(stoi(cfgPort)); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) FailedToConnect("Failed to connect: " + cfgAddress + ":" + cfgPort); return result; }

Page 97: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

std::unique_ptr<Connection> ConnectToServer() { auto cfgAddress = ConfigurationManager::Instance().ValueOf("address"); if (cfgAddress.empty()) FailedToConnect("Configuration value missing: address"); auto address = inet_addr(cfgAddress.c_str()); if (address == -1) FailedToConnect("Invalid address: " + cfgAddress); auto cfgPort = ConfigurationManager::Instance().ValueOf("port"); if (cfgPort.empty()) FailedToConnect("Configuration value missing: port"); auto port = htons(stoi(cfgPort)); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) FailedToConnect("Failed to connect: " + cfgAddress + ":" + cfgPort); return result; }

Page 98: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

std::unique_ptr<Connection> ConnectToServer() { auto cfgAddress = Configuration::Instance().ValueOf("address"); if (cfgAddress.empty()) FailedToConnect("Configuration value missing: address"); auto address = inet_addr(cfgAddress.c_str()); if (address == -1) FailedToConnect("Invalid address: " + cfgAddress); auto cfgPort = Configuration::Instance().ValueOf("port"); if (cfgPort.empty()) FailedToConnect("Configuration value missing: port"); auto port = htons(stoi(cfgPort)); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) FailedToConnect("Failed to connect: " + cfgAddress + ":" + cfgPort); return result; }

Page 99: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

std::unique_ptr<Connection> ConnectToServer( const std::string & cfgAddress, const std::string & cfgPort) { if (cfgAddress.empty()) FailedToConnect("Configuration value missing: address"); auto address = inet_addr(cfgAddress.c_str()); if (address == -1) FailedToConnect("Invalid address: " + cfgAddress); if (cfgPort.empty()) FailedToConnect("Configuration value missing: port"); auto port = htons(stoi(cfgPort)); auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) FailedToConnect("Failed to connect: " + cfgAddress + ":" + cfgPort); return result; }

Page 100: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

std::unique_ptr<Connection> ConnectToServer(in_addr_t address, in_port_t port) { auto result = std::make_unique<Connection>(address, port); if (!result->IsOK()) FailedToConnect(address, port); return result; }

Page 101: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks

std::unique_ptr<Connection> ConnectToServer(in_addr_t address, in_port_t port) { return std::make_unique<Connection>(address, port); }

Page 102: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks
Page 103: Clean Coders Hate What Happens To Your Code When You Use These Enterprise Programming Tricks