<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dedipyaman Das</title>
    <description>The latest articles on DEV Community by Dedipyaman Das (@2dsharp).</description>
    <link>https://dev.to/2dsharp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F236869%2Fec073124-b563-4880-953f-5f2d3a7eda93.jpeg</url>
      <title>DEV Community: Dedipyaman Das</title>
      <link>https://dev.to/2dsharp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/2dsharp"/>
    <language>en</language>
    <item>
      <title>PHP sucks, can it suck less?</title>
      <dc:creator>Dedipyaman Das</dc:creator>
      <pubDate>Thu, 26 Sep 2019 18:39:33 +0000</pubDate>
      <link>https://dev.to/2dsharp/php-sucks-can-it-suck-less-1mjb</link>
      <guid>https://dev.to/2dsharp/php-sucks-can-it-suck-less-1mjb</guid>
      <description>&lt;p&gt;PHP has garnered a bad reputation in the software market. Many developers hold strong opinions against the language and to some extent, that's correct. Over the last few years, although PHP has gone through an evolution and is it the same &lt;em&gt;"Fractal of Bad Design"&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post was originally made to my blog: &lt;a href="https://www.twodee.me/blog"&gt;Twodee's Kitchen&lt;/a&gt;. I would love for you to visit that and show some love.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Yes, I admit it. &lt;em&gt;PHP sucks.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;And I shamelessly write PHP code, so I must suck too, right? &lt;/p&gt;

&lt;p&gt;As popularly stated in the famous article &lt;a href="https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/"&gt;PHP: a fractal of bad design&lt;/a&gt; :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PHP is an embarrassment, a blight upon my craft. It’s so broken, but so lauded by every empowered amateur who’s yet to learn anything else, as to be maddening. It has paltry few redeeming qualities and I would prefer to forget it exists at all.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The article has only gotten popular over the years, and it's still being circulated over Quora (One of them being the founder himself, Adam D'Angelo).&lt;/p&gt;

&lt;p&gt;And while the article came out, yes, it was right. It was badly designed and badly implemented. The users made it worse. While PHP came along, it didn't originally plan to be as massively used as it is today. Since it was so easy to adapt to, people started using it everywhere. It worked to some extent.&lt;/p&gt;

&lt;p&gt;As soon as the web got more popular, we changed, our needs changed. PHP didn't. It was still stuck behind, probably due to the community? It was inconsistent with its naming, it had the insecure &lt;code&gt;mysql_*&lt;/code&gt; functions built into its design. There were a lot of gotchas in the language, and having used PHP for a long time, I know that it is a pain.&lt;/p&gt;

&lt;p&gt;But this is 2019. The article was written in 2012. I am surprised people still keep quoting that article &lt;em&gt;everywhere!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;C'mon people, PHP has changed (evolved) a lot, don't tell me the public eye is too blind to see it. &lt;/p&gt;

&lt;p&gt;PHP has had some major pushes like the HipHop to HHVM movement from Facebook and PHP 7. Developers have recognized the issues that came along with it, and they have been addressing it so far. If you are living in 2019 and still writing &lt;code&gt;mysql_*&lt;/code&gt; functions (or blaming PHP for having that), you seriously need to learn to &lt;a href="https://en.wikipedia.org/wiki/RTFM"&gt;RTFM&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So why this hatred still?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Well, as long as something's popular - people will hate it. People hate Java, people hate C++. When millions use your product, you cannot expect everyone to be happy customers with every design decisions that you make. There will be people who don't like your approach and that's true for any remotely popular language.&lt;/p&gt;

&lt;p&gt;People often compare Python to PHP in the web context. I have nothing against Python, I think it's a great language that fits the purposes it was intended for (scripting?) and purposes it was adapted popularly for (AI/ML/Data Science?).&lt;/p&gt;

&lt;p&gt;But here's where Python fails to impress me against PHP:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It's slow. Not a deal-breaker (especially on the web), but I am making arguments for the sake of making arguments. If you still complain about PHP being ugly, I can complain it about being slow.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It needs a framework for anything web. Initially, when I just wanted to get a Python application up and running for the web, I had the community continuously push me over to use Django or Flask. I hate being coupled to a framework, as many others would (and should) be too.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whitespacing? Not a fan. Again, it's not a deal-breaker, but having whitespace &lt;em&gt;mean&lt;/em&gt; something doesn't make sense to me. I understand that it was a design decision to keep the lines cleaner, but when things break because I missed an invisible whitespace - it hurts my feelings.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Its Object-oriented model is alien to me. Access specifiers are done by enforcing conventions with underscores? Okay. No. Maybe it works for some folks, but I like things being implied explicitly rather than implicitly.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But okay, Python is a great language. It works great, it's got a great community and yet - there are people who hate it. And that's okay if you are remotely popular anywhere - you will have people not liking you.&lt;/p&gt;

&lt;p&gt;Javascript on the other hand - It's something I really don't like. It's a matter of personal opinion. Especially after the fact that some Javascript dudes who are a few years older than me were trying to shove Node.js down my throat and bashing PHP for the time I was in front of them. Asserting that Node is far superior, safe and faster than PHP (and anything else for the web) and I should learn Node right away.&lt;/p&gt;

&lt;p&gt;They went as far as saying that PHP invented SQL injection. I stopped trying to speak at that point.&lt;/p&gt;

&lt;p&gt;Coming back to PHP:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do you still have/write legacy PHP code that follows the arcane PHP 5 approach?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I have a list for you:&lt;/p&gt;

&lt;h3&gt;
  
  
  Start writing OOP
&lt;/h3&gt;

&lt;p&gt;While you can still write procedural PHP, the community has moved towards an object-oriented approach. It simply fits the new model and works great to have you structure the code well. With object-oriented, several clean coding practices like SOLID and DRY are automatically implied.&lt;/p&gt;

&lt;p&gt;OOP silently enforces clean structuring of your codebase and keep things separated better. Of course, it's optional, if you like writing spaghetti code, no one's stopping you. You can make the worst out of PHP and give yourself a bad name. But that's entirely up to you, any language will allow you to do that. Not just PHP.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strict type as much as you can
&lt;/h3&gt;

&lt;p&gt;While we are at the subject, also use Strict Types. It's as simple as: &lt;code&gt;&amp;lt;?php declare(strict_types=1)&lt;/code&gt; at the beginning. Static typing surely helps you keep things consistent and safe. Yes, PHP is a dynamically typed language, and we need to squeeze that feature out of PHP sometimes. But in most cases, going by the safer path with strict types can save you from a lot of weirdness and unpredictability at runtime.&lt;/p&gt;

&lt;h3&gt;
  
  
  Namespaces, please
&lt;/h3&gt;

&lt;p&gt;The include statements on top of the page are no more common and the community recommends that you use namespaces to &lt;em&gt;"import"&lt;/em&gt; modules you need to &lt;em&gt;"use"&lt;/em&gt;. Its a means of abstraction over your raw PHP files that allows you to encapsulate the include logic.&lt;/p&gt;

&lt;p&gt;Yes, it could be weird to use &lt;code&gt;\&lt;/code&gt; as the namespace separator at first, but you'll get used to it. Get rid of those includes and start using namespaces to put things into their correct places. Which brings me to my next point:&lt;/p&gt;

&lt;h3&gt;
  
  
  Composer
&lt;/h3&gt;

&lt;p&gt;If you are planning to start a PHP project, get &lt;a href="https://getcomposer.org/"&gt;composer&lt;/a&gt; immediately. It's a dependency management tool which allows you to define your dependencies, your application and test entry points and load dependencies from the central Packagist repository as you need them. It generates an autoloader for you, and that's the only thing you should &lt;code&gt;include&lt;/code&gt; in your entire project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Throw away &lt;code&gt;mysql_*&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;All the &lt;code&gt;mysql_*&lt;/code&gt; functions have been deprecated for a long, long time and it has been removed in PHP 7 for good. So if you are still complaining about &lt;code&gt;mysql_*&lt;/code&gt; functions being bad, please upgrade your PHP version. The best way to deal with a database as of now is to use &lt;code&gt;PDO&lt;/code&gt; with prepared statements. It's a generic API that works quite well with a vast array of databases.&lt;/p&gt;

&lt;p&gt;The things that I like about PDO are: it's clean, relatively modern design, object-oriented and consistent. You will move to exclusively use PDO in no time once you start a project with PDO.&lt;/p&gt;

&lt;p&gt;Again, don't create DB wrappers like &lt;code&gt;DBConnection extends PDO&lt;/code&gt;. Just don't. If you need some sort of abstraction over PDO, check out the Data Mapper pattern and some ORM like &lt;code&gt;Doctrine&lt;/code&gt; (and not some evil Active Record variant).&lt;/p&gt;

&lt;h3&gt;
  
  
  Separate your concerns
&lt;/h3&gt;

&lt;p&gt;Most of the bashing PHP gets today is because new developers mess it up so bad, that it smells worse than Javascript (I am opinionated, sorry). Because its easy to learn and get started with, newbies just can't resist themselves from writing hacky code and deploying it to production.&lt;/p&gt;

&lt;p&gt;Other languages don't get this because &lt;/p&gt;

&lt;p&gt;a) they have a steeper learning curve&lt;/p&gt;

&lt;p&gt;b) they give out a strict design strategy beforehand.&lt;/p&gt;

&lt;p&gt;c) It takes time to get them up and running.&lt;/p&gt;

&lt;p&gt;Fix this by separating your concerns. I remember once I used to copy-paste portions of long functions to other files to do the same thing, but slightly differently. I understand why beginners do that.&lt;/p&gt;

&lt;p&gt;Start off by making your functions smaller.&lt;/p&gt;

&lt;p&gt;Break your codebase down to small pieces acting independently doing exactly one thing. Read more on SOLID and DRY principles.&lt;/p&gt;

&lt;p&gt;If you have a &lt;code&gt;User&lt;/code&gt; class, don't allow it to be able to create a message, encrypt the message and also send the message to another user. &lt;/p&gt;

&lt;p&gt;Incorporate libraries from the internet, people solved your problems with better testing well before you did. And they did it better. Whilst we may be tempted to have everything custom made, we tend to deviate from the actual business needs. Focus on your business logic, use what's already available.&lt;/p&gt;

&lt;p&gt;But no tight coupling.&lt;/p&gt;

&lt;h3&gt;
  
  
  PSRs to the rescue
&lt;/h3&gt;

&lt;p&gt;And finally, follow coding conventions and read on PHP-FIG. The PHP Standards Recommendations (PSRs) will allow you to have a consistent codebase that others can easily understand and extend. The standards will help you to write code that can be compatible with other code written by others, and that will save you from cursing yourself 3 months after you write some bad PHP code.&lt;/p&gt;

&lt;p&gt;This recommendation applies to every language in general. Follow coding conventions and strive to write better code. Of course, no one can stop you from being a "code-rebel". We don't have the technology to stop you from that yet. &lt;/p&gt;

&lt;p&gt;(Yes we do, coding standards checks during integrations can keep idiots at bay)&lt;/p&gt;

&lt;h3&gt;
  
  
  A few concluding words
&lt;/h3&gt;

&lt;p&gt;Yes, PHP sucks. So does every other language. You just gotta deal with it. &lt;/p&gt;

&lt;p&gt;It's up to you and your team to write code that looks like poetry rather than an ugly war cry. You can write severely bad Java code even with its verbosity and static typing.&lt;/p&gt;

&lt;p&gt;You were scared about starting your next project with PHP because your coworkers will judge? Go right ahead and do that. It's your job and it's your tool. &lt;/p&gt;

&lt;p&gt;Your coworkers are probably too free, maybe their code is still compiling.&lt;/p&gt;

</description>
      <category>php</category>
      <category>javascript</category>
      <category>oop</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Technical debt</title>
      <dc:creator>Dedipyaman Das</dc:creator>
      <pubDate>Thu, 26 Sep 2019 06:59:16 +0000</pubDate>
      <link>https://dev.to/2dsharp/technical-debt-24j0</link>
      <guid>https://dev.to/2dsharp/technical-debt-24j0</guid>
      <description>&lt;p&gt;Software development is cursed with an implied cost at any stage, no matter how elegant your solution looks at any point of time. The cost of fixing what you messed up. Humans write code, and code is prone to ugliness. This piles up over time and dealing with it is a challenge every team faces many times in their lifetime.&lt;/p&gt;

&lt;p&gt;While building any sensible system with code filling more than a page you start breaking things down to pieces, if you aren't you should. When you start abstracting things away into separate smaller modules, you start thinking about how to put them together. Oftentimes, you need to add one tiny feature and then one more, while trying to keep the initial structure you had in mind intact.&lt;/p&gt;

&lt;p&gt;Although the idea was clear at the beginning, the pieces don't simply fit together as easily as you'd want them to be. So, you mess around the structure of the codebase a bit, just fine little harmless tweaks. As features accumulate, you start to see a big change that module's structure.&lt;/p&gt;

&lt;p&gt;What starts off as a clean, well-structured module now became this horrible mess of hacky additions of features. At some point, you stop understanding what it was supposed to do, because it's doing too many things.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;But it works.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So you leave it behind, put a checkbox in your TO-DO list and move on with the next module. Then days after, your requirements change and you need to modify that module once again. Or maybe you'd like to integrate it with some other module. The confusing module is now hard to read, understand and extend. Moving any further would be an impossible task with it.&lt;/p&gt;

&lt;p&gt;You've two choices at this point- &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Discard it entirely and start fresh&lt;/li&gt;
&lt;li&gt;Fix it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unless you're building your first application which doesn't do much, the first is usually not the best option. Especially in a project where you put significant amount of time thinking about and developing that module. It works, and it has some clever crafted code inside it, it's just turned messy.&lt;/p&gt;

&lt;p&gt;Now you have to take a step and fix it.&lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;Technical Debt&lt;/strong&gt;. As in monetary debt, if you don't repay your dues early on, the interests will accumulate and push you into a hard-to-return-from point.&lt;/p&gt;

&lt;p&gt;When you thought, &lt;em&gt;"hey, this works for now, we can fix that later"&lt;/em&gt; you fell a little deeper into the debt trap. Technical debt is the additional cost you implicitly added to your project by choosing an easier path that you will have to pay for in the future.&lt;/p&gt;

&lt;p&gt;Now what would have taken you 3 days of work, will take you about a week. It's the cost of fixing the code (paying off the debt) + the cost of implementing the new feature.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I prevent technical debt?
&lt;/h3&gt;

&lt;p&gt;You don't. You can only minimize it. Think of it as a financial system, and do exactly what you'd do when you loan money off your bank. You pay the dues gradually. Once you are done implementing the first feature in your module, you take some extra time to remove that mess and make it saner.&lt;/p&gt;

&lt;p&gt;Yes, you did put extra time into it, but it lessened the burden for the next feature and decreased the overall cost. This gradual removal of mess enables you to fix the parts of your code that needs the most modifications over time, and hence needs to be the cleanest.&lt;/p&gt;

&lt;p&gt;Cleaning it off gradually over time will make sure the debt doesn't accumulate to the point of no return over time. And it helps you keep the internal code quality fresh at the points where you need it the most.&lt;/p&gt;

&lt;p&gt;Recently, in a system I had used a factory to convert transfer objects to domain entities. It was initially meant to do just that, but I ended up validating the data and checking for redundancy from the persistence layer inside it. All within a factory. It was too smart at that point.&lt;/p&gt;

&lt;p&gt;Eventually I kept using this factory in too many pieces, and adding more logic to make it smarter. I started having a hard time connecting the pieces together and that was a clear violation of &lt;strong&gt;SRP&lt;/strong&gt;. I had to take a day off just to dumb the factory down to just be limited to creating the objects and not do too much.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tight coupling&lt;/strong&gt; is a major evil in a modular system and can often lead to code smell even in the most initially well thought out business logic. Coupling can end up contributing to the technical debt you accumulate over time, and you simply would be confused when something stops working in the cleaning up process.&lt;/p&gt;

&lt;p&gt;When you start paying off the dues at a much later stage in a tightly coupled system, you find yourself breaking things even more than you'd do normally. At that point the paying off process becomes a nightmare and takes huge effort to fix.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unpopular opinion:&lt;/strong&gt; Don't fix something that works and you seldom change.&lt;/p&gt;

&lt;p&gt;If your co-worker built a module that works perfectly fine and integrates just well enough for you to go on with - don't bother about fixing it if you don't modify it frequently enough. There's little reason to fix something that confusing and doesn't need fiddling with. If it meets your business requirements, leave it alone.&lt;/p&gt;

&lt;p&gt;Finally, &lt;strong&gt;write more tests&lt;/strong&gt;. Sometimes, we just need to add a quick fix, inject a hacky piece of code to make something work or to slightly change its behavior. What we don't see at the first glance are the side effects. We tend to look more at whether that quick fix solved the actual problem, and we miss out the side effects it brought along with it. Tests help out with that, you get instant feedback.&lt;/p&gt;

&lt;p&gt;That's why I am an advocate of Test Driven Development. It gets you to move those sticky pieces long before you even are done implementing a feature, and hence reduce the cost. A slight cost added is the cost of writing those tests, but you would have to write them anyway.&lt;/p&gt;

&lt;p&gt;Technical debt is inevitable in software development, and it will come around. And sometimes we need to trade off elegance for making things work, that being said, paying them off sooner will make your life a lot easier.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>tdd</category>
      <category>java</category>
    </item>
  </channel>
</rss>
