java - Avoiding constant checking to ensure json object contains elements -


i'm writing code , have feeling of urgh when feels ugly , inelegant can't see immediate way avoid it.

i have json object i'm getting third party. know expect can't sure each element there need check exists this:

if (object.has(element)) {   jsonobject element = object.get(element); } 

the problem have go pretty deep object , begins feel ugly when many nested ifs. here example:

private boolean lineexists(jsonarray orders, line lineitem) {     final string line_items = "lineitems";     final string elements = "elements";     final string note = "note";     boolean exists = false;      log.debug("checking if line item exists in order...");      // if there no order payload order hasn't been posted there can't duplicate line item     if (orders != null) {         (int = 0; < orders.size(); i++) {             jsonobject order = orders.get(i).getasjsonobject();             if (order.has(line_items)) {                 jsonobject lineitems = order.get(line_items).getasjsonobject();                 if (lineitems.has(elements)) {                     jsonarray elements = lineitems.get(elements).getasjsonarray();                     (int j = 0; j < elements.size(); j++) {                         jsonobject existingline = elements.get(j).getasjsonobject();                         if (existingline.has(note)) {                             string note = existingline.get(note).getasstring();                             // note may change after comparison check if id contained in                             // note                             if (note.contains(lineitem.getnote())) {                                 exists = true;                                  log.warn("line item note containing '{}' exists in order.",                                         lineitem.getnote());                             }                         }                     }                 }             }         }     }      return exists; } 

i know can split of tests out own method this:

private boolean lineexistcheck(jsonobject order) {     final string line_items = "lineitems";     final string elements = "elements";      return order.has(line_items) && order.get(line_items).getasjsonobject().has(elements); } 

i'm wondering if there design pattern or way of thinking me write better code in circumstance.

you want read single layer of abstraction principle.

in essence, idea each thing requires indent right ... should better go own method. so, not put tests separate methods; go further.

and side note: using final ... doesn't buy anything. in eyes (but opinion on style!) waste makes cpu burn cycles no reason. going 1 step further; might make more sense turn local constants global static ones.

the other thing (which of course tricky, given fact working on jsonobject) ... code "nice" counter example tell dont ask. point is: code querying internal status of other objects; make decisions based on that. in essence, procedural programming. in real oo, don't ask object information something; nope; tell object whatever needed. said; doesn't here; given fact dealing generic jsonobject; , not "real" object has known , well-defined interface using "tell" do.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -