5.5 C
New York
Wednesday, February 26, 2025

11 guidelines for writing higher code



And you’ll want to be very, very positive earlier than you resolve that there could 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?

Chances are you’ll not suppose so, however each time you create an object inside a category, you might be 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 a substitute, 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);
  }
}

Usually ‘over-engineering’ is correct engineering

Consider me, I get the notion of over-engineering. I simply informed you to maintain issues easy. However generally, doing issues “the appropriate manner” seems like over-engineering. Everytime you suppose you might be over-engineering, cease and take into account that, properly, perhaps you aren’t. Creating interfaces and coding towards them can look like over-engineering. I do know it’s a fantastic line to stroll, however planning forward for one thing you recognize you’ll need is just not 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 recognize, you probably did find yourself needing it. And by then, implementing this factor you “weren’t going to wish” 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 wish flexibility right here, proper?). Perhaps you didn’t plan on ever needing seven taxes, or a unique encryption algorithm. I see no hurt in considering “You understand, finally, we’re going to have to take care of greater than widgets right here” and coding in order that adjustments are simple when new cogs and sprockets inevitably come alongside.



Supply hyperlink

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles