11 guidelines for writing higher code

Learn extra at:

And you might want to be very, very certain earlier than you resolve that there might be not more than three of something in your system. A corollary to this rule is…

Don’t hard-code something

This appears apparent, however some builders like to hard-code stuff. Candy child Elvis, I even see this type of factor on a regular basis:


someString.PadLeft(13);

I imply, actually? Why 13? Why not 14? Or 12? How a few fixed that explains the that means of the worth?

You could not assume so, however each time you create an object inside a category, you’re hard-coding that class and its implementation. For instance:


class SimpleEncryptor {
  public encrypt(plainText: string): string {
    const weakEncryption = new WeakEncryptionAlgorithm();
    return weakEncryption.encrypt(plainText);
  }
}

So what if you wish to change the encryption algorithm? 

As an alternative, use dependency injection, and you should utilize any algorithm you need:


interface IEncryptionAlgorithm {
  encrypt(plainText: string): string;
}

class SimpleEncryptor {
  public encrypt(plainText: string, encryptionAlgorithm: IEncryptionAlgorithm): string {
    return encryptionAlgorithm.encrypt(plainText);
  }
}

Typically ‘over-engineering’ is correct engineering

Imagine me, I get the notion of over-engineering. I simply advised you to maintain issues easy. However generally, doing issues “the fitting means” appears like over-engineering. Everytime you assume you’re over-engineering, cease and think about that, properly, perhaps you aren’t. Creating interfaces and coding towards them can seem to be over-engineering. I do know it’s a high quality line to stroll, however planning forward for one thing you understand you have to just isn’t unsuitable. And this leads me to…

Typically you’re going to want it

I’ve by no means fairly understood the YAGNI precept (“You aren’t gonna want it”). All too usually, you discover that, properly, you understand, you probably did find yourself needing it. And by then, implementing this factor you “weren’t going to want” has turn out to be such a nightmare that you simply dearly want you had gone forward an laid the groundwork for it. 

Perhaps you hard-coded one thing (you weren’t going to want flexibility right here, proper?). Perhaps you didn’t plan on ever needing seven taxes, or a unique encryption algorithm. I see no hurt in pondering “You already know, ultimately, we’re going to must cope with greater than widgets right here” and coding in order that adjustments are straightforward when new cogs and sprockets inevitably come alongside.

Turn leads into sales with free email marketing tools (en)

Leave a reply

Please enter your comment!
Please enter your name here