Monday, March 3, 2025

11 guidelines for writing higher code

And you must be very, very certain earlier than you determine that there will 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 couple of fixed that explains the which means of the worth?

Chances are you’ll not assume 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 an alternative, use dependency injection, and you need to use 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

Imagine me, I get the notion of over-engineering. I simply advised you to maintain issues easy. However generally, doing issues “the appropriate manner” appears to be like like over-engineering. Everytime you assume you might be over-engineering, cease and take into account that, nicely, possibly you aren’t. Creating interfaces and coding towards them can look like over-engineering. I do know it’s a tremendous line to stroll, however planning forward for one thing you recognize you have to isn’t incorrect. And this leads me to…

Generally you’ll want it

I’ve by no means fairly understood the YAGNI precept (“You aren’t gonna want it”). All too typically, you discover that, nicely, you recognize, you probably did find yourself needing it. And by then, implementing this factor you “weren’t going to wish” has change into 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 distinct encryption algorithm. I see no hurt in pondering “You realize, ultimately, we’re going to have to take care of greater than widgets right here” and coding in order that adjustments are straightforward when new cogs and sprockets inevitably come alongside.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles