mardi 11 juillet 2023

Why are developers leaning towards less performant and very-easy-to-read coding practices?

I've been a programmer for +7 years now, and have been making a living by it for +5 years; throughout my career I haven't ever felt the need to see codes that even a 5 year old can read (which seems to be one of the measurements that modern practices encourage), neither have I even seen that practice executed in action in a whole production project. So, my question from the community, and specifically from the ones encouraging these practices, is 'why is this considered an evolution, and what are the real productive benefits compared to a more traditional approach?'

For better understanding, here's what I'd call a code 'encouraged by modern approach' in C#:

using System;

namespace ModernApproach {
    class Program {
        static void Main() {
            Person person = new ("John", "Doe");
            person.PrintFullName();
        }
    }

    interface IPrintable {
        void Print();
    }

    class Person : IPrintable {
        private readonly string firstName;
        private readonly string lastName;

        public Person(string firstName, string lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public void Print() {
            Console.WriteLine(GetFullName());
        }

        private string GetFullName() {
            return $"{firstName} {lastName}";
        }
    }
}

and a more traditional approach

// Traditional Approach

using System;

namespace TraditionalApproach {
    class Program {
        static void Main() {
            Person person = new {
                FirstName = "John",
                LastName = "Doe"
            };
            person.PrintFullName();
        }
    }

    class Person {
        public required string FirstName;
        public required string LastName;

        public void PrintFullName() {
            Console.WriteLine($"{FirstName} {LastName}");
        }
    }
}

Of course this isn't an ideal example but it was the best I could come up with right now. I hope you get the idea.

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire