Clean Coded Theme October 2, 2017 admin Post in HTML How to write Clean Code? There are two things- Programming and Good Programming. Programming is what we have all been doing. Now is the time to do good programming. We all know that even the bad code works. But it takes time and resources to make a program good. Moreover, other developers mock you when they are trying to find what all is happening in your code. But it’s never too late to care for your programs. Characteristics of a clean code: It should be elegant — clean code should be pleasing to read. Reading it should make you smile the way a well-crafted music box or well-designed car would. Clean code is focused —each function, each class, each module exposes a single-minded attitude that remains entirely undistracted, and unpolluted, by the surrounding details. Always care of clean code. Someone has taken the time to keep it simple and orderly. They have paid appropriate attention to details. They have cared. Runs all the tests Contains no duplication Minimize the number of entities such as classes, methods, functions, and the like. How to write clean code? Meaningful Names Use intention revealing names. Choosing good names takes time but saves more than it takes. The name of a variable, function, or class, should answer all the big questions. It should tell you why it exists, what it does, and how it is used. If a name requires a comment, then the name does not reveal its intent. Class Names — Classes and objects should have noun or noun phrase names like Customer, Wiki Page, Account, and Address Parser. Avoid words like Manager, Processor, Data, or Info in the name of a class. A class name should not be a verb. Method Names —Methods should have verb or verb phrase names like post Payment, delete Page, or save. When constructors are overloaded, use static factory methods with names that describe the arguments. For example, Complex fulcrum Point = Complex.FromRealNumber(23.0); is generally better than Complex fulcrumPoint = new Complex Pick One Word per Concept —Pick one word for one abstract concept and stick with it. For instance, it’s confusing to have fetch, retrieve, and get as equivalent methods of different classes. How do you remember which method name goes with which class? Likewise, it’s confusing to have a controller and a manager and a driver in the same code base. What is the essential difference between a DeviceManager and a Protocol- Controller? Functions The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that. This implies that the blocks within if statements, else statements, while statements, and so on should be one line long. Probably that line should be a function call. Not only does this keep the enclosing function small, but it also adds documentary value because the function called within the block can have a nicely descriptive name. Function arguments A function shouldn’t have more than 3 arguments. Keep it as low as possible. Reducing the number of arguments by creating objects out of them may seem like cheating, but it’s not. Now when I say to reduce a function size, you would definitely think how to reduce try-catch as it already makes your code so much bigger. My answer is making a function containing just the try-catch-finally statements. And separate the bodies of try/catch/finally block in separate functions. Eg- This makes the logic crystal clear. Function names easily describe what we are trying to achieve. Error handling ignored. This provides a nice separation that makes the code easier to understand and modify. Error Handling is one thing — Function should do one thing. Error handling is one another thing. If a function has try keyword then it should be the very first keyword and there should be nothing after the catch/finally blocks. Comments If you are writing comments to prove your point, you are doing a blunder. Ideally, comments are not required at all. If your code needs commenting, you are doing something wrong. Our code should explain everything. Modern programming languages are English like through which we can easily explain our point. Correct naming can prevent comments. Objects and Data Structures This is a complex topic so pay good attention to it. First we need to clarify the difference between object and Data Structures. These 2 things are completely different. One is just about storing data and other is how to manipulate that data. Consider, for example, the procedural shape example below. The Geometry class operates on the three shape classes. The shape classes are simple data structures without any behavior. All the behavior is in the Geometry class. Consider what would happen if a perimeter () function were added to Geometry. The shape classes would be unaffected! Any other classes that depended upon the shapes would also be unaffected! On the other hand, if I add a new shape, I must change all the functions in Geometry to deal with it. Again, read that over. Now consider another approach for the above scenario. Now we can easily add new Shapes i.e. data structures as compared to previous case. And if we have to add perimeter () function in only one Shape, we are forced to implement that function in all the Shapes as Shape class is an interface containing area () and perimeter () function. In any complex system there are going to be times when we want to add new data types rather than new functions. For these cases objects and OO are most appropriate. On the other hand, there will also be times when we’ll want to add new functions as opposed to data types. In that case procedural code and data structures will be more appropriate.