49
Code smells in MVC systems Maurício Aniche @mauricioaniche https://www.aluguetemporada.com.br/imovel/p4086968

Code smells in MVC applications (Dutch Spring meetup)

Embed Size (px)

Citation preview

CodesmellsinMVCsystemsMaurício Aniche@mauricioaniche

https://www.aluguetemporada.com.br/imovel/p4086968

beautifulcode

Iwillfindasolutionforthat!

OfcourseIdidn’t…

What’stheugliestthingyouseeinasourcecode?

Longmethods

Complexmethods

Godclasses Coupledclasses

Noncohesiveclasses

ThedarksideIsenseinyou…

Ifeeltheseonesarenotenough!

8

Talktodevelopersyoushould,youngpadawan!

Why?theirownsmellseachpartofthearchitecturehas!

A catalogueof smells

specific to MVC(after talking to 20+70 developers)

10

Grrrrrr…Controllers that offer many

servicesGrrrr…

Repositories that deal with many Entities

Grrrrr…

Aniche, Maurício, Bavota, Gabriele, Treude, Christoph, van Deursen, Arie, Gerosa, Marco.“A Validated Set of Smells in Model-View-Controller Architecture”. Software Maintenance and Evolution (ICSME), 2016, 32th International Conference, IEEE, 2016.

Promiscuous ControllerOffers too many actions

ICSME2016 11

BrainControllerToo much business logic

ICSME2016 12

Meddling ServiceQueries the database

ICSME2016 13

Brain RepositoryComplex logic (SQL or code)

ICSME2016 14

Laborious Repository Method

Many queries in a single method

ICSME2016 15

Fat RepositoryManages many entities

ICSME2016 16

Spring MVC in github

ICSME2016 17

Detection strategies

ICSME2016 18

3Q + 1.5IRQ

Smelly classes exist!

ICSME2016 19

Are they smells?

ICSME2016 20

Developers’ perceptions

Significantly different

ICSME2016 21

OOPattributes?Thinkaboutcontextyou

should!WewannameasureOOattributes,butwedon’tagreewiththeresults!

23

Sugarlevel150[70,120]

24

Coupling10<15

Howtofindthisnumber?

SCAM2016 25

Benchmarking

Benchmarkin Controllers

26

Benchmarkin Controllers

27

That’s not whatwe want

28

29

Quantiles

Coup

ling(CBO

)

Aniche, M., Treude, C., Zaidman, A., van Deursen, A., Gerosa, M.A., “SATT: Tailoring Code Metric Thresholds for Different Software Architectures”. 16th IEEE International Working Conference on Source Code Analysis and Manipulation, 2016.

30

Expertsexplained to us

SATT for the rescue31

32

Itindeedbecameatool!

33

Database-centriccodeBecauseinthewrong

place,theyare!It’sveryhardtofindsomeSQLqueriesinmycodebase…

Queriescan get complicated!SELECT

p.name as project,c.id as commitId,a.name as artifactName,a.path as artifactPath

FROMProjects p

JOIN Commits c ON c.project_id = p.id

JOIN Artifacts a ON a.commit_id = c.id

WHEREp.repository = ’Apache’;

WhereshouldIput

it?

Project #ofclasses

#ofcommits

#ofDAOs #ofmethods

Gnarus 924 10451 39 233

Caelumweb 1321 12077 81 590

There aremany queries!

public class InvoiceDAO {public Invoice findById(int id) { … }public ShoppingCart find(User u) { … })public List<Invoice> getAll() { … }public void save(Invoice inv) { ...}

}

Bythemethodsignatureswe

should!

Whereshouldwestart?

Project #ofDAOmethods

#of”correct”methods

#of”wrong”methods

%ofwrongmethods

Caelumweb 590 511 79 13.38%

Gnarus 233 200 33 14.16%

Project #ofinspectedmethods

#ofagreement

%ofagreement

Caelumweb 79 59 74.68%

Gnarus 33 16 48.48%

Aniche, Maurício F., Gustavo A. Oliva, and Marco A. Gerosa. “Are the Methods in Your Data Access Objects (DAOs) in the Right Place? A Preliminary Study.”. 6th Workshop on Managing Technical Debt, in conjunction with ICSME2014, Canada. Short paper.

TestcodestabilityNothappenitshould!

Wehatesystemtestingbecausewehavetorewritethem

often!

Largeseleniumcodeis!

Changefrequently,webapplications

A catalogueof patterns

43

Fixture APIID in HTML

Move Fast, Move Slow…

Aniche, M., Guerra, E., Gerosa, M. “A Set of Patterns to Improve Code Quality of Automated Functional Tests of Web Applications”. 21th Conference on Pattern Languages of Programs. 2014.

Alotofstorytelling…(fromthebeginning!)

Wedoneedhardevidenceoftheimportanceofcodesmells…buthow?

Thesolution?

Howtomeasurereadability,easetomaintain,easetotest,easefor

someoneelsetostarttomaintain,etc,howmuchitslowsmedown?

AnswersIdon’thave…

47

I’llfindout!J

Thanks!@mauricioaniche

YoungmeworkingonalowqualitycodethatIwrotemyselfat

myfirstinternationalproject(DominicanRepublic,2006).

Sorryaboutmyhair.

Mybossesdoingpeerpress…oops,pairprogramming!