Skip to main content

Posts

Showing posts with the label clean code

[Post Event] Refactoring and Clean Code Workshop, Sibiu, January 26, 2016

This week I had the opportunity to have a 3 hours workshop at Lucian Blaga University  where I talked about Refactoring and Code Smells in general. We also took a look on Visual Studio 2015 features that help us to refactor our code. Below, you can find the slides from my presentation. The code source I will not share because is ugly and smelly (smile). Radu vunvulea refactoring&code smells from Radu Vunvulea See you next time!

Clean Code – Boundaries, Error Handling and Objects

In the last 3 months I tried to talk about different subjects presented in Clean Code. Even if this is the 4th article about this topic, I have the feeling that there are so many things that we should talk about when we are talking about a clean and good written code. We could say that ‘Clean Code’ book, written by Robert C. set the standards in our industry from this perspective. It is the developers Bible and many times it is a used as the ‘law’ of the code. I don’t want to go deeper into this subject, but I promise that one time I will talk in details why/why not we should use this book as THE Bible. Today topics In this article I will try to talk about Objects and Data Structure, Error Handling and Boundaries I expect to go from the code format, to how the code should look like and how we should implement different features. Objects and Data Structure I think that all of us remember the University courses, when teachers try to explain us that we should only expose from a ...

Clean Code - Comments and Formatting

Introduction In the last two posts we discovered what kind of naming we should use for our methods, fields, classes and so on. Related to this we saw we should always use meaningful names that are related to the problem that we want to solve. Also, we saw that a method name should always express an action (start with a verb) and a class name should be always a noun (or to express a noun). After this we talked about function, where we find out that a function should be short, to only one thing and the number of parameters should be limited. All the information from this series is inspired from “Clean Code”, written by Robert C. Martin. I hope that in this way I will be able to make people to read this book and write better code. What next? In this article we will talk about comments in our code and code formatting. The scope is to try to find out when we should add comments in our code and how this comments should look like. Because developers needs to read code every day we need t...

Clean Code - Functions

http://vunvulearadu.blogspot.ro/2014/08/clean-code-naming.html In the last blog post we discover the universe of Clean Code written by Robert C. Martin. We had the opportunity to go deeper on the naming topic and see how easily small things like meaningful names or naming that revel intention can improve the quality and readability of the code itself. Today we will talk dive deeper in Clean Code and we will talk about ‘Functions’. This basic and simple mechanism used to write programs can impact not only how easily a program can be maintain and extended but also the mental health of developers. Don’t forget that long methods will make your eyes bleed. Imagine a book where all the paragraphs are mixed, the font size is different for each of it and a part of them has 20 pages. How easily you can read a book like that. Code should be written in a way that gives the opportunity to people to read it like a book, from to bottom, where each different logic is grouped separately. Happy st...

Clean Code - Naming

 Introduction If you are a ‘true’ developer than you heard about ‘Clean Code’ written by Robert C. Martin. In many companies this book become part of the developer bible. In combination with ‘Clean Coder’ I would say that this two books are mandatory for all developers. I will start a series of article related to this topic. If you already read this book, than it is a good occasion to refresh your memories. For others, it is the perfect moment to discover how good code should look like. All the main ideas are from ‘Clean Code’. You can look at this series of articles as a summary of the book itself. Why? I decided to start to write about this topic because there are things that needs to be remembered and reviewed from time to time. ‘Clean Code’ is the kind of book that you don’t read once and throw it in a dark corner of your room. This is the kind of book that you read it again and again. Every time you will discover new things that you missed or things that are reveling to...

Coding Stories

Parameter names public void class Person { … public bool IsSimilar(Person person2) { … } } What do you think about the name of parameter name ‘person2’. Names of parameters, fields like xxx1, xxx2, xxx3 are not the best choice. In this case maybe a better name would be ‘otherPerson’. Magic numbers public void ValidatePhone(string phoneNumber) { … phoneNumber.Contains(“40”); } The “40” don’t’ say nothing to a reader. What this value represent, why is used and so on. If this value is used in only one place you should at least extract a constant in the body of method. In the case the same value is used in different places of the application, you should put this constraints in a common place. Property names and enums public class InvitesFilter { public bool SendByLetter { get; set; } public bool SendBySms { get; set; } public bool SendByMail { get; set; } … } First of all, do we really need the ‘SendBy’ prefix? We already know that it is in invitation filter. You ...

Code refactoring - NULL check

Part 1 Part 2 Part 3 Let’s look over the following code: public class Foo { SomeFoo _some; ... public int GetA() { if(_some == null) { return 0; } return _some.A; } public string GetB() { if(_some == null) { return null; } return _some.B; } public int GetTotalX() { if(_some == null) { return -1; } return _some.A + _some.C; } } We can see that the IF checks appears in more than one place. Even if is only a simple check, there can be a lot of places where it appears. If we manage to extract this check in a generic method, than the code would be more legible. Because we don’t return each time the default value of an object we need to be able to return custom “default” value for the case when or object is null. We can image a method that accept as parameter the object that we check if is null or not and another two parameters that represent the default value that need to be return and...

Java Script code refactoring - hands on code

Let's see how we can create a mechanism that based on some flags; it will able to determine the status of some objects. The first version of code would look like this: ItemStatus = { Status1: "Status1", Status2: "Status2", Status3: "Status3" }(); ItemTypes = { Item1: "Item1", Item2: "Item2", Item3: "Item 3" }(); var ItemConfiguration = function () { function ItemConfiguration() { } ItemConfiguration.prototype = function () { getItemConfiguration: function (itemTypes, flag1, flag2, flag3, flag4) { switch (itemTypes) { case ItemTypes.Item1: if (flag1 && flag2) { return ItemStatus.Status1; } else if (flag3 || flag4) { return ItemStatus.Status2; } break; case ItemTypes.Item2: if (...

Don't name your class "XXXManager"

How to you name your classes? How many classes you have in your current project named "Manager" and "Repository"? In the last period of time I saw a lot of classes named "Manager", so may that I begin to think that this is a suffix for any class that do some actions. In this blog post I will talk about some suffixed that can be used when we need to name a class. First of all let why I don't like the "Manager" suffix? Because you cannot understand what the class do. Basically, "Manager" is so general that you can put almost anything there. When I read the name of the class I expect to understand what it does. For example a class with "Writer" suffix will tell me that this class is used to write data. In the next part of the post I will enumerate some common suffix and when we can use them: XXXFactory - Create objects XXXWritter - Write data to a specific location XXXReader - Read data from a specific location XXXProtocol -...

Code refactoring - Create base class/interface when is needed

When I made the last code review on a project I found the following lines of code: Original version public abstract class FooBase { Person _person; public void string PersonId() { if(_person is Student) { return ((Student)_person).Id; } if(_person is Worker) { return ((Worker)_person).Id; } return _someDefaultValue; } public void string ScreenName() { if(_screen is MainScreen) { return ((MainScreen)_screen).Name; } if(_screen is SettingsScreen) { return ((SettingsScreen)_screen).Name; } ((DefaultScreen)_screen).Name ; } } After some new functionality was added: public abstract class FooBase { Person _person; public void string PersonId() { if(_person is Student) { return ((Student)_person).Id; } if(_person is Worker) { return ((Worker)_person).Id; } if(_person is Vampire) { return ((...

What Is Clean Code?

Reciteam " Clean Code " zilele astea È™i am dat peste definiÈ›iile date de către mai multi programatori a cea ce înseamna un cod curat. Mai jos găsiÈ›i aceste citate: I like my code to be elegant and efficient. The logic should be straightforward to make it hard for bugs to hide, the dependencies minimal to ease maintenance, error handling complete according to an articulated strategy, and performance close to optimal so as not to tempt people to make the code messy with unprincipled optimizations. Clean code does one thing well. Bjarne Stroustrup Clean code is simple and direct. Clean codereads like well-written prose. Clean code neverobscures the designer’s intent but rather is fullof crisp abstractions and straightforward linesof control. Grady Booch Clean code can be read, and enhanced by adeveloper other than its original author. It hasunit and acceptance tests. It has meaningfulnames. It provides one way rather than manyways for doing one thing. It has minimal dependencies...