Labels

Showing posts with label Clean Code. Show all posts
Showing posts with label Clean Code. Show all posts

Monday, November 5, 2012

Clean Code : Meaningful Names

Hi friends,

This post is also about How We Write Clean Code.
when we write clean code First thing we should consider is putting meaningful names. This is a pretty simple thing, only thing you have to do is just think of some thing nice and matching to what they do (variable, function or classes). If u have watch Smurf its just like that you give names that exactly match with their personality.

There are few things you have to consider when giving names.

- Use intention Revealing Names

when you or some one read you variable name they should get What you was planing to do with it.

- Avoid Disinformation

You have to be more careful when you tring to put Small names for variables, it should be more clear to others what you are tring to say. Clearly Do Not use soo much of small names in your code

Eg-:  If u write a code like this, can you say what is this A is really equal to is it zero or an another variable ???




- Use Pronounceable Names

 If you can Read the variable name loud in front of others without sounding like an idiot then there is No Problem. Just put names that you can pronuce Eg -: Do not use something like this 'genymdhms' (generation date, year, month, day, hour, minute and second)

- Use Searchable Names

When you write a big codes its not so easy to find variable and constants if you haven't name them properly .
Eg -: if you have just put 7 as the working Days of the week instead of putting a constant 'WORKING_DAYS_OF_THE_WEEK' ,when you want to change the working days you have to check all the 7 's in the code. You will be in trouble if the code contain so many 7's :(

- Class Names

 Classes and objects should have noun or noun phrase names like Customer, WikiPage,Account, and AddressParser.

- Method Names

  When you write a method make sure that you use verb or verb phrase like 'deletePage'

when you are writing names don't use much cute names that only you can get. may be some people get the what you meant but not all.Cuteness in code often appears in the form of colloquialisms or slang. Eg -: don’t use the name whack() to mean kill().

These are just a few things that you have to worry when putting a name. These are just Simple things that make your code much more readable and clean.
Hope you guys get the idea of how to put a good name :D

Be happy Always friends.

Saturday, October 20, 2012

What is Clean Code

Hi Friends,

These days I'm reading a book about clean code. so thought of sharing some new things I have learned. Thing I was wondering when reading was, Why do we write Bad codes????  What is Clean Code???

 WHY do we write bad code?? 

  • Can be because of the rush, deadline is so near and have to do to complete so many functionalists.
  •  Boss would be angry with you if you took the time to clean up your code.
  • Perhaps you were just tired of working on this program and wanted it to be over. 


Some times when we write codes we think of cleaning up the code later after we done writing the whole program or some times we plan to do it tomorrow or many be next week. like wise when we delay to clean the code , code get more messy and difficult to clean the mess. 

LeBlanc’s law: Later equals never.


What is clean Code???


what is clean code? this is the Big question I had when I was reading. Simply if You write a piece of code that should be easily understandable for others. code should be elegant and efficient. The logic should be straightforward to make it hard for bugs to hide.

  •  Contains no duplication;
  •  Expresses all the design ideas that are in the system
This is the 'Big' Dave Thomas's idea about the clean code,
" Clean code can be read, and enhanced by a developer other than its original author. It has unit and acceptance tests. It has meaningful names. It provides one way rather than many ways for doing one thing. It has minimal dependencies, which are explicitly defined, and provides a clear and minimal API. Code should be literate since depending on the  language, not all necessary information can be expressed clearly in code alone "
“Big” Dave Thomas, founder of OTI, godfather of the Eclipse strategy

After reading this post hope You guys get an idea of What is clean code.
Have Fun :)